While Loop MCQs in Python: 10 Solved Questions with Explanations

Test your Python while-loop tracing with 10 solved MCQs. Each answer explains the condition, state change and stopping point without skipping iterations.

KnowledgeGate Team

Exam prep & CS education

Updated 25 Nov 20268 min read
While Loop MCQs in Python: 10 Solved Questions with Explanations

While-loop questions are rarely about remembering the keyword. They test whether you can evaluate truthiness, update state in the right order, recognise termination and trace a value without skipping an iteration. Choose an option, write the final state, then compare your trace with the state changes below.

For syntax, range(), break, continue and loop else, use Loops in Python: for, while, range(), break and continue. While-loop state tracing needs a different drill: solve these ten MCQs to a final value before checking the explanation. Widen the drill with broader Python output-based questions. Open the full solution pages for Questions 2, 6 and 7, then practise the remaining questions in the While Loop PYQ Questions hub.

While loops in Python: the four checks to make before solving

For every loop, note the initial state, evaluate the Boolean condition, execute the body, then find the update or termination point. In Python, 0 is false and a non-zero integer is true. A while ... else block runs when the condition becomes false normally, but it is skipped when a break ends the loop.

For a quick calibration, start with n = 37, total = 0 and count = 0. Each pass takes digit = n % 10, adds it to total, increments count, then replaces n with n // 10.

check

condition

body result

next state

1

37 > 0 is true

(n, digit, total, count) = (37, 7, 7, 1)

n = 3

2

3 > 0 is true

(n, digit, total, count) = (3, 3, 10, 2)

n = 0

3

0 > 0 is false

Body does not run

total = 10, count = 2

The Python Course: Concepts, MCQs & Coding provides sequenced practice after this tracing method.

While Loop MCQs 1-2: loop roles and truthy conditions

Question 1

Complete the analogy:

Condition : while :: Iteration : ?

  • A. If

  • B. For

  • C. Auto

  • D. Do

Correct answer: B. For.

Read this as an analogy. A while loop repeats while a condition stays true; a for loop iterates over a sequence or range. if makes a one-time selection, auto is not a Python loop keyword, and Python has no do ... while statement. Use while when changing state controls repetition and for for a known iterable.

Question 2

Which of the following will not run the loop statement an infinite number of times in Python?

  • A.

python
  while 21 % 2:
  print("ALWAYS")
  • B.

python
  while 21 // 2:
  print("ALWAYS")
  • C.

python
  while True:
  print("ALWAYS")
  • D.

python
  while 1 % 1:
  print("ALWAYS")

Correct answer: D. while 1 % 1:.

Evaluate each condition first. 21 % 2 is 1, 21 // 2 is 10, and True is true. Options A, B and C repeat indefinitely because their conditions never change. But 1 % 1 is 0, so D executes zero times. Open the full solution for this question.

While Loop MCQs 3-4: trace order and the else block

Question 3

What will be the output?

i = 1

while i < 4:

print(i, end=" ")

i += 1

  • A. 1 2 3

  • B. 0 1 2 3

  • C. 1 2 3 4

  • D. Infinite loop

Correct answer: A. 1 2 3.

The checks occur before the prints. Starting at i = 1, three passes print 1, 2 and 3, with an increment after each print. Then i = 4, so 4 < 4 is false and 4 is never printed. The update prevents an infinite loop; end=" " keeps the values on one line.

Question 4

What is the output of the following?

i = 0

while i < 3:

print(i)

i += 1

else:

print(0)

  • A. 0 1 2 3 0

  • B. 0 1 2 0

  • C. 0 1 2

  • D. Error

Correct answer: B. 0 1 2 0.

The body prints 0, 1 and 2. The third increment makes i = 3, so the condition becomes false. Because no break occurs, the else suite prints the final 0. The actual print() calls put the values on separate lines, though the option uses spaces. A break would skip else.

While Loop MCQs 5-6: two ways to compute a GCD

State trace for the remainder GCD method: (10,6) to (2,0), then return 2.

Question 5

What does this pseudocode return for a = 10 and b = 6?

Integer func(Integer a, Integer b)

Integer temp

while (b)

temp = a MOD b

a = b

b = temp

end while

return a

End function func()

  • A. 2

  • B. 4

  • C. 3

  • D. 1

Correct answer: A. 2.

Treat a = 10 and b = 6 as the inputs. (10, 6) gives remainder 4 and state (6, 4); remainder 2 gives (4, 2); remainder 0 gives (2, 0). After these three body executions, while(b) is false and the function returns a = 2.

Question 6

Choose the correct output of the following Python program snippet:

A, B = 95, 65

while A != B:

if A > B:

A -= B

else:

B -= A

print(A)

  • A. 1

  • B. 5

  • C. 15

  • D. 30

Correct answer: B. 5.

Do not skip subtraction. The states are (95,65) -> (30,65) -> (30,35) -> (30,5) -> (25,5) -> (20,5) -> (15,5) -> (10,5) -> (5,5). The equal value is gcd(95,65) = 5. Question 5 used remainders; this version uses subtraction. Open the full solution for this question.

While Loop MCQs 7-8: binary digits and powers of two

Question 7

Choose the correct output of the following Python program snippet:

D = 11

B, E = 0, 1

while D > 0:

R = D % 2

D //= 2

B += R * E

E *= 10

print(B)

  • A. 1011

  • B. 1101

  • C. 1001

  • D. 1100

Correct answer: A. 1011.

old D

R

new D

B

E

11

1

5

1

10

5

1

2

11

100

2

0

1

11

1000

1

1

0

1011

10000

With D = 0, the next check fails and the program prints 1011. B remains a decimal integer whose digits encode binary 11; its type has not changed. Open the full solution for this question.

Question 8

Find the output of the following pseudo-code:

Integer value, n

Set value = 1, n = 45

while(value less than equal to n)

value = value << 1

end loop

Print value

  • A. 64

  • B. 32

  • C. 45

  • D. None of the above

Correct answer: A. 64.

A one-bit left shift doubles a positive integer. From 1, six body executions produce 2, 4, 8, 16, 32, 64. The next check, 64 <= 45, is false. The output is 64, the first power of two strictly greater than 45.

While Loop MCQs 9-10: sequential assignment and counting loop moves

Question 9

Below is pseudocode:

Set x to 1;

Set x1 to 0;

Set x2 to 0;

Set x3 to 1;

While (x < 10)

Set x1 = x1 + x2 + x3;

Set x2 = x2 + x1 + x3;

Set x3 = x3 + x2 + x1;

Write x1;

Write x2;

Write x3;

x = x + 1;

In which series is the output?

  • A. Tribonacci series

  • B. Fibonacci Series

  • C. Triangular Series

  • D. Arithmetic Series

Correct answer: A. Tribonacci series.

The condition is x < 10. Assignment is sequential, so each line uses the values updated just above it. From (x1,x2,x3) = (0,0,1), the first pass prints (1,2,4) and the second prints (7,13,24). The continuous sequence 0, 0, 1, 1, 2, 4, 7, 13, 24 adds the previous three terms to get each new term, identifying Tribonacci.

Question 10

Consider the following code snippet:

python
h = float(input())
w = float(input())
h1 = float(input())
w1 = float(input())
count = 0
while h != h1:
    if h1 <= h/2:
        count+=1
        h = h/2
    elif h1 > h/2:
        count+=1
        h = abs(h-(h-h1))
while w != w1:
    if w1 <= w/2:
        count+=1
        w = w/2
    elif w1 > w/2:
        count+=1
        w = abs(w-(w-w1))
print(count)

What does the above code indicate?

  • A. Decreases the dimension of the rectangular paper

  • B. Increases the height and width of the rectangular paper

  • C. Counts the minimum no. of moves to get to a particular dimension

  • D. Counts the maximum no. of moves to get to a particular dimension

Correct answer: C. Counts the minimum no. of moves to get to a particular dimension.

Each loop adjusts one side and adds one move. For positive target dimensions no larger than the starting dimensions, it halves while the target is at most half the current value. Otherwise, abs(h - (h - h1)) simplifies to h1, so that side reaches its target in one final adjustment. With h = 16 and h1 = 3, the states are 16 -> 8 -> 4 -> 3, so count = 3. The program prints a move count rather than a dimension, which selects option C. Under the stated halve-or-adjust rule, another halving is taken only when it stays at or above the target; the final adjustment then completes that side. For h = 2 and w = 3 with targets 2 and 2, the program prints 1 because the height already matches and the width reaches 2 in one adjustment.

Score the set, diagnose the miss, and choose the next practice step

A score of 9-10 means you are ready for mixed output problems. At 7-8, redo Questions 2, 4, 6 and 7 with a state table. At 0-6, rebuild condition, update and termination tracing before attempting more code.

Use the miss itself as a diagnosis. Question 2 points to truthiness. Questions 3-4 test condition, body and update order. Questions 5-6 require both variables to be traced. Questions 7-8 demand every intermediate value. Question 9 tests sequential assignment, while Question 10 asks you to read nested branches inside a loop and say what the printed value counts.

Do not merely note the answer. Write the missed state change beside each wrong response.

Use the Coding & DSA Courses page for broader problem-solving paths. The Python course linked above provides sequenced while-loop practice.