Bird
0
0

What will be printed by this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Enumerable and Collection Processing
What will be printed by this Ruby code?
words = ["apple", "banana", "cherry", "date"]
filtered = words.select { |w| w.length == 5 }
puts filtered.join(", ")
Abanana, date
Bcherry, date
Capple, banana
Dapple
Step-by-Step Solution
Solution:
  1. Step 1: Check the length condition in select

    The block selects words with length exactly 5.
  2. Step 2: Identify words with length 5

    Lengths: "apple"=5, "banana"=6, "cherry"=6, "date"=4. Only "apple" matches, so filtered=["apple"], puts filtered.join(", ") prints "apple".
  3. Final Answer:

    apple -> Option D
  4. Quick Check:

    Length == 5 only matches "apple" [OK]
Quick Trick: Count string length carefully when filtering [OK]
Common Mistakes:
  • Miscounting string lengths
  • Including words with length not equal to 5
  • Assuming multiple matches without checking

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes