Bird
0
0

What will be the output of this Ruby code? ```ruby def details(opts = {}) "Age: #{opts[:age]}, City: #{opts[:city]}" end puts details(city: 'Paris', age: 25) ```

medium📝 Predict Output Q4 of 15
Ruby - Hashes
What will be the output of this Ruby code? ```ruby def details(opts = {}) "Age: #{opts[:age]}, City: #{opts[:city]}" end puts details(city: 'Paris', age: 25) ```
AAge: Paris, City: 25
BAge: 25, City: Paris
CAge: , City:
DError: wrong number of arguments
Step-by-Step Solution
Solution:
  1. Step 1: Understand method call with named parameters

    The method receives a hash with keys :city and :age.
  2. Step 2: Substitute values in string interpolation

    opts[:age] is 25 and opts[:city] is 'Paris', so output is "Age: 25, City: Paris".
  3. Final Answer:

    Age: 25, City: Paris -> Option B
  4. Quick Check:

    Hash keys match output values [OK]
Quick Trick: Hash keys map to values in output string [OK]
Common Mistakes:
MISTAKES
  • Mixing up key-value order
  • Expecting error due to missing parentheses
  • Confusing keys with values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes