Bird
0
0

Why does mapOf("a" to 1, "a" to 2) contain only one entry for key "a"?

hard📝 Conceptual Q10 of 15
Kotlin - Collections Fundamentals
Why does mapOf("a" to 1, "a" to 2) contain only one entry for key "a"?
AmapOf keeps both entries as a list
BmapOf throws an error on duplicate keys
CDuplicate keys cause the map to be empty
DDuplicate keys are overwritten, last value wins
Step-by-Step Solution
Solution:
  1. Step 1: Understand duplicate key handling

    In Kotlin maps, duplicate keys are not allowed; later entries overwrite earlier ones.
  2. Step 2: Confirm behavior of mapOf

    mapOf silently keeps the last value for duplicate keys.
  3. Final Answer:

    Duplicate keys are overwritten, last value wins -> Option D
  4. Quick Check:

    Duplicate keys overwrite previous values = C [OK]
Quick Trick: Last duplicate key value overwrites previous in mapOf [OK]
Common Mistakes:
MISTAKES
  • Expecting error on duplicates
  • Thinking duplicates are stored as lists

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes