Bird
0
0

What will be printed by this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Enumerable and Collection Processing
What will be printed by this Ruby code?
x = [10, 20]
y = ["a", "b", "c"]
result = x.zip(y)
result.each { |pair| puts pair.join('-') }
A10-a 20-b - c
B10-a 20-b 20-c
C10-a 20-b
D10-a 20-b -
Step-by-Step Solution
Solution:
  1. Step 1: Zip arrays with different lengths

    Result is [[10, 'a'], [20, 'b']] because zip length matches the receiver array length.
  2. Step 2: Check output of each pair joined by '-'

    Pairs: '10-a', '20-b'. The third pair does not exist.
  3. Final Answer:

    10-a 20-b -> Option C
  4. Quick Check:

    Zip stops at shortest array length = 2 pairs [OK]
Quick Trick: Zip length matches first array length [OK]
Common Mistakes:
  • Expecting zip to produce 3 pairs
  • Confusing zip behavior with fill
  • Misreading join output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes