Python While Loop MCQs (25)
Python Training in Vizag (Softenant)1) What is the output?
i = 0
while i < 3:
print(i, end=" ")
i += 1
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. (no output)
2) Output?
n = 3
while n:
print(n, end=" ")
n -= 1
A. 3 2 1
B. 2 1 0
C. 3 2 1 0
D. 1 2 3
3) Choose the output.
i = 0
while i < 5:
i += 2
print(i)
A. 4
B. 5
C. 6
D. Error
4) What prints?
i = 0
while i < 4:
if i == 2:
break
print(i, end=" ")
i += 1
A. 0 1
B. 0 1 2
C. 2 3
D. (no output)
5) Output?
i = 0
while i < 4:
i += 1
if i == 2:
continue
print(i, end=" ")
A. 1 3 4
B. 2 3 4
C. 1 2 3
D. 2 4
6) Choose the output.
total = 0
i = 1
while i <= 4:
total += i
i += 1
print(total)
A. 6
B. 10
C. 4
D. 3
7) Output?
i = 0
while i < 3:
print("A", end=" ")
i += 1
else:
print("B")
A. A A A
B. A A A B
C. B only
D. Error
8) While–else behavior?
i = 0
while i < 3:
if i == 1:
break
i += 1
else:
print("Done")
A. Prints “Done”
B. Prints nothing
C. Prints Done then i
D. Error
9) Output?
s = "py"
i = 0
while i < len(s):
print(s[i], end="")
i += 1
A. py
B. p⏎y
C. yp
D. Error
10) Choose the output.
i = 5
while i > 5:
print(i)
i -= 1
print("X")
A. 5 4 3 2 1 X
B. X
C. (no output)
D. 4 3 2 1 X
11) Output?
i = 0
while True:
if i == 3:
break
i += 1
print(i)
A. 2
B. 3
C. 4
D. Infinite loop
12) Choose the output.
i = 0
out = ""
while i < 3:
out += str(i)
i += 1
print(out)
A. 012
B. 123
C. 01
D. 23
13) What prints?
i = 0
while i < 5:
i += 2
print(i, end=" ")
A. 1 3 5
B. 2 4 6
C. 2 4 5
D. 0 2 4
14) Output?
n = 7
cnt = 0
while n > 0:
n //= 2
cnt += 1
print(cnt)
A. 2
B. 3
C. 4
D. 7
15) Choose the output.
i = 0
while i < 3:
j = 0
while j < i:
j += 1
print(j, end=" ")
i += 1
A. 0 1 2
B. 0 0 0
C. 1 1 1
D. 0 1 1
16) Output?
i = 0
while i < 5:
if i % 2 == 0:
i += 1
continue
print(i, end=" ")
i += 1
A. 0 2 4
B. 1 3
C. 1 2 3 4
D. 2 4
17) What prints?
i = 1
while i < 10:
i *= 2
print(i)
A. 8
B. 10
C. 12
D. 16
18) Choose the output.
i = 3
while i:
i -= 1
else:
print("Z")
A. (no output)
B. Z
C. 3 2 1 Z
D. Infinite loop
19) Output?
i = 0
s = 0
while i < 5:
s += i
i += 1
print(s)
A. 10
B. 15
C. 5
D. 9
20) Choose the output.
i = 0
while i < 3:
print(i, end=" ")
i = i + (i == 1) + 1
Note: (i == 1) evaluates to True/False → 1/0.
A. 0 1 2
B. 0 1
C. 0 2
D. 1 2
21) Output?
i = 0
while i < 3:
print("X", end="")
if i == 2:
break
i += 1
else:
print("Y")
A. XXXY
B. XXX
C. XXY
D. XY
22) Choose the output.
i = -1
while i > -5:
print(i, end=" ")
i -= 2
A. -1 -2 -3 -4
B. -1 -3 -5
C. -1 -3
D. -1 -3 -5 -7
23) Output?
txt = "abba"
i = 0
cnt = 0
while i < len(txt) - 1:
if txt[i] == txt[i+1]:
cnt += 1
i += 1
print(cnt)
A. 0
B. 1
C. 2
D. 3
24) Choose the output.
i = 0
j = 0
while i < 3:
while j < 2:
break
print(i, j, end="; ")
i += 1
Note: inner break exits only the inner loop.
A. 0 0; 1 0; 2 0;
B. 0 0; 1 0; 2 0; 3 0;
C. 0 2; 1 2; 2 2;
D. Infinite loop
25) What prints?
i = 0
while i < 3:
if i == 1:
i += 1
continue
print(i, end=" ")
i += 1
else:
print("| Done")
A. 0 1 2 | Done
B. 0 2 | Done
C. 0 1 2
D. 0 2
Answer Key
1) A
2) A
3) C
4) A
5) A
6) B
7) B
8) B
9) A
10) B
11) B
12) A
13) B
14) B
15) A
16) B
17) D
18) B
19) A
20) C
21) B
22) C
23) C
24) A
25) B