Nested Iteration

AP Computer Science A· difficulty 4/5

public static int run() {
    int p = 1;
    for (int i = 1; i <= 3; i++) {
        for (int j = 1; j <= i; j++) {
            p *= 2;
        }
    }
    return p;
}
// Call: System.out.println(run());

What is printed?

  • A

    32

  • B

    16

  • C

    64

    check_circle
  • D

    128

Explanation

Total iterations = 1+2+3 = 6. p = 2^6 = 64.

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

Related questions