Python While Loops: MCQs for Practice
Sharpen your Python while loop skills with multiple-choice questions covering beginner, intermediate, and advanced concepts. Practice loop syntax, iteration, break, continue, and the else clause.
Topic: loops-while
๐ Multiple Choice Questions
๐ข Beginner
Q1. What is the correct syntax for a basic while loop in Python?
-
๐ข A. while (condition) { code }
-
๐ต B. while condition: code
-
๐ C. while condition { code }
-
๐ด D. while condition then code
Answer
while condition: code
Python uses the 'while condition:' syntax with indentation for blocks, not curly braces.
Q2. What is the correct syntax for a basic while loop in Python?
-
๐ข A. while (condition) { code }
-
๐ต B. while condition: code
-
๐ C. while condition { code }
-
๐ด D. while condition then code
Answer
while condition: code
Python uses the 'while condition:' syntax with indentation for blocks, not curly braces.
Q3. What is the output of the following code snippet?
i = 1
while i < 10:
print(i)
i += 2
-
๐ข A. 1 2 3 4 5 6 7 8 9
-
๐ต B. 1 3 5 7 9
-
๐ C. 0
-
๐ด D. IndentationError: expected an indented block
Answer
1 3 5 7 9
The loop starts at 1 and increments by 2 each iteration, printing odd numbers until i reaches 9.
Q4. What is the output of the following code snippet?
i = 0
while i < 5:
print(i)
i += 1
else:
print("Done")
-
๐ข A. 0 1 2 3 4 Done
-
๐ต B. 0 1 2 3 Done
-
๐ C. SyntaxError: invalid syntax
-
๐ด D. IndentationError: expected an indented block
Answer
0 1 2 3 4 Done
The loop prints 0-4, then the else clause executes after normal loop completion.
Q5. What is the output of the following code?
count = 0
while count < 3:
print(count)
count += 1
-
๐ข A. 0 1 2
-
๐ต B. 0 1
-
๐ C. 1 2 3
-
๐ด D. The code will run indefinitely.
Answer
0 1 2
The loop prints count values from 0 up to (but not including) 3.
Q6. What is the output of this while loop?
x = 3
while x > 0:
print(x)
x -= 1
-
๐ข A. 3 2 1
-
๐ต B. 3 2 1 0
-
๐ C. 2 1 0
-
๐ด D. Infinite loop
Answer
3 2 1
The loop prints x then decrements it, stopping when x is no longer greater than 0.
Q7. What will happen if the condition in a while loop starts as False?
-
๐ข A. The loop will execute once
-
๐ต B. The loop will never execute
-
๐ C. It will cause a syntax error
-
๐ด D. The loop will run indefinitely
Answer
The loop will never execute
If the condition is False initially, the loop body is skipped entirely.
๐ก Intermediate
Q1. What is the output of the following code?
x = 10
while x > 0:
print(x)
x -= 2
-
๐ข A. 9 7 5 3 1
-
๐ต B. 10 8 6 4 2
-
๐ C. 10 9 8 7 6
-
๐ด D. The code will run indefinitely.
Answer
10 8 6 4 2
The loop starts at 10 and decrements by 2 each iteration, stopping when x reaches 0.
Q2. How can you prevent an infinite loop?
-
๐ข A. By ensuring the loop condition eventually becomes False
-
๐ต B. By using a for loop instead
-
๐ C. By adding a sleep statement
-
๐ด D. All of the above
Answer
By ensuring the loop condition eventually becomes False
The essential way to prevent infinite loops is to make sure the condition will eventually evaluate to False.
Q3. What is the purpose of the 'else' clause in a while loop?
-
๐ข A. It executes if the loop condition is False
-
๐ต B. It executes if the loop completes normally (without break)
-
๐ C. It executes after every iteration
-
๐ด D. Python doesn't support else with while loops
Answer
It executes if the loop completes normally (without break)
The else clause executes after the loop finishes normally, but not if break was called.
๐ด Advanced
Q1. Which scenario is best suited for a while loop rather than a for loop?
-
๐ข A. When you know the exact number of iterations needed
-
๐ต B. When you need to iterate until a certain condition is met
-
๐ C. When you need to iterate through a list
-
๐ด D. When you need the fastest possible loop
Answer
When you need to iterate until a certain condition is met
While loops are ideal when the number of iterations isn't known beforehand but depends on a condition.
๐ง Practice & Progress
Explore More Topics
๐ Learn Python
Tutorials, Roadmaps, Bootcamps & Visualization Projects
Python Fundamentals
Fundamentals more ...
๐ง Python Advanced
More...
๐ง Modules