Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - String Operations
What will be the output of this Ruby code?
str = "Hello"
str << " " << "World"
puts str
AHello << World
BHelloWorld
CHello World
DError: invalid syntax
Step-by-Step Solution
Solution:
  1. Step 1: Understand chaining of << operator

    The << operator returns the modified string, so chaining appends multiple strings.
  2. Step 2: Trace the string changes

    First append adds a space, second append adds "World", resulting in "Hello World".
  3. Final Answer:

    Hello World -> Option C
  4. Quick Check:

    Chained << appends strings in order [OK]
Quick Trick: << returns modified string, so you can chain it [OK]
Common Mistakes:
  • Thinking << doesn't chain
  • Expecting no space added
  • Confusing output with error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes