Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Enumerable and Collection Processing
What is the output of the following Ruby code?
a = [1, 2, 3]
b = ['a', 'b', 'c']
result = a.zip(b)
puts result.inspect
A[[1, 2, 3], ['a', 'b', 'c']]
B[1, 'a', 2, 'b', 3, 'c']
C[[1, 'a'], [2, 'b'], [3, nil]]
D[[1, 'a'], [2, 'b'], [3, 'c']]
Step-by-Step Solution
Solution:
  1. Step 1: Understand zip pairing

    The method pairs elements by index: 1 with 'a', 2 with 'b', 3 with 'c'.
  2. Step 2: Check output format

    The result is an array of arrays, each inner array holding one pair.
  3. Final Answer:

    [[1, 'a'], [2, 'b'], [3, 'c']] -> Option D
  4. Quick Check:

    Zip pairs elements by index = B [OK]
Quick Trick: Zip pairs elements index-wise into nested arrays [OK]
Common Mistakes:
  • Expecting flat array instead of nested arrays
  • Confusing zip with concatenation
  • Misunderstanding nil insertion

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes