Bird
0
0

Find and fix the error in this code snippet:

medium📝 Debug Q7 of 15
Ruby - Enumerable and Collection Processing
Find and fix the error in this code snippet:
arr1 = [1, 2]
arr2 = ["a", "b"]
result = arr1.zip arr2
puts result.join(', ')
AReplace <code>join</code> with <code>inspect</code> to print array correctly
BAdd parentheses to <code>zip</code> call to fix syntax
CConvert inner arrays to strings before joining
DNo error; code runs and prints correctly
Step-by-Step Solution
Solution:
  1. Step 1: Understand result of zip

    Result is an array of arrays like [[1, "a"], [2, "b"]].
  2. Step 2: Check join on nested arrays

    Calling join on nested arrays without converting inner arrays to strings causes error.
  3. Step 3: Fix by converting inner arrays to strings

    Use result.map(&:join).join(', ') to join inner arrays first.
  4. Final Answer:

    Convert inner arrays to strings before joining -> Option C
  5. Quick Check:

    Join nested arrays requires string conversion [OK]
Quick Trick: Join inner arrays before joining outer array [OK]
Common Mistakes:
  • Calling join directly on nested arrays
  • Assuming zip call needs parentheses
  • Using inspect instead of join for formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes