Bird
0
0

What will this Ruby code output?

medium📝 Predict Output Q5 of 15
Ruby - Hashes
What will this Ruby code output?
info = { a: 1, b: 2 }
info[:c] = 3
puts info[:c]
Anil
B2
Cc
D3
Step-by-Step Solution
Solution:
  1. Step 1: Add a new key-value pair to the hash

    info[:c] = 3 adds the key :c with value 3 to the hash.
  2. Step 2: Access and print the value for :c

    puts info[:c] prints 3 because the key :c exists with that value.
  3. Final Answer:

    3 -> Option D
  4. Quick Check:

    Adding key-value pairs updates the hash = B [OK]
Quick Trick: You can add keys anytime: hash[:new_key] = value [OK]
Common Mistakes:
  • Expecting nil because key was not in original hash
  • Confusing key names with values
  • Trying to add keys incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes