Object Creation and Storage (Instantiation) FRQ Practice

Responses receive AI feedback against a rubric after submission. Scores are practice estimates.

Question 1 of 5

The supplied DeckOven constructors are shown.

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;
    }
}

(a) Give the constructor signature selected by new DeckOven(450). (b) Give the constructor signature selected by new DeckOven(). (c) Explain how Java chooses between those overloads.

Question 2 of 5

A student tries new DeckOven(400, "steam") with the supplied DeckOven constructors.

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;
    }
}

(a) State whether the call compiles. (b) Name the supplied constructor signatures. (c) Explain why this argument list is rejected.

Question 3 of 5

Class Gondola is supplied.

(a) Write a Gondola reference that currently stores null. (b) Write a correctly typed construction using Gondola(int). (c) Explain why boolean car = new Gondola(8); is invalid.

Question 4 of 5

Trace the supplied GrainHopper constructor.

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

Caller:

System.out.print("S");
GrainHopper a = new GrainHopper(11);
GrainHopper b = new GrainHopper(4);
System.out.print(a.getKg() + "," + b.getKg());

(a) Give the exact displayed text. (b) Explain the order of constructor execution. (c) Give the final kg in each hopper.

Question 5 of 5

Primitive arguments are copied. Using the supplied Gondola class:

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

int n = 10;
Gondola car = new Gondola(n);
n = 2;

(a) What is n after these statements? (b) What does car.getSeats() return? (c) Explain why the later assignment to n does not change the gondola.