Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Loops and Iteration
What is the output of this Ruby code?
i = 0
while i < 3
  i += 1
  if i == 2
    redo
  end
  puts i
end
A1 2 2 2 ... (infinite loop)
B1 3
C1 2 3
D1 3 3
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop and redo behavior

    When i == 2, redo repeats the current iteration without incrementing i again, causing the loop to print 2 repeatedly.
  2. Step 2: Understand infinite repetition

    Since i is not changed inside the redo loop, it never moves past 2, causing an infinite loop printing 2 repeatedly.
  3. Final Answer:

    1 2 2 2 ... (infinite loop) -> Option A
  4. Quick Check:

    redo repeats current step causing infinite loop [OK]
Quick Trick: Redo repeats current step, watch for infinite loops [OK]
Common Mistakes:
MISTAKES
  • Assuming i increments inside redo
  • Thinking redo skips iteration
  • Ignoring infinite loop possibility

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes