Bird
0
0

Find the error in this code:

medium📝 Debug Q7 of 15
Ruby - Enumerable and Collection Processing
Find the error in this code:
arr = [5, 10, 15]
filtered = arr.reject { |x| x > 10 }
puts filtered
AThe block variable is missing.
BNo error; code works and prints expected output.
CThe puts method will print the array object id, not elements.
DThe reject method does not return a new array.
Step-by-Step Solution
Solution:
  1. Step 1: Check block and reject usage

    The block has a variable x and condition x > 10, which is valid.
  2. Step 2: Understand puts behavior

    Calling puts on an array prints each element on a new line, which is expected.
  3. Final Answer:

    No error; code works and prints expected output. -> Option B
  4. Quick Check:

    reject returns new array; puts prints elements [OK]
Quick Trick: puts prints array elements line by line [OK]
Common Mistakes:
  • Thinking puts prints array object id
  • Assuming reject modifies original array
  • Missing block variable in reject

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes