Bird
0
0

You want to create a method greet_person that takes a name and returns a greeting like "Hello, Alice!". Which code correctly does this?

hard📝 Application Q15 of 15
Ruby - Methods
You want to create a method greet_person that takes a name and returns a greeting like "Hello, Alice!". Which code correctly does this?
Adef greet_person(name) puts "Hello, #{name}!" end
Bdef greet_person(name) "Hello, " + name + "!" end
Cdef greet_person(name) return puts "Hello, " + name + "!" end
Ddef greet_person(name) print "Hello, " + name + "!" end
Step-by-Step Solution
Solution:
  1. Step 1: Understand method return vs output

    To return a greeting string, the method should produce the string as its last expression or use return. Using puts or print outputs to console but returns nil.
  2. Step 2: Analyze each option

    def greet_person(name) "Hello, " + name + "!" end returns the greeting string correctly. def greet_person(name) puts "Hello, #{name}!" end prints but returns nil. def greet_person(name) return puts "Hello, " + name + "!" end returns nil because puts returns nil. def greet_person(name) print "Hello, " + name + "!" end prints but does not return the string.
  3. Final Answer:

    def greet_person(name) "Hello, " + name + "!" end -> Option B
  4. Quick Check:

    Return string, not print it [OK]
Quick Trick: Return string directly; puts prints but returns nil [OK]
Common Mistakes:
MISTAKES
  • Using puts instead of returning string
  • Returning result of puts (which is nil)
  • Confusing print with return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes