Bird
0
0

What is the issue with this Ruby for loop code snippet?

medium📝 Debug Q6 of 15
Ruby - Loops and Iteration
What is the issue with this Ruby for loop code snippet?

for i in 1..5
puts i * 2
AThe <code>puts</code> method cannot be used inside a <code>for</code> loop.
BThe range <code>1..5</code> is invalid syntax.
CThe variable <code>i</code> cannot be used inside the loop.
DThe loop is missing the <code>end</code> keyword to close it.
Step-by-Step Solution
Solution:
  1. Step 1: Check loop syntax

    The for loop must be closed with an end keyword.
  2. Step 2: Identify missing end

    The given code snippet lacks the end keyword after the loop body.
  3. Final Answer:

    The loop is missing the end keyword to close it. -> Option D
  4. Quick Check:

    Code blocks in Ruby require end [OK]
Quick Trick: Always close loops with 'end' in Ruby [OK]
Common Mistakes:
MISTAKES
  • Forgetting to add 'end' after the loop body
  • Misunderstanding range syntax
  • Assuming variables can't be used inside loops

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes