Object Creation and Storage (Instantiation) MCQ Practice

Question 1 of 10

The supplied Kiln constructors are available. What is the constructor signature for public Kiln(int degrees, String glaze)?

class Kiln {
    private int temp;
    public Kiln() {
        temp = 200;
    }
    public Kiln(int degrees) {
        temp = degrees;
    }
    public Kiln(int degrees, String glaze) {
        temp = degrees + glaze.length();
    }
    public int getTemp() {
        return temp;
    }
}
  1. A. public Kiln(int degrees, String glaze)
  2. B. void Kiln(int, String)
  3. C. Kiln(int, String)
  4. D. new Kiln(int, String)

Question 2 of 10

Using the supplied Crate constructors, which constructor is selected by new Crate(12)?

class Crate {
    private int n;
    private String label;
    public Crate() {
        n = 0;
        label = "empty";
    }
    public Crate(int count) {
        n = count;
        label = "bulk";
    }
    public Crate(int count, String text) {
        n = count;
        label = text;
    }
    public String describe() {
        return label + n;
    }
}
Call Matching signature
new Crate() Crate()
new Crate(12) ?
new Crate(12, "oak") Crate(int, String)
  1. A. Crate()
  2. B. Crate(int)
  3. C. Crate(int, String)
  4. D. No constructor matches, so the code cannot compile.

Question 3 of 10

Using the supplied Crate constructors, what is the result of new Crate("oak")?

class Crate {
    private int n;
    private String label;
    public Crate() {
        n = 0;
        label = "empty";
    }
    public Crate(int count) {
        n = count;
        label = "bulk";
    }
    public Crate(int count, String text) {
        n = count;
        label = text;
    }
    public String describe() {
        return label + n;
    }
}
  1. A. Crate() is selected and label becomes oak.
  2. B. Crate(int) converts the string to 0.
  3. C. Crate(int, String) is selected with count 0.
  4. D. The call does not match any constructor, so the code cannot compile.

Question 4 of 10

Class Kiln is supplied. Which declaration correctly stores a constructed Kiln object?

  1. A. int hearth = new Kiln(900);
  2. B. Kiln hearth = new Kiln(900);
  3. C. Kiln hearth = 900;
  4. D. String hearth = new Kiln(900);

Question 5 of 10

Class Crate is supplied. Which statement declares a Crate reference that currently refers to no crate?

  1. A. Crate box = new Crate();
  2. B. Crate box = null;
  3. C. int box = null;
  4. D. Crate box = "empty";

Question 6 of 10

Using the supplied Kiln class, what value does getTemp return after this caller runs?

class Kiln {
    private int temp;
    public Kiln() {
        temp = 200;
    }
    public Kiln(int degrees) {
        temp = degrees;
    }
    public Kiln(int degrees, String glaze) {
        temp = degrees + glaze.length();
    }
    public int getTemp() {
        return temp;
    }
}

Kiln hearth = new Kiln(850);
System.out.print(hearth.getTemp());
  1. A. 200
  2. B. 850
  3. C. 0
  4. D. The code cannot compile.

Question 7 of 10

Using the supplied Kiln class, what text does this caller display?

class Kiln {
    private int temp;
    public Kiln() {
        temp = 200;
    }
    public Kiln(int degrees) {
        temp = degrees;
    }
    public Kiln(int degrees, String glaze) {
        temp = degrees + glaze.length();
    }
    public int getTemp() {
        return temp;
    }
}

Kiln hearth = new Kiln(800, "ash");
System.out.print(hearth.getTemp());
  1. A. 800
  2. B. 803
  3. C. 200
  4. D. The code cannot compile.

Question 8 of 10

What text does this caller display? The supplied Buoy class is accessible.

class Buoy {
    private int height;
    public Buoy(int wave) {
        System.out.print("B");
        height = wave;
    }
    public int getHeight() {
        return height;
    }
}

System.out.print("A");
Buoy gulf = new Buoy(4);
System.out.print("C" + gulf.getHeight());
  1. A. A4C
  2. B. ABC4
  3. C. AB4C
  4. D. ACB4

Question 9 of 10

Primitive arguments are copied. Using the supplied FerryPass class, what text does this caller display?

class FerryPass {
    private int punches;
    public FerryPass(int start) {
        start = start + 2;
        punches = start;
    }
    public int getPunches() {
        return punches;
    }
}

int rides = 5;
FerryPass pass = new FerryPass(rides);
System.out.print(rides + "," + pass.getPunches());
  1. A. 5,5
  2. B. 7,7
  3. C. 5,7
  4. D. 7,5

Question 10 of 10

Using the supplied Kiln class, what text does this caller display?

class Kiln {
    private int temp;
    public Kiln() {
        temp = 200;
    }
    public Kiln(int degrees) {
        temp = degrees;
    }
    public Kiln(int degrees, String glaze) {
        temp = degrees + glaze.length();
    }
    public int getTemp() {
        return temp;
    }
}

int cone = 6;
Kiln first = new Kiln(cone);
cone = 10;
Kiln second = new Kiln();
System.out.print(first.getTemp() + "," + second.getTemp());
  1. A. 6,200
  2. B. 10,200
  3. C. 6,6
  4. D. 10,10