Question 241
Consider the following class and client code. What is printed?
public class GameTimer {
private int seconds;
private boolean running;
public GameTimer(int s) { seconds = s; running = false; }
public void start() { running = true; }
public int tick(int n) {
if (!running) { return seconds; }
seconds -= n;
if (seconds <= 0) { seconds = 0; running = false; }
return seconds;
}
}
// Client:
GameTimer gt = new GameTimer(30);
int t1 = gt.tick(5);
gt.start();
int t2 = gt.tick(12);
int t3 = gt.tick(10);
int t4 = gt.tick(15);
int t5 = gt.tick(5);
System.out.println(t1 + " " + t2 + " " + t3 + " " + (t4 + t5));Main Solution
Step-by-step trace:
Initial state: seconds = 30, running = false
- t1 = gt.tick(5):
runningisfalse, so the method immediately returnssecondswhich is30. No change to state. t1 = 30. State:seconds = 30,running = false. - gt.start(): Sets
running = true. State:seconds = 30,running = true. - t2 = gt.tick(12):
runningistrue.seconds -= 12→seconds = 18.18 > 0, so no reset. Returns18. t2 = 18. State:seconds = 18,running = true. - t3 = gt.tick(10):
runningistrue.seconds -= 10→seconds = 8.8 > 0, so no reset. Returns8. t3 = 8. State:seconds = 8,running = true. - t4 = gt.tick(15):
runningistrue.seconds -= 15→seconds = -7.-7 <= 0, soseconds = 0,running = false. Returns0. t4 = 0. State:seconds = 0,running = false. - t5 = gt.tick(5):
runningisfalse, so immediately returnssecondswhich is0. t5 = 0. State:seconds = 0,running = false.
Output: System.out.println(t1 + " " + t2 + " " + t3 + " " + (t4 + t5))
= 30 + " " + 18 + " " + 8 + " " + (0 + 0)
= 30 18 8 0
The correct answer is D.
Teacher Explanation
This question tests students' ability to trace object state across multiple method calls. Key concepts: (1) the guard clause if (!running) { return seconds; } causes early return without modifying state when the timer is not running; (2) the timer clamps to 0 and stops when seconds <= 0; (3) once stopped, subsequent tick calls return the current seconds value (0) without decrementing. Students must carefully track both seconds and running through each call.
Education Coach Advice
AP Exam Strategy: For code-tracing questions, create a small table tracking each variable's value after every statement. For this question, track seconds, running, and each t variable. Pay special attention to early-return guard clauses — they skip the rest of the method body entirely. Also note that (t4 + t5) uses parentheses, so it computes the arithmetic sum (0 + 0 = 0) before string concatenation, not string concatenation of t4 and t5 separately.
Traps
Option A (25 18 8 0): A common trap — students may incorrectly apply the tick logic to t1 even though running is false at that point, computing 30 - 5 = 25. The guard clause if (!running) { return seconds; } prevents any subtraction when the timer hasn't been started.
Option C (30 12 8 0): Students might miscalculate t2 as 30 - 12 = 12 instead of correctly getting 18, perhaps forgetting that t1's tick did not change seconds.
Option B (30 18 8 5): Students might think t5 returns 5 (the argument passed) rather than the current value of seconds (which is 0 after the timer stopped).
Answer images

