30 Python Loops MCQ Quiz for Practice

Welcome to Python loops MCQ quiz! Here, I have collected the best 30 multiple-choice questions based on Python loops, which will cover everything from basic to advanced concepts.

Whether you’re a beginner or preparing for Python coding test, these 30 multiple-choice questions will test your understanding of for loops, while loops, nested loops, and loop control statements. So, are you ready to test your knowledge? Let’s begin from the question number 1.👇

1. Which loop is guaranteed to execute at least once in Python?
A. for loop
B. while loop
C. do-while loop
D. None of the above
d
Python does not support do-while loop. Both ‘for’ and ‘while’ loops may not execute if the condition is false initially.
2. What will be the output of the following code?
for i in range(3):
    print(i, end=' ')
A. 0 1 2 3
B. 0 1 2
C. 1 2 3
D. 1 2
b
range(3) generates numbers from 0 to 2, excluding 3.
3. Which of the following statements will create an infinite loop?
A. while True:
B. for i in range(1, 10):
C. while i < 10:
D. for i in range(10):
a
while True: runs indefinitely until a break condition is met.
4. What is the output of the following code snippet if not error?
count = 0
while count < 5:
    if count == 3:
        break
    print(count, end=' ')
    count += 1
A. 0 1 2 3 4
B. 0 1 2
C. 0 1 2 3
D. 0 1 2 3 4 5
b
The loop breaks when count is equal to 3, and it stops before printing 3.
5. How many times will the following loop execute in the below code?
for i in range(2, 15, 3):
    print(i)
A. 3 times
B. 4 times
C. 5 times
D. Infinite times
c
The loop generates numbers 2, 5, 8, 11, and 14, resulting in 5 iterations.
6. Which of the following keyword in Python is used to skip the current iteration?
A. pass
B. break
C. continue
D. skip
c
The continue keyword skips the current iteration and moves to the next iteration.
7. What is the output of the nested loop in the below code?
for i in range(2):
    for j in range(2):
        print(i, j)
A. (0,0) (1,1)
B. (0,1) (1,0)
C. (0,0) (1,0) (0,1) (1,1)
D. (0,0) (0,1) (1,0) (1,1)
d
Both loops iterate over two elements, resulting in 2 * 2 = 4 iterations.
8. What is the output of this code?
for i in range(6):
    if i % 2 == 0:
        continue
    print(i, end=' ')
A. 1 2 4
B. 1 3 5
C. 1 2 3 4 5
D. 1 3
b
Even numbers are skipped, leaving only odd numbers to be displayed.
9. Which expression generates squares of numbers 0 to 4?
A. [i * i for i in range(1, 6)]
B. [i**2 for i in range(1, 5)]
C. [i + i for i in range(0, 5)]
D. [i * i for i in range(5)]
d
This is the list comprehension which generates squares from 0 to 4. The output is [0, 1, 4, 9, 16]
10. Which type of loop is ideal for looping through a dictionary?
A. for loop with range()
B. while loop
C. for loop with items()
D. Nested for loop
c
for key, value in dict.items() is the most Pythonic way to iterate over both keys and values simultaneously.
11. What is the output of the following code?
x = 1
def increment():
    global x
    x += 1

for i in range(3):
    increment()
print(x)
A. 3
B. 4
C. 5
D. None of the above
b
The global variable x is incremented each time the function runs, so it becomes 4.
12. What will be the output if we modify the loop variable inside the loop?
for i in range(5):
    i += 10
    print(i, end=' ')
A. 0 1 2 3 4
B. 10 11 12 13 14
C. 10 11 12 13 14 15
D. 1 2 3 4
b
Modifying the loop variable doesn't affect the iteration.
13. What will be the result of nums after the below code executes?
nums = [1, 2, 3, 4]
for i in nums:
    nums.append(i * 2)
    if len(nums) > 6:
        break
print(nums)
A. [1, 2, 3, 4, 2, 4, 6]
B. [1, 2, 3, 4, 2, 4]
C. [1, 2, 3, 4, 2]
D. Infinite loop
a
The initial length of list is 4. During first iteration (i = 1), nums.append(1 * 2) → Appends 2. New nums: [1, 2, 3, 4, 2]. New length = 5 (not > 6), so no loop break. During second iteration (i = 2), nums.append(2 * 2) → Appends 4. New nums: [1, 2, 3, 4, 2, 4]. Now length = 6 (not > 6), so no loop break. During third iteration (i = 3), nums.append(3 * 2) → Appends 6. New nums: [1, 2, 3, 4, 2, 4, 6]. Length = 7 (which is > 6), so loop breaks and exits. Thus, the final output is [1, 2, 3, 4, 2, 4, 6]. The loop stops after processing the third element (3). The fourth element (4) is never processed because the loop breaks before reaching it. Note that the for loop iterates over the original nums list ([1, 2, 3, 4]), not the modified version.
14. What will the following code be output?
x = [1, 2, 3]
y = [4, 5, 6]
for i in x:
    for j in y:
        if j == 5:
            continue
        print(f"{i}{j}", end=' ')
A. 14 15 16 24 25 26 34 35 36
B. 14 15 24 25 34 35
C. 14 16 24 26 34 36
D. 15 25 35
c
The continue skips printing when j is equal to 5, so all combinations are printed except those ending with 5.
15. What will you get the output after the below code executes?
result = []
for i in range(1, 6):
    if i % 2 == 0:
        result.append(i**2)
    else:
        result.append(i**3)
print(result)
A. [1, 4, 27, 16, 125]
B. [1, 4, 9, 16, 25]
C. [1, 2, 3, 4, 5]
D. [1, 8, 9, 64, 25]
a
Odd numbers are cubed (1, 3, 5 → 1, 27, 125) and even numbers are squared (2, 4 → 4, 16).
16. What is the output of the following code when executed?
for i in range(3):
    for j in range(3):
        if i == j:
            continue
        print(i, j)
A. (0,0) (1,1) (2,2)
B. (0,1) (1,2) (2,0)
C. (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)
D. No output
c
The continue skips cases where i == j, and will display only unequal pairs.
17. What is the output of this code?
for i in range(5):
    if i == 3:
        break
else:
    print("No Break")
A. No Break
B. 0 1 2
C. 0 1 2 3 No Break
D. No output
d
The else block will only be executed if the loop completes normally without a break.
18. How many times does the inner loop run?
for i in range(3):
    for j in range(i):
        print(i, j)
A. 1 time
B. 2 times
C. 3 times
D. 0 times
c
For range(n) in the outer loop, the inner loop runs n(n-1)/2 times. Here, n=3 → 3(2)/2 = 3.
19. What will the following code result?
x = 5
while x > 0:
    x -= 1
    if x == 2:
        continue
    print(x)
