Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Hashes
What will be the output of this Ruby code?
h = { name: 'Eve', 'age' => 28 }
puts h[:name]
puts h['age']
Anil nil
BEve 28
C 28
DEve
Step-by-Step Solution
Solution:
  1. Step 1: Access symbol key :name

    The key :name exists and maps to 'Eve', so puts h[:name] prints 'Eve'.
  2. Step 2: Access string key 'age'

    The key 'age' exists and maps to 28, so puts h['age'] prints '28'.
  3. Final Answer:

    Eve\n28 -> Option B
  4. Quick Check:

    Symbol and string keys access = D [OK]
Quick Trick: Use exact key type (symbol or string) to access hash values [OK]
Common Mistakes:
  • Mixing symbol and string keys when accessing
  • Expecting automatic conversion between key types
  • Assuming keys are interchangeable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes