Bird
0
0

Identify the error in this Ruby code:

medium📝 Debug Q14 of 15
Ruby - String Operations
Identify the error in this Ruby code:
name = "Alice"
name.freeze
name.upcase!
AError because <code>upcase!</code> tries to modify a frozen string.
BNo error; the code runs fine.
CError because <code>freeze</code> is called incorrectly.
DError because <code>name</code> is not a string.
Step-by-Step Solution
Solution:
  1. Step 1: Check freeze usage

    The freeze method is correctly called on the string name, making it immutable.
  2. Step 2: Understand upcase! effect

    The upcase! method tries to change the string in place, which is not allowed on a frozen string, causing a runtime error.
  3. Final Answer:

    Error because upcase! tries to modify a frozen string. -> Option A
  4. Quick Check:

    Modifying frozen string with upcase! = error B [OK]
Quick Trick: In-place methods fail on frozen strings [OK]
Common Mistakes:
  • Thinking freeze is called incorrectly
  • Assuming no error on upcase!
  • Confusing variable type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes