Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q5 of 15
Ruby - Hashes
What will be the output of this Ruby code?
h = {a: 1, b: 2}
h[:b] = 5
puts h[:b]
A2
B5
Cnil
DError
Step-by-Step Solution
Solution:
  1. Step 1: Understand hash value update

    Assigning h[:b] = 5 updates the value of key :b from 2 to 5.
  2. Step 2: Output the updated value

    puts h[:b] prints 5.
  3. Final Answer:

    5 -> Option B
  4. Quick Check:

    Updated hash value = 5 [OK]
Quick Trick: Assigning to hash key updates its value [OK]
Common Mistakes:
MISTAKES
  • Expecting old value after assignment
  • Confusing key names
  • Assuming nil if key exists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes