Bird
0
0

What will be the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Hashes
What will be the output of the following Ruby code?
def info(options = {})
  "Name: #{options[:name]}, Age: #{options[:age]}"
end

puts info(name: "Alice", age: 30)
AName: , Age:
BError: wrong number of arguments
CName: {:name=>"Alice"}, Age: {:age=>30}
DName: Alice, Age: 30
Step-by-Step Solution
Solution:
  1. Step 1: Understand method call with hash named parameters

    The method info accepts a hash options. The call info(name: "Alice", age: 30) passes a hash with keys :name and :age.
  2. Step 2: Evaluate string interpolation

    The method returns a string with values from the hash accessed by options[:name] and options[:age], which are "Alice" and 30 respectively.
  3. Final Answer:

    Name: Alice, Age: 30 -> Option D
  4. Quick Check:

    options[:name], options[:age] = Alice, 30 [OK]
Quick Trick: Access hash keys with options[:key] inside method [OK]
Common Mistakes:
  • Expecting error due to multiple arguments
  • Confusing hash keys with string keys
  • Not using symbol keys for access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes