Bird
0
0

What will this code print?

medium📝 Predict Output Q5 of 15
Python - While Loop
What will this code print?
i = 0
while i < 2:
    j = 0
    while j < 3:
        print(i + j, end=' ')
        j += 1
    i += 1
A0 1 2 3 4 5
B0 1 2 1 2 3
C0 1 2 0 1 2
D1 2 3 2 3 4
Step-by-Step Solution
Solution:
  1. Step 1: Calculate values for i=0

    j runs 0 to 2, prints 0+0=0, 0+1=1, 0+2=2.
  2. Step 2: Calculate values for i=1

    j runs 0 to 2, prints 1+0=1, 1+1=2, 1+2=3.
  3. Final Answer:

    Output is: 0 1 2 1 2 3 -> Option B
  4. Quick Check:

    Sum of i and j printed in nested loops = output [OK]
Quick Trick: Add outer and inner loop variables for output [OK]
Common Mistakes:
MISTAKES
  • Not resetting inner loop variable
  • Confusing i and j values
  • Forgetting end=' ' in print

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes