Bird
0
0

Identify the problem in this Ruby code:

medium📝 Debug Q7 of 15
Ruby - Methods
Identify the problem in this Ruby code:
str = "test"
str.capitalize
puts str
AThe string is not changed because <code>capitalize</code> is non-bang.
BMissing bang method <code>capitalize!</code> causes syntax error.
CNo problem; output is "Test".
DThe code will raise an exception.
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between capitalize and capitalize!

    capitalize returns a new string with first letter capitalized but does not change original.
  2. Step 2: Check output of puts str

    Since str was not changed, it prints "test" instead of "Test".
  3. Final Answer:

    The string is not changed because capitalize is non-bang. -> Option A
  4. Quick Check:

    Non-bang methods do not modify original [OK]
Quick Trick: Use bang methods to modify original strings [OK]
Common Mistakes:
  • Expecting non-bang methods to change original
  • Confusing syntax error with behavior
  • Thinking code raises exceptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes