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 Crate constructors are shown. Students do not design the class.

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

(a) Give the constructor signature selected by new Crate(). (b) Give the constructor signature selected by new Crate(4, "pears"). (c) Explain why those two calls do not select the same constructor.

Question 2 of 5

A student wants to write new Crate(true) using the supplied Crate constructors.

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

(a) State whether the call compiles. (b) Identify the supplied constructor signatures. (c) Explain the mismatch.

Question 3 of 5

Class Kiln is supplied.

(a) Write one valid declaration of a Kiln reference that currently refers to no object. (b) Write one valid declaration that stores a constructed Kiln using Kiln(int). (c) Explain why int hearth = new Kiln(900); is invalid.

Question 4 of 5

Trace the supplied Kiln constructors. Students do not design the class.

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

Caller:

Kiln a = new Kiln();
Kiln b = new Kiln(720);
Kiln c = new Kiln(700, "salt");

(a) Give getTemp for a, b, and c. (b) Identify which constructor ran for each variable. (c) Explain when those constructors execute relative to new.

Question 5 of 5

Primitive arguments are copied. Using the supplied FerryPass class:

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

int rides = 3;
FerryPass pass = new FerryPass(rides);
rides = 9;

(a) What is rides after these statements? (b) What does pass.getPunches() return? (c) Explain both results using call-by-value.