Constructors MCQ Practice

Question 1 of 10

A Beacon object stores wattage. Which statement is accurate about state?

  1. A. wattage has-a Beacon object.
  2. B. The Beacon class is an attribute of each wattage value.
  3. C. Each Beacon object has-a wattage as part of its state.
  4. D. State exists only while a constructor parameter is being copied, then disappears.

Question 2 of 10

A beacon is constructed with an initial wattage. What text does this Java code display?

public class Beacon {
    private int watts;
    public Beacon(int start) {
        watts = start;
    }
    public int getWatts() {
        return watts;
    }
}

Beacon lamp = new Beacon(250);
System.out.print(lamp.getWatts());
  1. A. 0
  2. B. 250
  3. C. null
  4. D. 250.0

Question 3 of 10

Constructor parameters initialize a tide mark. What text does this Java code display?

public class TideMark {
    private int cm;
    private boolean rising;
    public TideMark(int height, boolean up) {
        cm = height;
        rising = up;
    }
    public String describe() {
        return cm + ":" + rising;
    }
}

TideMark m = new TideMark(80, false);
System.out.print(m.describe());
  1. A. 0:false
  2. B. 80:true
  3. C. 80:false
  4. D. height:up

Question 4 of 10

Lens declares no constructor. What text does this Java code display?

public class Lens {
    private int rings;
    private boolean clean;
    public int getRings() {
        return rings;
    }
    public boolean isClean() {
        return clean;
    }
}

Lens glass = new Lens();
System.out.print(glass.getRings() + " " + glass.isClean());
  1. A. 0 false
  2. B. 0 true
  3. C. The code does not compile because no constructor is written.
  4. D. null false

Question 5 of 10

FogHorn declares no constructor. What text does this Java code display?

public class FogHorn {
    private int blasts;
    private double pitch;
    private boolean armed;
    private String code;
    public String dump() {
        return blasts + "," + pitch + "," + armed + "," + code;
    }
}

FogHorn horn = new FogHorn();
System.out.print(horn.dump());
  1. A. 0,0.0,false,null
  2. B. 0,0,false,null
  3. C. 0,0.0,true,null
  4. D. null,0.0,false,null

Question 6 of 10

Lamp writes one constructor. What happens when this Java code is compiled?

public class Lamp {
    private int lumens;
    public Lamp(int n) {
        lumens = n;
    }
    public int getLumens() {
        return lumens;
    }
}

Lamp bulb = new Lamp();
  1. A. bulb refers to a Lamp whose lumens is 0.
  2. B. The code does not compile because Lamp() is not supplied once Lamp(int) is written.
  3. C. bulb is null.
  4. D. Java adds Lamp() automatically beside Lamp(int).

Question 7 of 10

A slip constructor assigns parameters to the wrong fields. What text does this Java code display?

public class Slip {
    private int length;
    private int beam;
    public Slip(int len, int width) {
        length = width;
        beam = len;
    }
    public String sizes() {
        return length + "x" + beam;
    }
}

Slip s = new Slip(20, 8);
System.out.print(s.sizes());
  1. A. 20x8
  2. B. 8x20
  3. C. 20x20
  4. D. 0x0

Question 8 of 10

Counter is a given mutable class. DockLease stores the parameter without copying. What text does this Java code display?

public class Counter {
    private int value;
    public Counter(int start) {
        value = start;
    }
    public void setValue(int n) {
        value = n;
    }
    public int getValue() {
        return value;
    }
}

public class DockLease {
    private Counter nights;
    public DockLease(Counter start) {
        nights = start;
    }
    public int getNights() {
        return nights.getValue();
    }
}

Counter c = new Counter(6);
DockLease lease = new DockLease(c);
c.setValue(2);
System.out.print(lease.getNights());
  1. A. 6
  2. B. 2
  3. C. 0
  4. D. 8

Question 9 of 10

Counter is a given mutable class. DockLease copies the starting value. What text does this Java code display?

public class Counter {
    private int value;
    public Counter(int start) {
        value = start;
    }
    public void setValue(int n) {
        value = n;
    }
    public int getValue() {
        return value;
    }
}

public class DockLease {
    private Counter nights;
    public DockLease(Counter start) {
        nights = new Counter(start.getValue());
    }
    public int getNights() {
        return nights.getValue();
    }
}

Counter c = new Counter(6);
DockLease lease = new DockLease(c);
c.setValue(2);
System.out.print(lease.getNights());
  1. A. 6
  2. B. 2
  3. C. 0
  4. D. 8

Question 10 of 10

The constructor returns a reference. What text does this Java code display?

public class Buoy {
    private int flashes;
    public Buoy(int start) {
        flashes = start;
    }
    public void blink() {
        flashes++;
    }
    public int getFlashes() {
        return flashes;
    }
}

Buoy outer = new Buoy(3);
Buoy inner = outer;
inner.blink();
System.out.print(outer.getFlashes() + " " + inner.getFlashes());
  1. A. 3 4
  2. B. 4 4
  3. C. 3 3
  4. D. 4 3