Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What is the output of the following Ruby code?
str = "  Hello World  "
puts str.strip.upcase
A" HELLO WORLD "
B" hello world "
C"hello world"
D"HELLO WORLD"
Step-by-Step Solution
Solution:
  1. Step 1: Apply strip to the string

    strip removes spaces at the start and end, so " Hello World " becomes "Hello World".
  2. Step 2: Apply upcase to the stripped string

    upcase converts all letters to uppercase, so "Hello World" becomes "HELLO WORLD".
  3. Final Answer:

    "HELLO WORLD" -> Option D
  4. Quick Check:

    strip + upcase = "HELLO WORLD" [OK]
Quick Trick: strip removes spaces, upcase makes uppercase [OK]
Common Mistakes:
  • Forgetting strip removes spaces
  • Applying upcase before strip
  • Expecting original spaces to remain

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes