Python While and While MCQs (25)
Python Training in Vizag (Softenant)1) Output?
i = 0
while i < 2:
j = 0
while j < 3:
print(f"{i}{j}", end=" ")
j += 1
i += 1
A. 00 01 02 10 11 12
B. 00 10 01 11 02 12
C. 00 01 10 11 02 12
D. (no output)
2) Sum produced?
total = 0
i = 0
while i < 3:
j = 0
while j < 2:
total += i * j
j += 1
i += 1
print(total)
A. 2
B. 3
C. 4
D. 6
3) Break in inner loop.
i = 0
while i < 3:
j = 0
while j < 3:
if j == 1:
break
print(f"{i}{j}", end=" ")
j += 1
i += 1
A. 00 01 10 11 20 21
B. 00 10 20
C. 01 11 21
D. (no output)
4) Continue on outer selection.
i = 0
while i < 4:
if i % 2 == 1:
i += 1
continue
j = 0
while j < 2:
print(f"{i}{j}", end=" ")
j += 1
i += 1
A. 00 01 10 11 20 21 30 31
B. 00 01 20 21
C. 10 11 30 31
D. (no output)
5) Outer while–else with inner break (outer not broken).
hit = False
i = 0
while i < 2:
j = 0
while j < 2:
if i == 1 and j == 1:
hit = True
break
j += 1
i += 1
else:
print("A")
if hit:
print("B")
A. A then B (two lines)
B. only A
C. only B
D. nothing
6) Triangular loop count?
cnt = 0
i = 0
while i < 4: # 0..3
j = 0
while j < i: # 0..i-1
cnt += 1
j += 1
i += 1
print(cnt)
A. 3
B. 4
C. 6
D. 8
7) Nested enumerate-like indexes built manually.
pairs = []
i = 0
s1, s2 = "ab", "XY"
while i < len(s1):
j = 0
while j < len(s2):
if i == j:
pairs.append(s1[i] + s2[j])
j += 1
i += 1
print(pairs)
A. [‘aX’, ‘bY’]
B. [‘aY’, ‘bX’]
C. [‘aX’,’aY’,’bX’,’bY’]
D. []
8) Double list via nested while.
out = []
i = 0
while i < 3:
j = 0
while j < 2:
out.append(i*j)
j += 1
i += 1
print(out)
A. [0, 1, 0, 2, 0, 3]
B. [0, 0, 0, 1, 2, 3]
C. [0, 0, 0, 1, 2, 2]
D. [0, 0, 0, 1, 2, 3] (order varies)
9) Count (i,j) with i<j for 0..2.
c = 0
i = 0
while i < 3:
j = 0
while j < 3:
if i < j:
c += 1
j += 1
i += 1
print(c)
A. 3
B. 6
C. 9
D. 0
10) Inner break vs outer else.
i = 0
while i < 2:
j = 0
while j < 2:
if j == 0: break
j += 1
i += 1
else:
print("OK")
A. Prints OK
B. Prints nothing
C. Error
D. Prints OK twice
11) Product (cartesian) size.
a, b = [1,2,3], ['x','y']
i = 0; cnt = 0
while i < len(a):
j = 0
while j < len(b):
cnt += 1
j += 1
i += 1
print(cnt)
A. 2
B. 3
C. 5
D. 6
12) With paired ranges using counters.
res = []
i = 0
while i < 3: # i = 0,1,2
j_val = i + 1 # pairs (0,1),(1,2),(2,3)
k = 0
while k < i:
res.append(j_val)
k += 1
i += 1
print(sum(res))
A. 0
B. 4
C. 5
D. 6
13) Nested dict build with tuple keys.
d = {}
i = 0
while i < 2:
j = 0
while j < 2:
d[(i,j)] = i + j
j += 1
i += 1
print(d[(1,1)])
A. 0
B. 1
C. 2
D. KeyError
14) Skip inner when outer odd.
out = []
i = 0
while i < 4:
j = 0
while j < 2:
if i % 2:
break
out.append((i, j))
j += 1
i += 1
print(len(out))
A. 8
B. 4
C. 2
D. 6
15) Order of pair strings.
pairs = []
i = 0
while i < 2:
j = 0
while j < 2:
pairs.append(f"{i}{j}")
j += 1
i += 1
print(pairs)
A. [’00’,’01’,’10’,’11’]
B. [’00’,’10’,’01’,’11’]
C. [’00’,’11’,’01’,’10’]
D. []
16) Inner continue count.
cnt = 0
i = 0
while i < 3:
j = 0
while j < 3:
if j % 2 == 0:
j += 1
continue
cnt += 1
j += 1
i += 1
print(cnt)
A. 3
B. 4
C. 6
D. 9
17) Multiplication table (diagonal sum 1..3).
res = 0
i = 1
while i <= 3:
j = 1
while j <= 3:
if i == j:
res += i*j
j += 1
i += 1
print(res)
A. 6
B. 14
C. 30
D. 1
18) Flatten a matrix.
m = [[1,2],[3,4],[5]]
flat = []
i = 0
while i < len(m):
j = 0
while j < len(m[i]):
flat.append(m[i][j])
j += 1
i += 1
print(flat[-1])
A. 4
B. 5
C. [5]
D. IndexError
19) Unique pairs set size for i<j (0..2).
pairs = set()
i = 0
while i < 3:
j = 0
while j < 3:
if i < j:
pairs.add((i,j))
j += 1
i += 1
print(len(pairs))
A. 3
B. 6
C. 9
D. 0
20) String × range with a special case.
count = 0
i = 0
while i < 2: # 'a','b'
ch = "ab"[i]
j = 0
while j < 3:
if ch == "a" and j == 1:
count += 2
else:
count += 1
j += 1
i += 1
print(count)
A. 5
B. 6
C. 7
D. 8
21) Early outer break via flag.
stop = False
i = 0
while i < 3:
j = 0
while j < 3:
if i*j == 2:
stop = True
break
j += 1
if stop: break
i += 1
print(i, j)
A. 2 1
B. 1 2
C. 2 2
D. 1 1
22) Set of sums i+j (0..2).
s = set()
i = 0
while i < 3:
j = 0
while j < 3:
s.add(i + j)
j += 1
i += 1
print(len(s))
A. 5
B. 6
C. 7
D. 9
23) Sum values nested over dict lists.
d = {"x": [1,2], "y": [3]}
keys = list(d)
i = 0; s = 0
while i < len(keys):
vs = d[keys[i]]
j = 0
while j < len(vs):
s += vs[j]
j += 1
i += 1
print(s)
A. 3
B. 6
C. 7
D. 1
24) Sum of (i-j) where (i+j) even.
total = 0
i = 0
while i < 3:
j = 0
while j < 3:
if (i + j) % 2 == 0:
total += (i - j)
j += 1
i += 1
print(total)
A. 0
B. 1
C. -1
D. 3
25) Nested range bounds via counters.
c = 0
i = 1
while i <= 3: # 1..3
j = i
while j <= 3: # i..3
c += 1
j += 1
i += 1
print(c)
A. 3
B. 4
C. 6
D. 7
Answer Key
1) A
2) C
3) B
4) B
5) C
6) C
7) A
8) B
9) A
10) B
11) D
12) B
13) C
14) B
15) A
16) A
17) B
18) B
19) A
20) C
21) B
22) A
23) B
24) A
25) C