Bird
0
0

What does the Ruby expression [1, 2, 3] & [2, 3, 4] return?

easy📝 Conceptual Q2 of 15
Ruby - Arrays

What does the Ruby expression [1, 2, 3] & [2, 3, 4] return?

A[2, 3]
B[1, 2, 3, 4]
C[1, 4]
D[1, 2]
Step-by-Step Solution
Solution:
  1. Step 1: Understand the & operator on arrays

    The & operator returns the intersection of two arrays, elements common to both.
  2. Step 2: Find common elements

    Between [1, 2, 3] and [2, 3, 4], common elements are 2 and 3.
  3. Final Answer:

    The intersection is [2, 3] -> Option A
  4. Quick Check:

    Array intersection = & [OK]
Quick Trick: Use & to get common elements between arrays [OK]
Common Mistakes:
MISTAKES
  • Confusing & with +
  • Expecting & to merge arrays
  • Mixing & with | operator

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes