Nested Iteration

AP Computer Science A· difficulty 3/5

public static int count() {
    int c = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
            if (i + j == 3) c++;
        }
    }
    return c;
}
// Call: System.out.println(count());

What is printed?

  • A

    4

    check_circle
  • B

    3

  • C

    6

  • D

    5

Explanation

Pairs (i,j) summing to 3 within 0..3: (0,3),(1,2),(2,1),(3,0). That is 4 pairs.

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

Related questions