while Loops

AP Computer Science A· difficulty 2/5

int n = 1;
while (n < 16) {
  n *= 2;
}
System.out.println(n);

What is printed?

  • A

    16

    check_circle
  • B

    8

  • C

    32

  • D

    15

Explanation

n doubles each time: 1, 2, 4, 8, 16. When n becomes 16 the condition n < 16 is false and the loop exits. n is 16.

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

Related questions