Calling Instance Methods FRQ Practice

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

Question 1 of 5

The SiloBin class is supplied.

class SiloBin {
    private int kg;
    public SiloBin(int start) {
        kg = start;
    }
    public void fill(int n) {
        kg += n;
    }
    public int stored() {
        return kg;
    }
}

SiloBin east = new SiloBin(9);
east.fill(2);
east.fill(6);

(a) Identify the receiver of each fill call. (b) Give the final stored(). (c) Explain the updates in order.

Question 2 of 5

The Lantern class is supplied.

class Lantern {
    private int lumens;
    public Lantern(int start) {
        lumens = start;
    }
    public void dim(int n) {
        lumens -= n;
    }
    public int glow() {
        return lumens;
    }
}

Lantern porch = new Lantern(22);
Lantern also = porch;
also.dim(7);

(a) How many Lantern objects exist after these statements? (b) Give porch.glow() and also.glow(). (c) Explain the result.

Question 3 of 5

The CableSpool class is supplied.

class CableSpool {
    private int meters;
    public CableSpool(int start) {
        meters = start;
    }
    public void cut(int n) {
        meters -= n;
    }
    public int left() {
        return meters;
    }
}

CableSpool reel = new CableSpool(40);
int saved = reel.left();
reel.cut(11);

(a) Give saved after cut. (b) Give reel.left() after cut. (c) Explain why saved did not become 29.

Question 4 of 5

The BadgePress class is supplied.

class BadgePress {
    private int count;
    public BadgePress(int start) {
        count = start;
    }
    public void press() {
        count++;
    }
    public int made() {
        return count;
    }
}

BadgePress shop = null;
System.out.print(shop.made());

(a) State the runtime result. (b) Identify the receiver. (c) Write a replacement first line that makes made() legal, using start count 2.

Question 5 of 5

The SiloBin class is supplied.

class SiloBin {
    private int kg;
    public SiloBin(int start) {
        kg = start;
    }
    public void fill(int n) {
        kg += n;
    }
    public int stored() {
        return kg;
    }
}

(a) Explain why west.fill(4) updates only one hopper when west and east are different objects. (b) Give Java that constructs two hoppers starting at 5, fills only west by 4, and prints both stored values. (c) Predict that printed text.