While Loops Review#
These questions are to assess your understanding of the while loops.
Question 1
while loops are typically used as definite loops.
See Answer
False
We will typically use while loops for what we call indefinite loops, meaning that when we write a while loop, we will write it in such a way that it runs till a condition is false.
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?
while (x == -1)while (x > -1)while (x != -1)while (x < -1)
See Answer
Option 4 is correct!
The loop when end when x == -1. Try going through the control flow of this for loop header for practice.
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 2 4 8 32 64 128
1 2 4 8 16 32 64 128
1 2 4 16 32 128 200
This will run forever and never stop
See Answer
Option 1 is correct!
We keep multiplying num by 2 until the number is bigger than 200