Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Class Methods and Variables
Identify the error in this Ruby code:
hash = {a: 1, b: 2}
hash.freeze
hash[:c] = 3
ASyntaxError due to hash syntax
BNo error, hash is modified
CRuntimeError: can't modify frozen Hash
DTypeError: wrong type for key
Step-by-Step Solution
Solution:
  1. Step 1: Freeze the hash object

    Calling freeze on hash makes it immutable.
  2. Step 2: Attempt to add new key-value pair

    Trying to add [:c] = 3 modifies the frozen hash, causing RuntimeError.
  3. Final Answer:

    RuntimeError: can't modify frozen Hash -> Option C
  4. Quick Check:

    Modifying frozen hash = RuntimeError [OK]
Quick Trick: Frozen hashes cannot be changed; modification raises error [OK]
Common Mistakes:
  • Assuming hash can be modified after freeze
  • Confusing syntax error with runtime error
  • Expecting silent failure

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes