Constructors MCQ Practice

Question 1 of 10

A Loaf object stores a weight in grams and a crust flag. Which statement correctly describes its state?

  1. A. The Loaf class has-a Loaf object as its only attribute.
  2. B. Each Loaf object has-a weight and a crust flag as part of its state.
  3. C. weight has-a Loaf because the field creates the object.
  4. D. A constructor has-a Loaf, and the object has no state of its own.

Question 2 of 10

A tin is constructed with an initial mass. What text does this Java code display?

public class Tin {
    private int grams;
    public Tin(int start) {
        grams = start;
    }
    public int getGrams() {
        return grams;
    }
}

Tin pan = new Tin(450);
System.out.print(pan.getGrams());
  1. A. 0
  2. B. 450
  3. C. null
  4. D. The constructor does not give pan a usable reference.

Question 3 of 10

Constructor parameters initialize the batch. What text does this Java code display?

public class Batch {
    private int count;
    private boolean proofed;
    public Batch(int n, boolean p) {
        count = n;
        proofed = p;
    }
    public String describe() {
        return count + ":" + proofed;
    }
}

Batch b = new Batch(12, true);
System.out.print(b.describe());
  1. A. 0:false
  2. B. 12:true
  3. C. 12:false
  4. D. n:p

Question 4 of 10

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

public class Rack {
    private int shelves;
    private boolean rolling;
    public int getShelves() {
        return shelves;
    }
    public boolean isRolling() {
        return rolling;
    }
}

Rack r = new Rack();
System.out.print(r.getShelves() + " " + r.isRolling());
  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

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

public class Label {
    private int size;
    private double ink;
    private boolean glossy;
    private String text;
    public String dump() {
        return size + "," + ink + "," + glossy + "," + text;
    }
}

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

Question 6 of 10

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

public class Pan {
    private int cm;
    public Pan(int width) {
        cm = width;
    }
    public int getCm() {
        return cm;
    }
}

Pan p = new Pan();
  1. A. p refers to a Pan whose cm is 0.
  2. B. p is null because the default constructor returns no object.
  3. C. The code does not compile because Pan() is not provided once Pan(int) is written.
  4. D. Java still supplies Pan() for every class.

Question 7 of 10

The constructor returns a reference that can be shared. What text does this Java code display?

public class Bowl {
    private int ml;
    public Bowl(int start) {
        ml = start;
    }
    public void add(int extra) {
        ml += extra;
    }
    public int getMl() {
        return ml;
    }
}

Bowl first = new Bowl(100);
Bowl second = first;
second.add(40);
System.out.print(first.getMl() + " " + second.getMl());
  1. A. 100 140
  2. B. 140 140
  3. C. 100 100
  4. D. 140 40

Question 8 of 10

Counter is a given mutable class. OvenTimer stores the constructor 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 OvenTimer {
    private Counter minutes;
    public OvenTimer(Counter start) {
        minutes = start;
    }
    public int getMinutes() {
        return minutes.getValue();
    }
}

Counter c = new Counter(8);
OvenTimer t = new OvenTimer(c);
c.setValue(3);
System.out.print(t.getMinutes());
  1. A. 8
  2. B. 3
  3. C. 0
  4. D. 11

Question 9 of 10

Counter is a given mutable class. OvenTimer copies the starting value into a new Counter. 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 OvenTimer {
    private Counter minutes;
    public OvenTimer(Counter start) {
        minutes = new Counter(start.getValue());
    }
    public int getMinutes() {
        return minutes.getValue();
    }
}

Counter c = new Counter(8);
OvenTimer t = new OvenTimer(c);
c.setValue(3);
System.out.print(t.getMinutes());
  1. A. 8
  2. B. 3
  3. C. 0
  4. D. 11

Question 10 of 10

FlourBin writes no constructor. Which printed dump matches Java's default field values?

public class FlourBin {
    private double kilos;
    private boolean sealed;
    private String mill;
    public String dump() {
        return kilos + "," + sealed + "," + mill;
    }
}
Field Type
kilos double
sealed boolean
mill String
  1. A. 0.0,false,null
  2. B. 0,false,null
  3. C. 0.0,true,null
  4. D. 0.0,false,