Learn AP Comp Sci

☰

Problem of the Day

Wednesday, December 2, 2026


Problem:

Consider the following code segment.

int n = 3;
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < 2*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 a. The outer loop can be seen to run three times (i = 1, 2, 30 to 2 * i times, resulting in 2, 4, then 6 asterisks.