Bird
0
0

Which of the following is the correct syntax to define a method with keyword arguments in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Methods
Which of the following is the correct syntax to define a method with keyword arguments in Ruby?
Adef greet(name =>, age =>); puts "Hello #{name}, you are #{age}"; end
Bdef greet(name, age); puts "Hello #{name}, you are #{age}"; end
Cdef greet(name:, age:); puts "Hello #{name}, you are #{age}"; end
Ddef greet(name =:, age =:); puts "Hello #{name}, you are #{age}"; end
Step-by-Step Solution
Solution:
  1. Step 1: Review Ruby keyword argument syntax

    Keyword arguments are defined with a colon after the argument name, like name:.
  2. Step 2: Check each option

    def greet(name:, age:); puts "Hello #{name}, you are #{age}"; end uses correct syntax with name: and age:. Others use invalid symbols or positional arguments.
  3. Final Answer:

    def greet(name:, age:); puts "Hello #{name}, you are #{age}"; end -> Option C
  4. Quick Check:

    Keyword args use colon after name [OK]
Quick Trick: Keyword args end with colon in method definition [OK]
Common Mistakes:
  • Using => instead of : for keyword arguments
  • Defining keyword arguments without colons
  • Confusing positional and keyword argument syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes