Nested Iteration

AP Computer Science A· difficulty 3/5

int sum = 0;
for (int i = 1; i <= 3; i++) {
  for (int j = 1; j <= 3; j++) {
    sum += j;
  }
}
System.out.println(sum);

What is printed?

  • A

    18

    check_circle
  • B

    6

  • C

    9

  • D

    27

Explanation

Inner loop sums 1+2+3 = 6. Outer loop runs 3 times, so total = 6 * 3 = 18.

Want 10 more like this — adaptive to your weak spots?

Related questions