Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Arrays
What will be the output of this Ruby code?
arr = [1, [2, 3], [4, 5]]
puts arr.flatten.include?(3)
Anil
Bfalse
CError
Dtrue
Step-by-Step Solution
Solution:
  1. Step 1: Flatten the nested array

    arr.flatten converts [1, [2, 3], [4, 5]] into [1, 2, 3, 4, 5].
  2. Step 2: Check if 3 is included

    include?(3) returns true because 3 is in the flattened array.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    flatten + include? = true if element present [OK]
Quick Trick: Flatten first, then check include? for nested arrays [OK]
Common Mistakes:
MISTAKES
  • Not flattening before include? check
  • Expecting false because 3 is nested
  • Confusing include? with length

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes