Bird
0
0

The following Ruby code prints nothing. What is the problem?

medium📝 Debug Q14 of 15
Ruby - Basics and Runtime
The following Ruby code prints nothing. What is the problem?
text = "HELLO"
puts text.upcase!
AMissing parentheses for method call
BUsing <code>upcase!</code> on a string literal causes error
CThe method <code>upcase!</code> returns nil if no changes made
DStrings are not objects, so methods can't be called
Step-by-Step Solution
Solution:
  1. Step 1: Understand upcase! behavior

    The upcase! method modifies the string in place and returns nil if no changes were made.
  2. Step 2: Analyze the code's output

    Since "HELLO" is uppercase, upcase! makes no changes and returns nil, so puts prints nothing.
  3. Step 3: Identify the error cause

    The return value nil is printed when no mutation occurs.
  4. Final Answer:

    upcase! returns nil if no changes made -> Option C
  5. Quick Check:

    Bang methods can return nil if no change [OK]
Quick Trick: Bang (!) methods may return nil if no change happens [OK]
Common Mistakes:
  • Thinking bang methods always return a string
  • Assuming strings are not objects
  • Missing that upcase! modifies in place

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes