Python for Loops MCQs – Multiple Choice Questions for Practice
Test your understanding of Python for loops with multiple choice questions. These beginner-friendly MCQs cover loop syntax, iteration, range, and nested loops in Python.
Topic: loops-for
📝 Multiple Choice Questions
🟢 Beginner
Q1. What is the correct syntax for a basic for loop in Python?
-
🟢 A. for i in range(10) { print(i) }
-
🔵 B. for (i = 0; i < 10; i++) { print(i) }
-
🟠 C. for i in range(10): print(i)
-
🔴 D. loop i from 0 to 9: print(i)
Answer
for i in range(10): print(i)
Python uses the 'for item in iterable:' syntax with indentation for blocks.
Q2. What is the output of the following code snippet?
for i in range(10):
if i % 2 == 0:
print(i)
-
🟢 A. 0 1 2 3 4 5 6 7 8 9
-
🔵 B. 0 2 4 6 8
-
🟠 C. 2 4 6 8
-
🔴 D. IndentationError: expected an indented block
Answer
0 2 4 6 8
The code prints even numbers between 0-9 (inclusive) since i%2==0 checks for even numbers.
Q3. What is the correct syntax for a for loop in Python?
-
🟢 A. for (int i = 0; i < 10; i++):
-
🔵 B. for i in range(10):
-
🟠 C. for i = 0 to 9:
-
🔴 D. for i in 10:
Answer
for i in range(10):
Python uses 'for item in iterable:' syntax, with range() being the most common way to generate number sequences.
Q4. What will be the output of the following code?
for i in range(5):
print(i * 2)
-
🟢 A. 0 1 2 3 4
-
🔵 B. 2 4 6 8 10
-
🟠 C. 10 8 6 4 2
-
🔴 D. 0 2 4 6 8
Answer
0 2 4 6 8
The loop iterates 0-4 and prints each number multiplied by 2.
Q5. What is the primary purpose of a for loop in Python?
-
🟢 A. To define a function
-
🔵 B. To iterate over a sequence
-
🟠 C. To create a conditional statement
-
🔴 D. To perform mathematical operations
Answer
To iterate over a sequence
For loops are designed to iterate over iterables like lists, strings, or range objects.
Q6. What does the range() function do when used in a for loop?
-
🟢 A. Generates a sequence of numbers
-
🔵 B. Defines a list
-
🟠 C. Calculates the average
-
🔴 D. Determines the length of a string
Answer
Generates a sequence of numbers
range() generates number sequences without creating the full list in memory.
Q7. What does the following code print?
for x in range(5, 8):
print(x)
-
🟢 A. 5 6 7
-
🔵 B. 5 6 7 8
-
🟠 C. 4 5 6 7
-
🔴 D. 5 6 7 8 9
Answer
5 6 7
range(5,8) generates numbers from 5 up to but not including 8.
Q8. What does the range(5) function generate?
-
🟢 A. [0, 1, 2, 3, 4, 5]
-
🔵 B. [1, 2, 3, 4, 5]
-
🟠 C. [0, 1, 2, 3, 4]
-
🔴 D. [5, 4, 3, 2, 1, 0]
Answer
[0, 1, 2, 3, 4]
range(n) generates numbers from 0 to n-1.
Q9. Which of these is NOT a valid iterable for a for loop?
-
🟢 A. [1, 2, 3]
-
🔵 B. (1, 2, 3)
-
🟠 C. 123
-
🔴 D. range(3)
Answer
123
Integers are not iterable in Python.
🟡 Intermediate
Q1. How many times is the print statement executed?
for i in range(3):
for j in range(2):
print(f"i = {i}, j = {j}")
-
🟢 A. 3 times
-
🔵 B. 5 times
-
🟠 C. 6 times
-
🔴 D. 9 times
Answer
6 times
Outer loop runs 3 times, inner loop runs 2 times for each outer iteration (3×2=6).
Q2. What will the program output if the number variable is set to 5?
number = 5
if number < 0:
print("Factorial is not defined for negative numbers.")
result = None
elif number == 0 or number ==1:
result = 1
else:
result = 1
for i in range(2, number + 1):
result *= i
if result is not None:
print("Factorial of", number, "is", result)
-
🟢 A. Factorial is not defined for negative numbers.
-
🔵 B. Factorial of 5 is 5
-
🟠 C. Factorial of 5 is 120
-
🔴 D. Factorial of 5 is 24
Answer
Factorial of 5 is 120
The code calculates 5! (5×4×3×2×1=120) using a for loop.
Q3. How can you create a nested loop in Python?
-
🟢 A. by using a loop inside another loop with proper indentation
-
🔵 B. by using a loop inside another loop with parentheses
-
🟠 C. by using a nested keyword before the inner loop
-
🔴 D. by using a colon after the outer loop and before the inner loop
Answer
by using a loop inside another loop with proper indentation
Python uses indentation to define nested blocks, including loops.
Q4. What does this code accomplish?
total = 0
for i in range(1, 6):
total += i
print(total)
-
🟢 A. It prints numbers from 1 to 5.
-
🔵 B. It calculates the sum of numbers from 1 to 5.
-
🟠 C. It prints the sum of numbers from 1 to 6.
-
🔴 D. It calculates the sum of numbers from 0 to 5.
Answer
It calculates the sum of numbers from 1 to 5.
The loop iterates from 1 to 5 (inclusive) and accumulates the sum in 'total'.
Q5. What is the output of the following code?
for i in range(3):
print(i)
else:
print("Done")
-
🟢 A. 0
1
2
-
🔵 B. 0
1
2
Done
-
🟠 C. Done
-
🔴 D. 3
Done
Answer
0
1
2
Done
The else clause executes after the loop completes normally.
Q6. How many times will 'Hello' be printed?
for i in range(2, 8, 2):
print("Hello")
-
🟢 A. 2
-
🔵 B. 3
-
🟠 C. 4
-
🔴 D. 6
Answer
3
range(2, 8, 2) generates 2, 4, 6 - so 3 iterations.
🔴 Advanced
Q1. What is the output of this nested loop?
result = []
for i in range(2):
for j in range(2):
result.append((i, j))
print(result)
-
🟢 A. [(0, 0), (0, 1)]
-
🔵 B. [(0, 0), (1, 0)]
-
🟠 C. [(0, 0), (0, 1), (1, 0), (1, 1)]
-
🔴 D. [(0, 1), (1, 0)]
Answer
[(0, 0), (0, 1), (1, 0), (1, 1)]
The nested loop generates all combinations of i and j.
Q2. What does this list comprehension equivalent to?
result = [x**2 for x in range(5)]
-
🟢 A. [0, 1, 2, 3, 4]
-
🔵 B. [1, 4, 9, 16, 25]
-
🟠 C. [0, 1, 4, 9, 16]
-
🔴 D. [0, 1, 8, 27, 64]
Answer
[0, 1, 4, 9, 16]
It squares each number from 0 to 4.
🧠 Practice & Progress
Explore More Topics
📘 Learn Python
Tutorials, Roadmaps, Bootcamps & Visualization Projects
Python Fundamentals
Fundamentals more ...
🧠 Python Advanced
More...
🧠 Modules