While Loops Review

While Loops Review#

These questions are to assess your understanding of the while loops.

Question 1

while loops are typically used as definite loops.


Question 2

Let’s write a while loop header! Say I’m writing a while loop that should loop until some variable x has value -1. Which test ensures my code will loop correctly?

  1. while (x == -1)

  2. while (x > -1)

  3. while (x != -1)

  4. while (x < -1)


Question 3

What is the output of the following program:

int num = 1; // initialization
while (num <= 200) { // test
    System.out.print(num + " "); // body
    num = num * 2; // update
}
  1. 1 2 4 8 32 64 128
    
  2. 1
    2
    4
    8
    16
    32
    64
    128
    
  3. 1 2 4 16 32 128 200
    
  4. This will run forever and never stop