Bird
0
0

Identify the problem in this Ruby code snippet:

medium📝 Debug Q7 of 15
Ruby - String Operations
Identify the problem in this Ruby code snippet:
text = "  Hello World"
puts text.strip.upcase!
Astrip! method should be used instead of strip
Bstrip method removes only spaces inside the string
Cupcase! is not a valid Ruby method
Dupcase! modifies string in place and returns nil if no change
Step-by-Step Solution
Solution:
  1. Step 1: Understand upcase! behavior

    upcase! changes the string in place and returns nil if no changes were made.
  2. Step 2: Check method chaining with strip

    strip returns a new string, so chaining upcase! on it may return nil if the string is already uppercase.
  3. Final Answer:

    upcase! modifies string in place and returns nil if no change -> Option D
  4. Quick Check:

    upcase! returns nil if no change [OK]
Quick Trick: Bang methods (!) modify in place and may return nil [OK]
Common Mistakes:
  • Assuming upcase! always returns string
  • Confusing strip and strip!
  • Thinking upcase! is invalid

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes