Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Basics and Runtime
What will be the output of this Ruby code?
def greet(name)
  "Hello, #{name}!"
end

puts greet("Friend")
Agreet(Friend)
BHello, name!
CHello, Friend!
DSyntaxError
Step-by-Step Solution
Solution:
  1. Step 1: Understand string interpolation in Ruby

    Ruby replaces #{name} with the value of the variable name inside the string.
  2. Step 2: Call the greet method with "Friend"

    The method returns "Hello, Friend!" which is printed by puts.
  3. Final Answer:

    Hello, Friend! -> Option C
  4. Quick Check:

    String interpolation works = Hello, Friend! [OK]
Quick Trick: Remember #{var} inserts variable value in strings [OK]
Common Mistakes:
  • Confusing #{name} as literal text
  • Expecting method name to print
  • Thinking puts returns the string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes