Learn AP Comp Sci

☰

Problem of the Day

Tuesday, December 1, 2026


Problem:

Consider the following code segment.

int n = 3;
for (int i = 1; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
System.out.print("[]");
}
System.out.println();
}

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


  1. []
    [][]
    [][][]

  2. [][]
    [][][]

  3. []
    [][]
    [][][]
    [][][][]

  4. [][]
    [][][]
    [][][][]

Show solution:

The correct answer is b. The outer loop can be seen to run twice (i = 1, 20 to i inclusive (0 - 1, then 0 - 2).