Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q13 of 15
Python - While Loop

What will be the output of this code?

counter = 1
while counter <= 3:
    print(counter)
    counter += 1
A1 2 3
B1 2 3 4
C0 1 2 3
D3 2 1
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop iterations

    Counter starts at 1, prints 1, then increments to 2, prints 2, increments to 3, prints 3, then increments to 4.
  2. Step 2: Check loop condition

    Loop stops when counter is 4 because 4 <= 3 is false. So printed numbers are 1, 2, 3.
  3. Final Answer:

    1 2 3 -> Option A
  4. Quick Check:

    Prints 1 to 3 with counter increment = C [OK]
Quick Trick: Loop runs while counter ≤ 3, prints 1,2,3 [OK]
Common Mistakes:
MISTAKES
  • Including 4 in output
  • Starting count from 0 instead of 1
  • Printing numbers in reverse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes