Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Hashes
What will be the output of this Ruby code?
person = { name: 'Bob', 'age' => 25 }
puts person[:name]
puts person['age']
puts person['name']
ABob 25 Bob
Bnil 25 Bob
CBob 25
DBob 25 nil
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash keys and access

    The hash has a symbol key :name and a string key 'age'. Access must match key type exactly.
  2. Step 2: Evaluate each puts statement

    person[:name] returns 'Bob'; person['age'] returns 25; person['name'] returns nil because 'name' key as string does not exist.
  3. Final Answer:

    Bob 25 nil -> Option D
  4. Quick Check:

    Key type must match to access value [OK]
Quick Trick: Access hash values with exact key type used [OK]
Common Mistakes:
  • Assuming symbol and string keys are interchangeable
  • Expecting 'name' string key to exist when only :name symbol key is present
  • Confusing output nil with empty string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes