Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Hashes

What is the output of this Ruby code?

a = {a: 1, b: 2}
b = {b: 3, c: 4}
c = a.merge(b)
puts c
A{:a=>1, :b=>2, :c=>4}
B{:a=>1, :b=>3}
C{:a=>1, :b=>3, :c=>4}
D{:b=>3, :c=>4}
Step-by-Step Solution
Solution:
  1. Step 1: Understand merge behavior with overlapping keys

    merge returns a new hash combining keys; overlapping keys get values from the second hash.
  2. Step 2: Apply to given hashes

    Key :b is in both hashes; value from b (3) replaces value from a (2). Keys :a and :c are included as is.
  3. Final Answer:

    {:a=>1, :b=>3, :c=>4} -> Option C
  4. Quick Check:

    Overlapping keys replaced by second hash [OK]
Quick Trick: Overlapping keys take second hash's value in merge [OK]
Common Mistakes:
  • Assuming original hash is changed by merge
  • Not replacing overlapping key values
  • Missing keys from either hash

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes