Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q4 of 15
Python - While Loop
What will be the output of this code?
i = 1
while i <= 3:
    print(i)
    i *= 2
A1 2
B1 2 3
C1 2 4
D1 2 4 8
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    i=1 (1<=3 true), print 1, i*=2 ->2; i=2 (2<=3 true), print 2, i*=2 ->4; i=4 (4<=3 false), end.
  2. Step 2: Confirm printed values

    Values printed are 1 and 2; 4 is assigned but loop ends before printing 4.
  3. Final Answer:

    1 2 -> Option A
  4. Quick Check:

    Loop variable update and condition checked each iteration [OK]
Quick Trick: Multiply loop variable carefully to avoid skipping exit [OK]
Common Mistakes:
MISTAKES
  • Assuming 4 is printed after condition fails
  • Confusing loop exit condition
  • Not tracing variable updates correctly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes