Bird
0
0

What is the output of this code?

medium📝 Predict Output Q13 of 15
Python - While Loop

What is the output of this code?

i = 0
while i < 3:
    print(i)
    i += 1
else:
    print('Done')
A0 1 2
B0 1 2 Done
CDone
D0 1 2 3 Done
Step-by-Step Solution
Solution:
  1. Step 1: Trace the while loop iterations

    Variable i starts at 0 and increments by 1 each loop until i < 3 is false. It prints 0, 1, 2.
  2. Step 2: Check else block execution

    Since the loop ends normally (i becomes 3, condition false), the else block runs and prints 'Done'.
  3. Final Answer:

    0 1 2 Done -> Option B
  4. Quick Check:

    Loop prints 0-2, else prints Done [OK]
Quick Trick: Else runs after loop if no break, prints 'Done' [OK]
Common Mistakes:
MISTAKES
  • Ignoring else block output
  • Expecting 3 to print inside loop
  • Thinking else runs only on break

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes