Bird
0
0

Consider this Ruby code:

hard📝 Application Q9 of 15
Ruby - Loops and Iteration
Consider this Ruby code:
arr = [1, 2, 3, 4]
arr.each do |num|
  if num == 3
    redo
  end
  puts num
end

What will happen when this code runs?
AIt prints 1, 2, then gets stuck repeating 3 infinitely.
BIt skips printing 3 and continues with 4.
CIt prints 1, 2, 3, 4 normally.
DIt raises a syntax error.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze redo inside each block

    When num == 3, redo repeats the current iteration without advancing.
  2. Step 2: Understand loop behavior

    This causes infinite repetition of 3, so printing 3 repeats endlessly.
  3. Final Answer:

    It prints 1, 2, then gets stuck repeating 3 infinitely. -> Option A
  4. Quick Check:

    Redo causes infinite repeat on 3 [OK]
Quick Trick: Redo repeats iteration, watch for infinite loops [OK]
Common Mistakes:
MISTAKES
  • Assuming loop continues after redo
  • Thinking redo skips iteration
  • Expecting syntax error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes