Object Creation and Storage (Instantiation) MCQ Practice

Question 1 of 10

The supplied DeckOven constructors are available. What is the constructor signature for public DeckOven(int degrees, int extra)?

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}
  1. A. DeckOven(int, int)
  2. B. public DeckOven(int degrees, int extra)
  3. C. void DeckOven(int, int)
  4. D. new DeckOven(int, int)

Question 2 of 10

Using the supplied DeckOven constructors, which constructor is selected by new DeckOven(425, 15)?

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}
  1. A. DeckOven()
  2. B. DeckOven(int)
  3. C. DeckOven(int, int)
  4. D. No constructor matches, so the code cannot compile.

Question 3 of 10

Using the supplied DeckOven constructors, what is the result of new DeckOven("hot")?

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}
  1. A. DeckOven() runs and ignores the string.
  2. B. DeckOven(int) converts "hot" to 400.
  3. C. DeckOven(int, int) splits the string into two ints.
  4. D. The call matches no constructor, so the code cannot compile.

Question 4 of 10

Class Gondola is supplied. Which declaration is type-correct?

  1. A. Gondola car = new Gondola(8);
  2. B. int car = new Gondola(8);
  3. C. Gondola car = 8;
  4. D. boolean car = new Gondola(8);

Question 5 of 10

Class DeckOven is supplied. After DeckOven brick = null; which statement is true?

  1. A. brick refers to a DeckOven whose temp is 400.
  2. B. brick holds the null reference and refers to no DeckOven object.
  3. C. The declaration cannot compile because ovens cannot be null.
  4. D. Java stores 0 in brick instead of a reference.

Question 6 of 10

Using the supplied DeckOven class, what does getTemp return after this caller?

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}

DeckOven brick = new DeckOven();
System.out.print(brick.getTemp());
  1. A. 0
  2. B. 400
  3. C. 425
  4. D. The code cannot compile.

Question 7 of 10

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

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}

DeckOven brick = new DeckOven(410, 20);
System.out.print(brick.getTemp());
  1. A. 410
  2. B. 400
  3. C. 430
  4. D. 20

Question 8 of 10

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

class GrainHopper {
    private int kg;
    public GrainHopper(int amount) {
        System.out.print("H");
        kg = amount;
    }
    public int getKg() {
        return kg;
    }
}

System.out.print("G");
GrainHopper bin = new GrainHopper(18);
System.out.print(bin.getKg());
  1. A. G18H
  2. B. GH18
  3. C. G18
  4. D. 18GH

Question 9 of 10

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

class Gondola {
    private int seats;
    public Gondola(int n) {
        n = n - 1;
        seats = n;
    }
    public int getSeats() {
        return seats;
    }
}

int n = 8;
Gondola car = new Gondola(n);
System.out.print(n + "," + car.getSeats());
  1. A. 8,8
  2. B. 7,7
  3. C. 8,7
  4. D. 7,8

Question 10 of 10

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

class DeckOven {
    private int temp;
    public DeckOven() {
        temp = 400;
    }
    public DeckOven(int degrees) {
        temp = degrees;
    }
    public DeckOven(int degrees, int extra) {
        temp = degrees + extra;
    }
    public int getTemp() {
        return temp;
    }
}

int heat = 375;
DeckOven first = new DeckOven(heat);
heat = 500;
DeckOven second = new DeckOven(heat, 10);
System.out.print(first.getTemp() + "," + second.getTemp());
  1. A. 375,510
  2. B. 500,510
  3. C. 375,500
  4. D. 400,510