Python While Loop MCQ Questions and Answers

Python's while loop is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. Here we present 12 multiple-choice questions to test your knowledge of Python While Loop. Each MCQ has the correct answer with an explanation. 

1. What is the basic structure of a while loop in Python?

a) while condition: statement
b) while (condition) {statement}
c) while condition do: statement
d) while condition [statement]

Answer:

a) while condition: statement

Explanation:

In Python, a while loop is written as 'while' followed by a condition, a colon, and then the statement(s) to execute.

2. How do you ensure a while loop runs at least once regardless of the condition?

a) Using a do-while loop
b) By setting the condition to True initially
c) It's not possible in Python
d) By writing the loop body before the condition

Answer:

c) It's not possible in Python

Explanation:

Python doesn't have a do-while loop construct that guarantees at least one execution of the loop body.

3. What does the 'break' statement do in a while loop?

a) Pauses the loop
b) Stops the loop and exits it
c) Skips the current iteration
d) Breaks the condition

Answer:

b) Stops the loop and exits it

Explanation:

The 'break' statement is used to exit a while loop immediately, regardless of the condition.

4. What is the purpose of the 'continue' statement in a while loop?

a) To continue the loop indefinitely
b) To skip the current iteration and continue with the next
c) To pause and wait for user input
d) To reset the loop condition

Answer:

b) To skip the current iteration and continue with the next

Explanation:

The 'continue' statement skips the remaining code in the current iteration and proceeds to the next iteration of the loop.

5. How can you use an else statement with a while loop?

a) To execute a block of code after the while loop condition becomes False
b) To handle exceptions in the while loop
c) To create an alternative loop if the while loop condition is False
d) To execute a block of code when the while loop condition is True

Answer:

a) To execute a block of code after the while loop condition becomes False

Explanation:

In Python, the else block after a while loop is executed when the loop condition becomes False.

6. What is the output of the following code?

x = 5
while x > 3:
print(x)
x -= 1
a) 5 4 3
b) 5 4
c) 5 4 3 2
d) Infinite loop

Answer:

b) 5 4

Explanation:

The loop decrements x and prints its value as long as x is greater than 3.

7. How do you create an infinite loop?

a) while True:
b) while False:
c) while 1 == 1:
d) Both a and c

Answer:

d) Both a and c

Explanation:

An infinite loop can be created by using 'while True:' or any condition that always evaluates to True.

8. What is the role of the 'else' clause in a while loop?

a) To execute code when the loop condition is initially False
b) To add another condition to the loop
c) To execute code when the loop exits without encountering a break
d) To catch exceptions in the loop

Answer:

c) To execute code when the loop exits without encountering a break

Explanation:

The else clause in a while loop is executed when the loop terminates naturally, without hitting a break statement.

9. What happens if 'continue' is executed in a while loop?

a) The loop terminates immediately
b) The current iteration stops, and the loop restarts from the next iteration
c) The loop skips to the else block
d) The loop condition is re-evaluated

Answer:

b) The current iteration stops, and the loop restarts from the next iteration

Explanation:

The 'continue' statement causes the loop to immediately start the next iteration.

10. What does the following code do?

x = 0
while x < 5:
x += 1
if x == 3:
break
else:
print("Loop completed")
a) Prints "Loop completed"
b) Exits the loop when x equals 3
c) Results in an infinite loop
d) Prints "Loop completed" after exiting the loop

Answer:

b) Exits the loop when x equals 3

Explanation:

The loop is terminated by the break statement when x equals 3, so the else block is not executed.

11. How can a while loop be terminated prematurely?

a) Using the 'exit' statement
b) Using the 'break' statement
c) Setting the loop condition to False
d) Using the 'stop' statement

Answer:

b) Using the 'break' statement

Explanation:

The 'break' statement is used to exit a while loop before the condition becomes False.

12. What is the output of the following code?

count = 0
while count < 5:
count += 1
if count == 3:
continue
print(count)
a) 1 2 4 5
b) 1 2 3 4 5
c) 1 2
d) 1 2 3 4

Answer:

a) 1 2 4 5

Explanation:

The 'continue' statement skips the print statement when count is 3, so 3 is not printed.


Comments