Bird
0
0

What is wrong with this Ruby code?

medium📝 Debug Q7 of 15
Ruby - String Operations
What is wrong with this Ruby code?
text = "example".freeze
text[1] = "A"
ARaises TypeError due to incompatible types
BIndex assignment is invalid syntax
CNo error; string is modified successfully
DCannot modify a frozen string; raises RuntimeError
Step-by-Step Solution
Solution:
  1. Step 1: Recognize string freezing

    The string text is frozen and cannot be changed.
  2. Step 2: Understand index assignment

    Assigning to an index modifies the string in place.
  3. Step 3: Resulting error

    Attempting to modify a frozen string raises a RuntimeError.
  4. Final Answer:

    Cannot modify a frozen string; raises RuntimeError -> Option D
  5. Quick Check:

    Check if text.frozen? is true before modification [OK]
Quick Trick: Index assignment modifies string; frozen strings disallow it [OK]
Common Mistakes:
MISTAKES
  • Thinking index assignment is syntax error
  • Assuming string can be modified silently
  • Confusing RuntimeError with TypeError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes