Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Enumerable and Collection Processing

What is the output of the following Ruby code?

words = ["apple", "banana", "cherry", "date"]
result = words.find { |w| w.length > 5 }
puts result
A"cherry"
Bnil
C"apple"
D"banana"
Step-by-Step Solution
Solution:
  1. Step 1: Check each word's length

    "apple" (5), "banana" (6), "cherry" (6), "date" (4).
  2. Step 2: Find first word with length > 5

    "banana" is the first word longer than 5 characters.
  3. Final Answer:

    "banana" -> Option D
  4. Quick Check:

    First word with length > 5 = "banana" [OK]
Quick Trick: Check elements in order; find stops at first match [OK]
Common Mistakes:
  • Choosing the last matching word instead of first
  • Confusing length condition
  • Assuming find returns all matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes