Nested Iteration

AP Computer Science A· difficulty 4/5

for (int i = 1; i <= 2; i++) {
  for (int j = 1; j <= 3; j++) {
    System.out.print(i * j + " ");
  }
  System.out.println();
}

What is printed?

  • A

    1 2 3 \n2 4 6

    check_circle
  • B

    1 1 1 \n2 2 2

  • C

    1 2 3 \n4 5 6

  • D

    2 4 6 \n3 6 9

Explanation

For i=1: prints 1<em>1, 1</em>2, 1*3 = "1 2 3". For i=2: "2 4 6". Each row ends with println.

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

Related questions