Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Hashes
What will this Ruby code print?
h = { :x => 10, "x" => 20 }
puts h[:x] + h["x"]
A10
BError
C20
D30
Step-by-Step Solution
Solution:
  1. Step 1: Identify values for keys

    h[:x] is 10 and h["x"] is 20 because symbol and string keys are different.
  2. Step 2: Add the values

    Adding 10 + 20 results in 30.
  3. Final Answer:

    30 -> Option D
  4. Quick Check:

    Symbol and string keys store separate values [OK]
Quick Trick: Symbol and string keys are distinct in hashes [OK]
Common Mistakes:
MISTAKES
  • Assuming symbol and string keys are same
  • Expecting error due to key duplication
  • Adding keys instead of values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes