Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - String Operations
What will this Ruby code print?
text = <<-EOF
  Hello
  World
EOF
puts text
AHello World
BHello World
CdlroW olleH
D Hello World
Step-by-Step Solution
Solution:
  1. Step 1: Understand <<- preserves indentation

    The <<- heredoc preserves all indentation inside the string.
  2. Step 2: Check the string content

    Both lines have two spaces before the words, so those spaces remain in the output.
  3. Final Answer:

    Hello World -> Option D
  4. Quick Check:

    <<- preserves indentation = D [OK]
Quick Trick: <<- keeps indentation; <<~ removes common indent [OK]
Common Mistakes:
  • Assuming indentation is removed with <<-
  • Confusing output with single line string
  • Missing newline at end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes