Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Hashes

Identify the error in this Ruby code snippet:

a = {foo: 1}
b = {bar: 2}
a.merge! b
puts b
ASyntax error due to missing parentheses
BError because merge! modifies b instead of a
CNo error; output is {:bar=>2}
DRuntime error because merge! returns nil
Step-by-Step Solution
Solution:
  1. Step 1: Understand merge! modifies the receiver

    merge! modifies a, not b.
  2. Step 2: Check output of puts b

    b remains unchanged, so output is {:bar=>2}.
  3. Final Answer:

    No error; output is {:bar=>2} -> Option C
  4. Quick Check:

    merge! modifies receiver, other hash unchanged [OK]
Quick Trick: merge! changes receiver hash only [OK]
Common Mistakes:
  • Thinking merge! changes argument hash
  • Expecting syntax error without parentheses

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes