Bird
0
0

What will be the output of this Ruby code?

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

puts greet("Alice")
AHello, #{name}!
Bgreet
CError: undefined method
DHello, Alice!
Step-by-Step Solution
Solution:
  1. Step 1: Understand method execution and string interpolation

    The method greet returns a string with the name inserted using #{name}.
  2. Step 2: Call greet with "Alice" and print result

    Calling greet("Alice") returns "Hello, Alice!" which puts prints.
  3. Final Answer:

    Hello, Alice! -> Option D
  4. Quick Check:

    String interpolation works = Hello, Alice! [OK]
Quick Trick: String interpolation inserts values inside double quotes [OK]
Common Mistakes:
MISTAKES
  • Expecting literal #{name} output
  • Confusing method name with output
  • Thinking method call causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes