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?

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

    The - operator removes elements in the second array from the first.
  2. Step 2: Remove elements 2 and 4 from first array

    Removing 2 and 4 from [1, 2, 3, 4, 5] leaves [1, 3, 5].
  3. Final Answer:

    The difference array is [1, 3, 5] -> Option B
  4. Quick Check:

    Array difference = - [OK]
Quick Trick: Subtract arrays with - to remove elements [OK]
Common Mistakes:
MISTAKES
  • Expecting elements from second array
  • Using + instead of -
  • Confusing with intersection &

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes