for Loops

AP Computer Science A· difficulty 2/5

int n = 1;
while (n < 100) {
    n *= 2;
}

Which for-loop produces the same final value of n?

  • A

    for (int n = 0; n < 100; n *= 2) {}

  • B

    for (int n = 1; n < 100; n *= 2) {}

    check_circle
  • C

    for (int n = 1; n < 100; n++) {}

  • D

    for (int n = 1; n <= 100; n /= 2) {}

Explanation

The update n *= 2 in the for-loop header reproduces the doubling step until n is no longer less than 100.

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

Related questions