Bird
0
0

What issue will arise when running this Ruby code?

medium📝 Debug Q6 of 15
Ruby - Hashes
What issue will arise when running this Ruby code?
data = { :title => 'Book', 'pages' => 300 }
puts data['title']
AIt returns nil because 'title' is a symbol key, not a string key
BIt raises a syntax error due to mixed key types
CIt prints 'Book' correctly
DIt raises a runtime error because keys must be all symbols or all strings
Step-by-Step Solution
Solution:
  1. Step 1: Identify key types

    The hash has a symbol key :title and a string key 'pages'.
  2. Step 2: Access with string key

    Accessing data['title'] looks for a string key 'title', but the key is a symbol :title.
  3. Final Answer:

    It returns nil because 'title' is a symbol key, not a string key -> Option A
  4. Quick Check:

    Accessing with mismatched key types returns nil [OK]
Quick Trick: Symbol and string keys are distinct in hashes [OK]
Common Mistakes:
MISTAKES
  • Assuming symbol and string keys are interchangeable
  • Expecting a syntax or runtime error for mixed keys

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes