Bird
0
0

Which of the following method definitions correctly uses a hash as named parameters with a default empty hash?

easy📝 Syntax Q3 of 15
Ruby - Hashes
Which of the following method definitions correctly uses a hash as named parameters with a default empty hash?
Adef greet(options = []) "Hello, #{options[:name]}!" end
Bdef greet(name: {}) "Hello, #{name}!" end
Cdef greet(options) "Hello, #{options.name}!" end
Ddef greet(options = {}) "Hello, #{options[:name]}!" end
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct default parameter type

    Using options = {} sets default to an empty hash, allowing optional named parameters.
  2. Step 2: Check correct key access syntax

    Accessing options[:name] is correct for hash keys.
  3. Final Answer:

    def greet(options = {})\n "Hello, #{options[:name]}!"\nend -> Option D
  4. Quick Check:

    Default empty hash = options = {} [OK]
Quick Trick: Default hash parameter is options = {} [OK]
Common Mistakes:
MISTAKES
  • Using array [] instead of hash {} as default
  • Using keyword arguments syntax incorrectly
  • Accessing hash keys with dot notation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes