Bird
0
0

Identify the error in this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Class Methods and Variables
Identify the error in this Ruby code snippet:
name = "Alice"
name.freeze
name[0] = 'M'
puts name
ANo error, output is "Mlice"
BRuntimeError: can't modify frozen String
CSyntaxError due to invalid assignment
DTypeError: wrong type of argument
Step-by-Step Solution
Solution:
  1. Step 1: Recognize string freezing

    The string name is frozen, so it cannot be changed after name.freeze.
  2. Step 2: Understand the attempted modification

    Trying to change the first character with name[0] = 'M' modifies the string, which is forbidden and raises a runtime error.
  3. Final Answer:

    RuntimeError: can't modify frozen String -> Option B
  4. Quick Check:

    Changing frozen string = RuntimeError [OK]
Quick Trick: Changing frozen strings causes runtime errors [OK]
Common Mistakes:
  • Assuming strings can be changed after freeze
  • Confusing syntax error with runtime error
  • Ignoring freeze effect on string elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes