Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - String Operations
What will be the output of the following Ruby code?
str = "Hi"
str << " there"
puts str
AHi there
BHi
Cereht iH
DError: undefined method << for String
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the initial string and operation

    The variable str starts as "Hi". The << operator appends " there" to it, modifying str to "Hi there".
  2. Step 2: Understand the output of puts

    puts str prints the string with a newline, but the output shown is the string content without the newline character visible.
  3. Final Answer:

    Hi there -> Option A
  4. Quick Check:

    Appending with << changes string to "Hi there" [OK]
Quick Trick: Remember << adds text inside original string [OK]
Common Mistakes:
MISTAKES
  • Expecting str to remain "Hi"
  • Confusing puts output with return value
  • Thinking << causes an error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes