Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Enumerable and Collection Processing
What is the output of this Ruby code?
arr = [10, 15, 20, 25]
result = arr.reject { |x| x > 15 }
puts result.inspect
A[]
B[10, 15]
C[10, 15, 20, 25]
D[20, 25]
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the reject block condition

    The block rejects elements where x > 15 is true, so elements greater than 15 are removed.
  2. Step 2: Identify remaining elements

    Elements 20 and 25 are removed; 10 and 15 remain.
  3. Final Answer:

    [10, 15] -> Option B
  4. Quick Check:

    reject x > 15 = keep x ≤ 15 [OK]
Quick Trick: Reject removes elements where block is true [OK]
Common Mistakes:
  • Confusing reject with select
  • Including rejected elements in output
  • Misreading the block condition

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes