Bird
0
0

Given the code below, what will be the output?

medium📝 Predict Output Q5 of 15
Ruby - Hashes
Given the code below, what will be the output?
h = { :city => 'Paris', 'country' => 'France' }
puts h[:country]
puts h['city']
AParis France
Bnil nil
C nil
DFrance Paris
Step-by-Step Solution
Solution:
  1. Step 1: Access :country key

    The hash has a string key 'country', but h[:country] looks for a symbol key :country, which does not exist, so returns nil.
  2. Step 2: Access 'city' key

    The hash has a symbol key :city, but h['city'] looks for a string key 'city', which does not exist, so returns nil.
  3. Final Answer:

    nil\nnil -> Option B
  4. Quick Check:

    Symbol vs string key mismatch returns nil = B [OK]
Quick Trick: Access hash keys with exact symbol or string type [OK]
Common Mistakes:
  • Assuming symbol and string keys are interchangeable
  • Expecting automatic key conversion
  • Confusing key types in hash access

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes