Bird
0
0

Consider this Ruby code snippet:

medium📝 Debug Q14 of 15
Ruby - Methods
Consider this Ruby code snippet:
str = "Hello"
str.upcase
puts str
str.upcase!
puts str
What is the error in this code?
ANo error; output is "HELLO" then "HELLO"
B<code>puts</code> cannot print strings
CCalling <code>upcase</code> without assignment does not change <code>str</code>
D<code>upcase!</code> method does not exist
Step-by-Step Solution
Solution:
  1. Step 1: Analyze str.upcase call

    str.upcase returns an uppercase string but does not change str itself unless assigned.
  2. Step 2: Analyze str.upcase! call

    str.upcase! modifies str in place, changing it to uppercase.
  3. Final Answer:

    Calling upcase without assignment does not change str -> Option C
  4. Quick Check:

    Non-bang needs assignment to change object [OK]
Quick Trick: Non-bang methods need assignment to change variable [OK]
Common Mistakes:
MISTAKES
  • Thinking non-bang changes original string
  • Believing upcase! does not exist
  • Assuming puts can't print strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes