Bird
0
0

What will be the output of this Ruby code? ```ruby def order_summary(details = {}) "Item: #{details[:item]}, Quantity: #{details[:quantity] || 1}" end puts order_summary(item: 'Book') ```

medium📝 Predict Output Q5 of 15
Ruby - Hashes
What will be the output of this Ruby code? ```ruby def order_summary(details = {}) "Item: #{details[:item]}, Quantity: #{details[:quantity] || 1}" end puts order_summary(item: 'Book') ```
AError: undefined method
BItem: Book, Quantity:
CItem: , Quantity: 1
DItem: Book, Quantity: 1
Step-by-Step Solution
Solution:
  1. Step 1: Analyze default quantity handling

    details[:quantity] is nil, so || 1 returns 1 as default.
  2. Step 2: Substitute values in output string

    details[:item] is 'Book', so output is "Item: Book, Quantity: 1".
  3. Final Answer:

    Item: Book, Quantity: 1 -> Option D
  4. Quick Check:

    Default value with || operator works [OK]
Quick Trick: Use || to provide default values for missing keys [OK]
Common Mistakes:
  • Expecting nil to print instead of default
  • Confusing || with && operator
  • Assuming missing keys cause errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes