Bird
0
0

What is the output of the following Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Hashes
What is the output of the following Ruby code?
person = { name: "Carol", "age" => 28 }
puts person[:name]
puts person["age"]
ACarol 28
BCarol
C 28
Dnil 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". Accessing with matching key types returns values.
  2. Step 2: Evaluate each puts statement

    person[:name] returns "Carol". person["age"] returns 28. Both print on separate lines.
  3. Final Answer:

    Carol 28 -> Option A
  4. Quick Check:

    Match key types to get values = Carol 28 [OK]
Quick Trick: Use matching symbol or string keys to access hash values [OK]
Common Mistakes:
  • Accessing string key with symbol or vice versa
  • Expecting mixed keys to be interchangeable
  • Forgetting keys are type-sensitive

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes