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 = 1
while i <= 2:
    j = 1
    while j <= 3:
        print(i, j)
        j += 1
    i += 1
A1 1\n1 2\n2 1\n2 2
B1 1\n2 1\n3 1
C1 1\n1 2\n1 3\n2 1\n2 2\n2 3
D1 1\n1 2\n1 3
Step-by-Step Solution
Solution:
  1. Step 1: Trace outer loop iterations

    i starts at 1 and runs while i <= 2, so i = 1 and i = 2.
  2. Step 2: Trace inner loop iterations for each i

    For each i, j runs from 1 to 3, printing i and j each time.
  3. Final Answer:

    1 1\n1 2\n1 3\n2 1\n2 2\n2 3 -> Option C
  4. Quick Check:

    Inner loop prints 3 times per outer loop iteration [OK]
Quick Trick: Inner loop runs fully for each outer loop iteration [OK]
Common Mistakes:
MISTAKES
  • Only printing inner loop once
  • Mixing up i and j values
  • Stopping loops too early

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes