this Keyword FRQ Practice

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

Question 1 of 5

A lantern constructor is unfinished.

class Lantern {
    private int oil;
    public Lantern(int oil) {
        /* missing assignment */
    }
    public int level() {
        return this.oil;
    }
}

(a) Write the constructor assignment that stores the parameter in the instance variable. (b) When new Lantern(7) runs, what object does this refer to? (c) After Lantern lamp = new Lantern(7); what does System.out.print(lamp.level()); display?

Question 2 of 5

Complete the missing table cells for this lantern. Then explain why the wick parameter named oil does not overwrite the field by itself.

class Lantern {
    private int oil;
    public Lantern(int oil) {
        this.oil = oil;
    }
    public void wick(int oil) {
        this.oil += oil;
    }
    public int level() {
        return this.oil;
    }
}

Lantern lamp = new Lantern(9);
lamp.wick(4);
Step Statement Field oil Parameter oil
1 constructor body starts 0 9
2 this.oil = oil 9
3 wick(4) body starts 9 4
4 this.oil += oil 4

Question 3 of 5

A badge is supposed to pass the current object to a desk.

class Badge {
    private int pins;
    public Badge(int pins) {
        this.pins = pins;
    }
    public int count() {
        return this.pins;
    }
    public void show(Desk d) {
        /* pass the current Badge */
    }
}
class Desk {
    public void stamp(Badge b) {
        System.out.print(b.count());
    }
}

Badge pass = new Badge(6);
Desk front = new Desk();
pass.show(front);

(a) Write the show body that passes this to stamp. (b) Identify the object this refers to during pass.show(front). (c) State the displayed text.

Question 4 of 5

A mill class method tries to use this.

class Mill {
    private int sacks;
    public Mill(int sacks) {
        this.sacks = sacks;
    }
    public static int leftover() {
        return this.sacks;
    }
}

(a) Does leftover compile? Explain using this. (b) Rewrite leftover as an instance method that legally returns the field. (c) For Mill mill = new Mill(4); write a legal print call and the displayed value.

Question 5 of 5

Write a Pond class with a private int depth. Include:

(a) a constructor Pond(int depth) that uses this to store the parameter; (b) rain(int depth) that adds the parameter to the field using this; (c) announce(Ranger r) that passes this to r.note; (d) the displayed text after this caller, assuming Ranger.note prints the pond depth with a getter.

class Ranger {
    public void note(Pond p) {
        System.out.print(p.getDepth());
    }
}

Pond lake = new Pond(5);
lake.rain(3);
lake.announce(new Ranger());