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 Kettle object stores private int cups.

(a) Write a non-void accessor getCups that returns cups. (b) Write a void mutator pourOut(int n) that subtracts n from cups. (c) Identify which method is the accessor and which is the mutator, and explain the void versus non-void choice.

Question 2 of 5

Complete the table for this spool. Then explain why the last assignment in wind does not run on the first call.

public class Spool {
    private int yarn;
    public Spool() {
        yarn = 18;
    }
    public int wind(boolean tight) {
        if (tight) {
            return yarn - 4;
        }
        yarn += 1;
        return yarn;
    }
}
Statement Returned value yarn after the statement
Spool s = new Spool(); (none) 18
s.wind(true)
s.wind(false)

Question 3 of 5

A ledger tracks an int named points.

(a) Write a void method bonus(int p) that executes p += 10;. (b) If points is 22 and the clerk calls bonus(points), state the value of points afterward. (c) Explain why the argument is unchanged, and write a non-void alternative that lets the caller store 32.

Question 4 of 5

Write a method for class Sensor.

(a) Write public int firstEven(int start) that uses a while loop and returns the first even value at or above start. Use a return inside the loop. Stop and return -1 if the value exceeds start + 10. (b) State the result of firstEven(7) and of firstEven(8). (c) Explain how return inside the loop differs from finishing the loop and then returning.

Question 5 of 5

A Latch object stores private int pins.

(a) Write a void mutator lockMore(int extra) that adds extra to pins only when extra is positive. (b) Write a non-void accessor getPins that returns pins. (c) If pins starts at 3, trace lockMore(-2), getPins(), lockMore(4), getPins(), and state both getPins results. Mention what happens to the primitive parameter extra.