Bird
0
0

Given two arrays a = [1, 2] and b = ['x', 'y'], what is the result of a.zip(b)?

easy📝 Conceptual Q2 of 15
Ruby - Enumerable and Collection Processing
Given two arrays a = [1, 2] and b = ['x', 'y'], what is the result of a.zip(b)?
A[[1, 2], ['x', 'y']]
B[[1, 'x'], [2, 'y']]
C[1, 'x', 2, 'y']
D[['x', 1], ['y', 2]]
Step-by-Step Solution
Solution:
  1. Step 1: Apply zip to arrays a and b

    Each element from a pairs with the element at the same index in b.
  2. Step 2: Form pairs

    Index 0: 1 and 'x', Index 1: 2 and 'y', so result is [[1, 'x'], [2, 'y']].
  3. Final Answer:

    [[1, 'x'], [2, 'y']] -> Option B
  4. Quick Check:

    Zip pairs elements by index = [[1, 'x'], [2, 'y']] [OK]
Quick Trick: Zip pairs elements index-wise into subarrays [OK]
Common Mistakes:
  • Mixing up order of elements in pairs
  • Expecting a flat array instead of nested arrays
  • Confusing zip with concatenation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes