Bird
0
0

The following Ruby code is intended to zip two arrays but raises an error. What is the problem?

medium📝 Debug Q14 of 15
Ruby - Enumerable and Collection Processing
The following Ruby code is intended to zip two arrays but raises an error. What is the problem?
a = [1, 2]
b = ["x", "y", "z"]
result = a.zip *b
puts result.inspect
AUsing double quotes instead of single quotes causes error.
BArrays have different lengths, causing an error.
CMissing parentheses around argument in <code>zip *b</code> call.
DThe variable <code>result</code> is not defined before use.
Step-by-Step Solution
Solution:
  1. Step 1: Check method call syntax

    In Ruby, calling zip without parentheses is allowed only if no ambiguity exists, but here parentheses are recommended.
  2. Step 2: Confirm array length and variable usage

    Different lengths do not cause errors; zip fills missing with nil. Variables are defined correctly.
  3. Final Answer:

    Missing parentheses around argument in zip *b call. -> Option C
  4. Quick Check:

    Use parentheses for method arguments to avoid errors = C [OK]
Quick Trick: Use parentheses with method arguments to avoid syntax errors [OK]
Common Mistakes:
  • Assuming different lengths cause errors
  • Ignoring parentheses in method calls
  • Confusing string quote types with errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes