Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - Functional Patterns in Ruby
Identify the error in this Ruby code:
hash = {a: 1, b: 2}
hash.freeze
hash[:a] = 3
puts hash[:a]
ANo error, prints 3
BSyntaxError because of hash syntax
CRuntimeError due to modifying frozen hash
DNo output because of infinite loop
Step-by-Step Solution
Solution:
  1. Step 1: Freeze the hash

    hash.freeze makes the hash immutable.
  2. Step 2: Attempt to modify frozen hash

    hash[:a] = 3 tries to change a value, causing RuntimeError.
  3. Final Answer:

    RuntimeError due to modifying frozen hash -> Option C
  4. Quick Check:

    freeze + hash modification = RuntimeError [OK]
Quick Trick: Frozen hashes cannot be modified; raises RuntimeError [OK]
Common Mistakes:
  • Assuming hash updates silently
  • Confusing syntax error with runtime error
  • Expecting old value to print without error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes