Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Methods
What will be the output of this Ruby code?
str = "hello"
result = str.upcase!
puts result
puts str
AHELLO HELLO
Bhello hello
CHELLO hello
Dnil hello
Step-by-Step Solution
Solution:
  1. Step 1: Understand upcase! effect

    The upcase! method changes the string to uppercase in place and returns the modified string.
  2. Step 2: Check output of result and str

    Both result and str will be "HELLO" because the original string was changed.
  3. Final Answer:

    HELLO HELLO -> Option A
  4. Quick Check:

    upcase! modifies and returns changed string [OK]
Quick Trick: Bang methods return modified object, not a new one [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Thinking upcase! returns nil always
  • Confusing upcase and upcase!

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes