Bird
0
0

You want to print numbers 1 to 5 but skip 3 and repeat printing 4 twice using next and redo. Which code achieves this correctly?

hard📝 Application Q15 of 15
Ruby - Loops and Iteration
You want to print numbers 1 to 5 but skip 3 and repeat printing 4 twice using next and redo. Which code achieves this correctly?
i = 0
while i < 5
  i += 1
  if i == 3
    # skip 3
  elsif i == 4
    # print 4 twice
  end
  puts i
end
Aif i == 3 next elsif i == 4 puts i redo next end
Bif i == 3 next elsif i == 4 redo end
Cif i == 3 next elsif i == 4 puts i redo end
Dif i == 3 redo elsif i == 4 next end
Step-by-Step Solution
Solution:
  1. Step 1: Skip number 3 using next

    Using next when i == 3 skips printing 3 and moves to next iteration.
  2. Step 2: Repeat printing 4 twice using redo

    When i == 4, print it once, then use redo to repeat the iteration and print 4 again.
  3. Step 3: Verify code correctness

    if i == 3 next elsif i == 4 puts i redo end prints 4, then calls redo to repeat printing 4, and skips 3 correctly. Other options misuse next or redo causing errors or infinite loops.
  4. Final Answer:

    if i == 3 next elsif i == 4 puts i redo end -> Option C
  5. Quick Check:

    next skips, redo repeats current step [OK]
Quick Trick: Use next to skip, redo after puts to repeat [OK]
Common Mistakes:
  • Calling redo before puts causing infinite loop
  • Using next after redo incorrectly
  • Not skipping 3 properly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes