Bird
0
0

What error will this Ruby code produce?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What error will this Ruby code produce?
message = "hello".freeze
message << " world"
ARuntimeError: can't modify frozen String
BNo error, output: "hello world"
CSyntaxError: unexpected << operator
DTypeError: can't append to string
Step-by-Step Solution
Solution:
  1. Step 1: Understand freeze effect

    Freezing a string prevents any modifications to it.
  2. Step 2: Analyze the << operator

    The << operator tries to modify the original string by appending.
  3. Step 3: Resulting error

    Since the string is frozen, Ruby raises a RuntimeError when modification is attempted.
  4. Final Answer:

    RuntimeError: can't modify frozen String -> Option A
  5. Quick Check:

    Try running the code to confirm error message [OK]
Quick Trick: Modifying frozen strings raises RuntimeError [OK]
Common Mistakes:
  • Assuming no error occurs
  • Confusing RuntimeError with SyntaxError
  • Thinking TypeError is raised instead

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes