Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Arrays

What is the output of the following Ruby code?

a = [1, 2, 3, 4]
b = [3, 4, 5, 6]
result = a | b
puts result.inspect
A[1, 2, 3, 4, 5, 6]
B[3, 4]
C[1, 2]
D[1, 2, 3, 4]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the union operator

    The | operator combines all unique elements from both arrays.
  2. Step 2: Apply union to arrays

    Arrays a and b combined uniquely are [1, 2, 3, 4, 5, 6].
  3. Final Answer:

    [1, 2, 3, 4, 5, 6] -> Option A
  4. Quick Check:

    Union = | [OK]
Quick Trick: Use | to combine unique elements from both arrays [OK]
Common Mistakes:
MISTAKES
  • Confusing union with intersection
  • Expecting duplicates in output
  • Using - which removes elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes