Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q6 of 15
Ruby - String Operations
Identify the error in this Ruby code:
str = "hello"
str.freeze!
str << " world"
AString must be frozen before assignment
BCannot append to string without converting to symbol
CNo error; code runs fine
DMethod freeze! does not exist; should use freeze
Step-by-Step Solution
Solution:
  1. Step 1: Check method names for freezing

    Ruby's String class has freeze method, but no freeze! method.
  2. Step 2: Identify the error

    Calling freeze! causes a NoMethodError; correct method is freeze.
  3. Final Answer:

    Method freeze! does not exist; should use freeze -> Option D
  4. Quick Check:

    Use freeze, not freeze! method [OK]
Quick Trick: Use freeze, not freeze! method [OK]
Common Mistakes:
  • Using freeze! instead of freeze
  • Thinking freeze! is a valid method
  • Ignoring method name case sensitivity

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes