for Loops

AP Computer Science A· difficulty 3/5

int n = 4;
int result = 1;
int i = 1;
while (i <= n) {
  result *= 2;
  i++;
}
System.out.println(result);

What is printed?

  • A

    16

    check_circle
  • B

    4

  • C

    8

  • D

    32

Explanation

The loop multiplies result by 2 four times: 1 -> 2 -> 4 -> 8 -> 16. Equivalent to 2^4 = 16.

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

Related questions