Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Loops and Iteration
What will be the output of this Ruby code?
count = 0
while count < 3
  count += 1
  if count == 2
    redo
  end
  puts count
end
A1 2 2 3
B1 2 2 2 ...
C1 3
D1 2 3
Step-by-Step Solution
Solution:
  1. Step 1: Analyze loop and redo

    When count == 2, redo repeats the current iteration without incrementing count.
  2. Step 2: Effect of redo here

    Since count is not incremented again inside the loop, it stays 2, causing infinite repeats printing 2.
  3. Final Answer:

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

    redo repeats iteration without changing variables [OK]
Quick Trick: Redo repeats iteration without increment, causing infinite loop [OK]
Common Mistakes:
MISTAKES
  • Assuming redo increments count
  • Thinking redo skips iteration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes