Learn AP Comp Sci

Problem of the Day

Tuesday, November 18, 2025


Problem:

Consider the following code segment.

int r = 1;
int a = 0;
while (r < 3) {
System.out.print(r + " + a + " ");
a += 1;
if (a >= 3) {
r += 1;
a = 0;
}
}

What is printed as a result of executing the code segment?

  1. 0 0 0 1 0 2 1 3 2 4
  2. 1 0 1 1 1 2 2 0 2 1 2 2
  3. 1 0 1 1 1 2 1 3 1 4 1 5
  4. 1 0 1 1 1 2 2 3 2 4 2 5

Show solution:

The correct answer is b. By tracing through the code, and/or carefully observing the beginning and ending values for the loop, the correct answer can be identified.