A. 4 3 2 1
B. 4 3 2 1 0
C. 4 3 1 0
D. Infinite loop
c
The continue statement skips printing when x == 2.
20. What is the output of this code snippet when exception occurs?
for i in range(3):
    try:
        print(10 // i)
    except ZeroDivisionError:
        print("Division by zero")
A. 10 5 3
B. 10 5 Division by zero
C. 10 5 0
D. Division by zero 10 5
d
Division by zero occurs when i = 0, which is caught by the except block. After that, loop will continue.
21. What happens when this loop runs?
while True:
    print("Infinite")
A. Prints "Infinite" once
B. Prints "Infinite" forever (infinite loop)
C. Throws a SyntaxError
D. None
b
while True: has no exit condition, so it runs indefinitely.
22. What does this output?
for i in range(3):
    if i == 5:
        break
else:
    print("Done")
A. No output
B. Done
C. 0 1 2 Done
D. Error
b
The else block executes only if the loop completes normally without break. Here, break statement does not execute, so else block will execute.
23. What is the risk in this code?
nums = [1, 2, 3]
for x in nums:
    nums.append(x * 2)
A. No risk, works fine.
B. Only appends even numbers.
C. Create an infinite loop.
D. Skips odd numbers.
c
If you modify a list while iterating over it causes infinite growth. Due to which, the loop never reaches the end because nums keeps expanding.
24. What is the value of i after the execution of loop in the below code?
for i in range(5):
    pass
print(i)
A. Error (undefined)
B. 0
C. 4
D. 5
c
In Python, loop variables "leak" into the enclosing scope, meaning that they retain their last value after the loop completes. Therefore, the variable i retains the last value (4) after the execution of loop.
25. What does range(3, 0, -1) produce?
A. [0, 1, 2]
B. [3, 2, 1]
C. [3, 2, 1, 0]
D. Error
b
The range(start, stop, step) function generates numbers from 3 down to 1 (exclusive of 0).
26. What will be displayed output of the below code?
for x in []:
    print("Hello")
else:
    print("World")
A. Hello
B. World
C. Error
D. No output
b
In Python, if you loop over an empty iterable (like an empty list or range with no elements), the loop body never executes because there are no items to iterate over. However, the else clause associated with the loop will always run if no break occurs.
27. How many times will "Python" be printed?

for i in range(1, 10, 2):
for j in range(i):
print("Python")

A. 5
B. 10
C. 15
D. 25
d
The outer loop runs for i = 1, 3, 5, 7, 9. The inner loop runs i times each, so total prints = 1 + 3 + 5 + 7 + 9 = 25.
28. What is the output of this nested loop?
for i in range(3):
    for j in range(3):
        if i == j:
            continue
        print(f"{i}{j}", end=' ')
A. 01 02 10 12 20 21
B. 00 11 22
C. 01 02 10 12 20 21 00 11 22
D. 01 02 10 12 20 21
c
The continue statement skips iterations when i equals j. So, combinations where both digits are the same will not print.
29. What does this code output when executed?
a = [10, 20, 30]
b = a
for x in a:
    b.append(x)
    if len(b) > 5:
        break
print(b)
A. [10, 20, 30, 10]
B. [10, 20, 30, 10, 20]
C. [10, 20, 30, 10, 20, 30]
D. None of the above
c
Before the loop starts, a = [1, 2, 3]. The line b = a means b is a reference to a, so any changes to b also modify a. During first iteration, x = 1 (first element of a). The statement b.append(1) will add 1 in the list. Now b = [1, 2, 3, 1]. len(b) = 4 → No break. During second iteration, x = 2 (second element of a). The statement b.append(2) will add 2 in the list. Now b = [1, 2, 3, 1, 2]. len(b) = 5 → No break. During third iteration, x = 3 (third element of a). The line b.append(3) will add 3 in the list. Now b = [1, 2, 3, 1, 2, 3]. len(b) = 6 → break occurs, loop exits.
30. What is the output of this code when modifying a list during iteration?
numbers = [1, 2, 3, 4]
for num in numbers:
    if num % 2 == 0:
        numbers.remove(num)
print(numbers)
A. [1, 3]
B. [1, 3, 4]
C. [1, 2, 3, 4]
D. [2, 4]
a
Before the loop starts, numbers = [1, 2, 3, 4]. During first iteration, num = 1 (first element of numbers). The condition 1 % 2 != 0 gives False. So, no removal happens. During second iteration, num = 2 (second element of numbers). The condition 2 % 2 == 0 gives True. So, the line numbers.remove(2) removes the number 2 from the list. Now numbers = [1, 3, 4]. Since we modified the list during iteration, the next element is skipped. During third iteration, num = 3 (third element in the original list, but after removal, it becomes the second element). The condition 3 % 2 != 0 gives False. So, no removal occurs. During fourth iteration, num = 4 (fourth element of the original list, but now it has shifted to third position). The condition 4 % 2 == 0 gives True. So, the line numbers.remove(4) will remove the number 4 from the list. Hence, numbers = [1, 3], which is the output.

Quiz Results

0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score
You can do better next time!