Bird
0
0

Find the error in this Ruby loop:

medium📝 Debug Q14 of 15
Ruby - Control Flow
Find the error in this Ruby loop:
i = 0
while i < 3
  puts i
  i = i + 1
end
puts i
AThere is no error; the code runs correctly
BThe loop will never run because i is 0
CMissing 'end' keyword to close the while loop
DThe variable i is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Check loop initialization and condition

    Variable i starts at 0, condition is i < 3, so loop runs while i is 0,1,2.
  2. Step 2: Verify loop syntax and increment

    Loop has correct 'end', increments i by 1 each time, so it will stop after i reaches 3.
  3. Final Answer:

    There is no error; the code runs correctly -> Option A
  4. Quick Check:

    Loop runs 3 times, i increments properly [OK]
Quick Trick: Check loop start, condition, increment, and end [OK]
Common Mistakes:
  • Thinking loop never runs with i=0
  • Missing the 'end' keyword (but it's present here)
  • Assuming variable i is uninitialized

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes