Learn AP Comp Sci

☰

Problem of the Day

Tuesday, November 17, 2026


Problem:

Consider the following method.

public static void nestedLoops(int a, int b)
{
for (int i = a; i < b; i++)
{
System.out.print(i);
int j = b;
while (j >= a)
{
System.out.print(i + "" + j);
j--;
}
System.out.print(j);
}
}

What will be printed as a result of the call nestedLoops(2, 3); ?

  1. 223221
  2. 221223
  3. 211233
  4. 222223

Show solution:

The correct answer is a. I'm not a big fan of problems like these because it's not clear to me that the loops are doing anything useful. Also, the variable names are terrible. The intent of a problem like this is to gauge your ability to do code-tracing: tracking the flow of instruction execution and the state of the different variables as the program runs. If you're working on paper, it's helpful to write down variables and the values they reference to see how things change over time.