Question 1
What is the basic syntax of a while loop in Python?
while (condition):
for condition:
while loop condition do:
loop while condition:
Question 2
How does a while loop differ from a for loop?
While loops can iterate over a sequence, for loops cannot.
While loops always have an exit condition, for loops do not.
While loops have entry-controlled flow, for loops have exit-controlled flow.
There is no difference between them.
Question 3
How many times will a while loop with the condition True run?
Zero times
Infinite times
Once
Until it encounters an error
Question 4
How can you avoid an infinite loop?
By using a for loop instead.
By setting the loop condition to True.
By providing a valid exit condition.
Infinite loops cannot be avoided.
Question 5
Which of the following statements is used to increment a variable within a while loop?
inc()
++
+=
increment
Question 6
How can you ensure a loop executes at least once?
Use a for loop.
Use a while loop with a break statement.
Set the loop condition to True.
Use a continue statement.
Question 7
What is the purpose of the locals() function in a while loop?
To access local variables of the loop.
To define new local variables.
To exit the loop.
It is not a valid function in a while loop.
Question 8
What is the output of the following Python code?
count = 0
while count < 3:
print("Hello")
count += 1
else:
print("Else block")
Prints "Hello" three times and then prints "Else block."
Prints "Hello" four times.
Prints "Else block" three times.
Raises a .SyntaxError
Question 9
What does the following Python code do?
num = 10
while num > 0:
if num % 2 == 0:
print(num, end=" ")
num -= 1
Prints even numbers in reverse order from 10 to 1.
Prints odd numbers in the range [1, 10].
Prints even numbers in the range [1, 10].
Raises a SyntaxError.
Question 10
What does the following Python code do?
x = 1
while x < 6:
print(x, end=" ")
x += 1
if x == 4:
continue
Prints numbers in the range [1, 6] with a skip for 4.
Prints numbers in the range [1, 6] without a skip.
Raises a SyntaxError.
Enters into an infinite loop.
There are 20 questions to complete.