Python for + if MCQs (25)
Covers filtering inside loops, break/continue, for–else, nested loops, enumerate, list comprehensions with if, and common pitfalls. Answers at the end
1) What is the output?
for i in range(5):
if i % 2 == 0:
print(i, end=" ")
A. 0 2 4
B. 1 3
C. 0 1 2 3 4
D. (no output)
2) Count vowels using for+if:
count = 0
for ch in "python":
if ch in "aeiou":
count += 1
print(count)
A. 1
B. 2
C. 0
D. 3
3) Choose the output.
for i in range(3):
for j in range(3):
if i == j:
print(i, end="")
A. 000
B. 012
C. 222
D. (no output)
4) Filtering into a list.
nums = [1, 2, 3, 4]
out = []
for n in nums:
if n % 2:
out.append(n * 2)
print(out)
A. [2, 6]
B. [2, 4, 6, 8]
C. [1, 3]
D. [4, 8]
5) Mind the break.
for i in range(3):
if i == 1:
break
else:
print(i, end=" ")
A. 0
B. 0 1
C. 1 2
D. (no output)
6) for–else + continue?
for i in range(3):
if i == 1:
continue
print(i, end=" ")
else:
print("|Done")
A. 0 2 |Done
B. 0 1 2 |Done
C. 0 2
D. |Done
7) Adjacent equal pairs.
s = "abba"
pairs = 0
for i in range(len(s)-1):
if s[i] == s[i+1]:
pairs += 1
print(pairs)
A. 0
B. 1
C. 2
D. 3
8) Iterating dictionary by key with a test.
d = {"a":1, "b":2}
for k in d:
if d[k] % 2 == 0:
print(k, end="")
A. a
B. b
C. ab
D. (no output)
9) for–else when a match occurs.
for i in range(1, 6):
if i % 3 == 0:
print("X")
break
else:
print("Y")
A. X
B. Y
C. XY
D. (no output)
10) for–else when no match occurs.
for i in range(1, 6):
if i % 7 == 0:
print("X")
break
else:
print("Y")
A. X
B. Y
C. (no output)
D. XY
11) Using enumerate with a test.
nums = [1, 2, 3]
for i, x in enumerate(nums):
if i == x:
print(i, end="")
A. 0
B. 1
C. 2
D. (no output)
12) Sum positives only.
out = 0
for n in [0, 0, 1, 0]:
if not n:
continue
out += n
print(out)
A. 0
B. 1
C. 2
D. 3
13) Nested loops with skip.
for i in range(2):
for j in range(3):
if j == 1:
continue
print(f"{i}{j}", end=" ")
A. 00 01 02 10 11 12
B. 00 02 10 12
C. 01 02 11 12
D. 00 10
14) Bitwise test inside loop.
res = []
for x in range(5):
if x & 1 and x > 1:
res.append(x)
print(res)
A. [1, 3]
B. [3]
C. [2, 4]
D. []
15) List comprehension with if.
print([x*x for x in range(5) if x % 2 == 0])
A. [0, 4, 16]
B. [1, 9]
C. [0, 1, 4, 9, 16]
D. []
16) Conditional expression in a comp.
print([x if x % 2 == 0 else -x for x in range(3)])
A. [0, 1, 2]
B. [0, -1, 2]
C. [-0, -1, -2]
D. [1, 2, 3]
17) Count evens in triangular iteration.
total = 0
for i in range(3):
for j in range(i):
if j % 2 == 0:
total += 1
print(total)
A. 1
B. 2
C. 3
D. 4
18) Using pass in branch.
for ch in "abc":
if ch == "b":
pass
else:
print(ch, end="")
A. abc
B. ac
C. b
D. (no output)
19) Dash for falsy, number otherwise.
nums = [0, 1, 2, 3]
for n in nums:
if n:
print(n, end="")
else:
print("-", end="")
A. -123
B. 0123
C. —
D. 123-
20) Compare pairs with a condition.
pairs = 0
for a in [1, 2]:
for b in [2, 3]:
if a < b:
pairs += 1
print(pairs)
A. 2
B. 3
C. 4
D. 1
21) Early exit when odd found.
found = None
for n in [2, 4, 5, 6]:
if n % 2 == 1:
found = n
break
else:
found = 0
print(found)
A. 0
B. 5
C. 6
D. None
22) Collect lowercase characters only.
s = "PYthon"
lower = []
for ch in s:
if ch.islower():
lower.append(ch)
print("".join(lower))
A. python
B. thon
C. PY
D. (no output)
23) Non-empty lists with truthy first element.
data = [[], [1], [], [2, 3]]
count = 0
for lst in data:
if lst and lst[0]:
count += 1
print(count)
A. 1
B. 2
C. 3
D. 4
24) Break on zero.
acc = 1
for n in [0, 2, 3]:
if not n:
break
acc *= n
print(acc)
A. 0
B. 1
C. 2
D. 6
25) Nested loop with guarded inner append.
result = []
for i in range(3):
if i % 2 == 0:
for j in range(2):
if j == 1:
break
result.append((i, j))
print(result)
A. [(0, 0), (1, 0), (2, 0)]
B. [(0, 0), (2, 0)]
C. [(0, 1), (2, 1)]
D. []
Answer Key
1) A
2) A
3) B
4) A
5) A
6) A
7) B
8) B
9) A
10) B
11) D
12) B
13) B
14) B
15) A
16) B
17) B
18) B
19) A
20) B
21) B
22) B
23) B
24) B
25) B