Bird
0
0

Why does this Ruby code not output the expected values?

medium📝 Debug Q7 of 15
Ruby - Hashes
Why does this Ruby code not output the expected values?
info = { 'name': 'Lara', :age => 30 }
puts info[:name]
puts info['age']
ABecause :age is not a valid symbol key
BBecause 'name': creates a symbol key, not a string key
CBecause keys must be all strings or all symbols
DBecause puts cannot print hash values directly
Step-by-Step Solution
Solution:
  1. Step 1: Understand key syntax

    The syntax 'name': creates a symbol key :name, not a string key 'name'.
  2. Step 2: Access keys

    info[:name] accesses the symbol key :name correctly, but info['age'] looks for a string key 'age' which does not exist.
  3. Final Answer:

    Because 'name': creates a symbol key, not a string key -> Option B
  4. Quick Check:

    Symbol keys created with colon after string literal [OK]
Quick Trick: Colon after quotes creates symbol keys [OK]
Common Mistakes:
  • Thinking 'name': creates a string key
  • Expecting string and symbol keys to be interchangeable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes