Methods: How to Write Them FRQ Practice

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

Question 1 of 5

A Beacon object stores private int lumens. The class already has a constructor that sets lumens to 40.

(a) Write a non-void accessor getLumens that returns the stored lumens. (b) Write a void mutator addGlow(int extra) that adds extra to lumens. (c) Explain why getLumens is non-void while addGlow is typically void.

Question 2 of 5

Complete the table for this valve. Then explain which assignment never runs during the first method call.

public class Valve {
    private int flow;
    public Valve() {
        flow = 10;
    }
    public int open(boolean wide) {
        if (wide) {
            return flow + 5;
        }
        flow -= 2;
        return flow;
    }
}
Statement Returned value flow after the statement
Valve v = new Valve(); (none) 10
v.open(true)
v.open(false)

Question 3 of 5

A clerk wants a method that appears to double an int named tickets.

(a) Write a void method doubleUp(int n) that executes n = n * 2;. (b) If tickets is 7 and the clerk calls doubleUp(tickets), state the value of tickets afterward. (c) Explain why that call does not change tickets, and name the mechanism that would let the caller receive 14.

Question 4 of 5

Write a method for class Marker.

(a) Write public int firstMultiple(int start) that uses a while loop, starting at n = start, and returns the first n that is a multiple of 5. Use a return inside the loop. If n grows past start + 20 without a match, return -1. (b) State the value returned by firstMultiple(12). (c) Explain why the remaining loop body after that return does not run.

Question 5 of 5

A Timer object stores private int minutes.

(a) Write a void mutator spend(int used) that subtracts used from minutes only when used is at most minutes; otherwise it should leave minutes unchanged. (b) Write a non-void method leftover() that returns minutes. (c) If minutes starts at 20, trace spend(9), then leftover(), then spend(15), then leftover(), and state both leftover results.