Bird
0
0

Why does this Ruby code cause an error?

medium📝 Debug Q7 of 15
Ruby - Loops and Iteration
Why does this Ruby code cause an error?
3.times { |i| puts i + j }
Atimes block cannot use addition
BVariable j is not defined
CBlock variable i must be named j
Dputs cannot print expressions
Step-by-Step Solution
Solution:
  1. Step 1: Check variables used inside block

    The block uses j which is not defined anywhere.
  2. Step 2: Understand scope rules

    Undefined variables cause a runtime error in Ruby.
  3. Final Answer:

    Variable j is not defined -> Option B
  4. Quick Check:

    Undefined variable causes error [OK]
Quick Trick: All variables must be defined before use in block [OK]
Common Mistakes:
  • Assuming block variable covers all variables
  • Thinking addition is invalid in block
  • Confusing variable names

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes