Bird
0
0

Identify the problem in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Loops and Iteration
Identify the problem in this Ruby code:
i = 0
while i < 3
  i += 1
  if i == 2
    redo
  end
  puts i
end
AThe loop will print 1, 2, 3 correctly.
BThe variable <code>i</code> is not incremented properly.
CThe <code>redo</code> keyword should be replaced with <code>next</code>.
DThe loop will run infinitely because <code>redo</code> repeats the iteration without incrementing <code>i</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Trace the loop with redo

    When i == 2, redo restarts the iteration without incrementing i again.
  2. Step 2: Understand loop behavior

    This causes an infinite loop printing 2 repeatedly.
  3. Final Answer:

    The loop will run infinitely because redo repeats the iteration without incrementing i. -> Option D
  4. Quick Check:

    Redo without increment causes infinite loop [OK]
Quick Trick: Redo repeats iteration, watch variable changes [OK]
Common Mistakes:
  • Assuming loop ends normally
  • Confusing redo with next
  • Ignoring variable increment inside loop

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes