Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Hashes

Identify the error in this Ruby code snippet:

a = {x: 10}
b = {y: 20}
a.merge! b
puts a
AOutput will be empty hash
BNo error; code works correctly
CRuntime error because merge! cannot be used with variables
DSyntax error due to missing parentheses in merge!
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of merge! method call

    merge! can be called without parentheses in Ruby, so a.merge! b is valid syntax.
  2. Step 2: Verify runtime behavior

    merge! updates a with keys and values from b, so a becomes {x: 10, y: 20}. No errors occur.
  3. Final Answer:

    No error; code works correctly -> Option B
  4. Quick Check:

    merge! updates original hash without error [OK]
Quick Trick: merge! can be called without parentheses [OK]
Common Mistakes:
  • Thinking parentheses are mandatory
  • Assuming merge! returns a new hash
  • Expecting runtime error with variable argument

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes