Python while + if MCQs (25)
Covers filters, break/continue, while–else, nested loops, sentinels, flags, and common pitfalls. Answers at the end
Python Training in Vizag (Softenant)1) What is the output?
i = 0
while i < 5:
if i % 2 == 0:
print(i, end=" ")
i += 1
A. 0 2 4
B. 1 3
C. 0 1 2 3 4
D. (no output)
2) Count vowels using while+if:
s = "python"
i = 0; c = 0
while i < len(s):
if s[i] in "aeiou":
c += 1
i += 1
print(c)
A. 1
B. 2
C. 0
D. 3
3) Break stops at 3.
n = 0
while n < 5:
if n == 3:
break
print(n, end=" ")
n += 1
A. 0 1 2
B. 0 1 2 3
C. 3 4
D. (no output)
4) Continue skips odds.
n = -1
while n < 3:
n += 1
if n % 2 == 1:
continue
print(n, end=" ")
A. 0 2
B. 1 3
C. 0 1 2 3
D. (no output)
5) while–else runs if no break.
n = 0
while n < 3:
n += 1
else:
print("E")
A. E
B. (no output)
C. E E E
D. Error
6) No match ⇒ else executes.
nums = [1, 2, 3]
i = 0
while i < len(nums):
if nums[i] == 4:
print("F")
break
i += 1
else:
print("N")
A. F
B. N
C. FN
D. (no output)
7) Sentinel sum (stop at 0).
vals = [3, 2, 0, 5]
i = 0; s = 0
while i < len(vals):
x = vals[i]; i += 1
if x == 0: break
s += x
print(s)
A. 3
B. 5
C. 10
D. 0
8) Nested while with guarded continue.
i = 0; c = 0
while i < 2:
j = 0
while j < 3:
if j == 1:
j += 1
continue
c += 1
j += 1
i += 1
print(c)
A. 2
B. 3
C. 4
D. 6
9) Off-by-one check.
i = 1
while i <= 3:
print(i, end="")
i += 1
A. 12
B. 123
C. 1234
D. (no output)
10) Decrement with skip.
n = 3
while n > 0:
n -= 1
if n == 1:
continue
print(n, end=" ")
A. 3 2 1
B. 2 0
C. 1
D. (no output)
11) Break only exits inner loop.
i = 0; c = 0
while i < 3:
j = 0
while j < 3:
if j == 0:
break
c += 1
j += 1
i += 1
print(c)
A. 0
B. 3
C. 6
D. 9
12) Count using while True + break.
s = "aba"
i = 0; c = 0
while True:
if i == len(s):
break
if s[i] == 'a':
c += 1
i += 1
print(c)
A. 1
B. 2
C. 3
D. 0
13) Mutating list while consuming.
lst = [1, 2, 3, 4]
s = 0
while lst:
x = lst.pop(0)
if x % 2 == 0:
s += x
print(s)
A. 4
B. 6
C. 10
D. 0
14) continue doesn’t cancel else.
n = 0
while n < 3:
n += 1
if n == 2:
continue
else:
print("E")
A. E
B. (no output)
C. E E
D. Error
15) Guarded infinite loop.
i = 0
while i < 3:
if i < 2:
i += 1
print("X", end="")
else:
break
A. X
B. XX
C. XXX
D. (no output)
16) Flag pattern.
i = 0; found = False
while i < 5:
if i*i == 9:
found = True
break
i += 1
print(found)
A. False
B. True
C. None
D. 9
17) Skip vowels while building string.
s = "team"
i = 0; out = ""
while i < len(s):
ch = s[i]
if ch in "aeiou":
i += 1
continue
out += ch
i += 1
print(out)
A. tm
B. team
C. ea
D. (empty)
18) Double-step on evens.
i = 0; c = 0
while i < 5:
if i % 2 == 0:
i += 2
c += 1
continue
i += 1
print(c)
A. 2
B. 3
C. 4
D. 5
19) Collect odd numbers descending.
n = 3; out = []
while n:
if n % 2:
out.append(n)
n -= 1
print(out)
A. [1, 3]
B. [3, 1]
C. [2]
D. []
20) Safe dict walk using snapshot of keys.
d = {"a":1, "b":2}
keys = list(d); i = 0
while i < len(keys):
k = keys[i]
if d[k] % 2 == 0:
d[k] += 1
i += 1
print(d["b"])
A. 2
B. 3
C. KeyError
D. 1
21) Parse leading digits.
s = "12a34"
i = 0; num = 0
while i < len(s) and s[i].isdigit():
num = num*10 + int(s[i])
i += 1
print(num)
A. 0
B. 12
C. 34
D. Error
22) Mixed branches accumulation.
n = 0; s = 0
while n < 4:
if n % 2 == 0:
s += n
elif n % 3 == 0:
s += 10
else:
s += 1
n += 1
print(s)
A. 3
B. 10
C. 13
D. 14
23) Skip falsy values.
q = [1, 0, 2]
i = 0; cnt = 0
while True:
if i == len(q): break
if not q[i]:
i += 1
continue
cnt += q[i]
i += 1
print(cnt)
A. 0
B. 1
C. 2
D. 3
24) Merge step (no tail append shown).
l1, l2 = [1, 3], [2, 4]
i = j = 0; out = []
while i < len(l1) and j < len(l2):
if l1[i] < l2[j]:
out.append(l1[i]); i += 1
else:
out.append(l2[j]); j += 1
print(out[2])
A. 1
B. 2
C. 3
D. 4
25) break suppresses else.
n = 0
while n < 3:
if n == 2:
break
n += 1
else:
print("E")
print(n)
A. E then 3
B. 2
C. E then 2
D. 3
Answer Key
1) A
2) A
3) A
4) A
5) A
6) B
7) B
8) C
9) B
10) B
11) A
12) B
13) B
14) A
15) B
16) B
17) A
18) B
19) B
20) B
21) B
22) C
23) D
24) C
25) B