Bird
0
0

Identify the error in the following Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Arrays

Identify the error in the following Ruby code snippet:

a = [1, 2, 3]
b = [2, 3, 4]
result = a & b
puts result

What is the problem?

ASyntax error due to missing parentheses
BOutputs all elements from both arrays
CRuntime error because & is not defined for arrays
DNo error; code runs and outputs common elements
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax and operators

    The & operator is valid for arrays to find intersection.
  2. Step 2: Verify output behavior

    The code outputs common elements [2, 3] without error.
  3. Final Answer:

    No error; code runs and outputs common elements -> Option D
  4. Quick Check:

    Intersection & works fine [OK]
Quick Trick: & works for arrays to find common items without syntax errors [OK]
Common Mistakes:
  • Thinking & causes syntax error
  • Expecting union instead of intersection
  • Confusing output with difference

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes