Class Variables and Methods FRQ Practice

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

Question 1 of 5

A factory models one work shift.

public class Shift
{
    private int hours;
    public Shift(int hours)
    {
        this.hours = hours;
    }
    public int getHours()
    {
        return hours;
    }
    public static int overtime()
    {
        return hours - 40;
    }
}

(a) Explain why overtime does not compile. (b) Rewrite overtime so it takes one Shift parameter and uses getHours. (c) Explain why that rewritten class method may call getHours.

Question 2 of 5

A vendor class already has a class variable and a class method.

public class Vendor
{
    private static int RATE = 10;
    public static int extra(int cents)
    {
        return cents * RATE / 100;
    }
}

(a) Write a class method named twiceExtra that takes one int cents and returns extra(cents) + extra(cents). (b) State the value of Vendor.twiceExtra(50). (c) Explain why twiceExtra may use RATE and extra without constructing a Vendor.

Question 3 of 5

Trace the raft helpers.

public class Raft
{
    private int logs;
    public Raft(int logs)
    {
        this.logs = logs;
    }
    public int getLogs()
    {
        return logs;
    }
    public static int load(Raft r)
    {
        return r.getLogs() + 4;
    }
    public static int pair(Raft a, Raft b)
    {
        return load(a) + load(b);
    }
}

Raft p = new Raft(3);
Raft q = new Raft(6);
System.out.print(Raft.pair(p, q));

(a) Complete a trace of load(p), load(q), and pair(p, q). (b) State the displayed text. (c) Explain why load can use getLogs even though load is static.

Question 4 of 5

A badge printer shares one issued count.

public class Badge
{
    public static int issued = 0;
    private int id;
    public Badge(int id)
    {
        this.id = id;
        issued++;
    }
    public int getId()
    {
        return id;
    }
}

Badge one = new Badge(7);
Badge two = new Badge(9);

(a) State the value of Badge.issued after both objects are constructed. (b) Explain why that value is shared rather than stored separately in one and two. (c) Write the class-name statement from another class that prints issued.

Question 5 of 5

A pond tracker mixes a shared count and a constant.

public class Pond
{
    public static final int MAX_FISH = 12;
    public static int fish = 5;
    private int lilies;
    public Pond(int lilies)
    {
        this.lilies = lilies;
        fish += lilies;
    }
    public int getLilies()
    {
        return lilies;
    }
}

Pond a = new Pond(2);
Pond b = new Pond(1);

(a) State Pond.fish and Pond.MAX_FISH after both constructors. (b) Explain why both pond objects see the same fish value. (c) Explain why fish = MAX_FISH; compiles but MAX_FISH = 20; does not.