Bird
0
0

What will be the output of this Ruby code?

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

puts greet("Alice")
AHello, name!
BHello, Alice!
Cgreet
DError: undefined method
Step-by-Step Solution
Solution:
  1. Step 1: Understand method behavior

    The method greet takes a parameter name and returns a string with the name inserted.
  2. Step 2: Evaluate the output of puts greet("Alice")

    This calls the method with "Alice", so the string becomes "Hello, Alice!" which is printed.
  3. Final Answer:

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

    Method returns string with interpolation [OK]
Quick Trick: String interpolation inserts variables inside strings [OK]
Common Mistakes:
  • Printing method name instead of return value
  • Using variable name literally
  • Syntax errors in string interpolation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes