Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Arrays

What will be the output of the following Ruby code?

a = [5, 6, 7]
b = [7, 8, 9]
result = a | b
puts result.inspect
A[7]
B[5, 6, 7, 8, 9]
C[5, 6, 7, 7, 8, 9]
D[5, 6, 8, 9]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the '|' operator

    The '|' operator returns the union of two arrays, combining unique elements from both.
  2. Step 2: Apply to given arrays

    Arrays a and b are combined without duplicates: [5, 6, 7, 8, 9].
  3. Final Answer:

    [5, 6, 7, 8, 9] -> Option B
  4. Quick Check:

    Union operator '|' merges unique elements [OK]
Quick Trick: Use '|' for union of arrays without duplicates [OK]
Common Mistakes:
  • Expecting duplicates in the union result
  • Confusing '|' with concatenation '+'
  • Assuming '&' returns union instead of intersection

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes