Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Variables and Data Types
What will be the output of the following Ruby code?
name = "Alice"
puts 'Hello, #{name}!'
puts "Hello, #{name}!"
AHello, #{name}! Hello, Alice!
BHello, Alice! Hello, Alice!
CHello, #{name}! Hello, #{name}!
DHello, Alice! Hello, #{name}!
Step-by-Step Solution
Solution:
  1. Step 1: Understand string interpolation in Ruby

    Interpolation #{variable} works only inside double-quoted strings, not single-quoted.
  2. Step 2: Analyze each puts statement output

    First puts uses single quotes, so prints literally 'Hello, #{name}!'. Second uses double quotes, so inserts variable value 'Alice'.
  3. Final Answer:

    Hello, #{name}! Hello, Alice! -> Option A
  4. Quick Check:

    Interpolation only in double quotes [OK]
Quick Trick: Only double quotes allow #{variable} interpolation [OK]
Common Mistakes:
MISTAKES
  • Assuming interpolation works in single quotes
  • Confusing output order
  • Ignoring difference between single and double quotes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes