Bird
0
0

Which of the following is the correct syntax to create a mutable map with keys "a" and "b" and values 1 and 2 respectively?

easy📝 Syntax Q12 of 15
Kotlin - Collections Fundamentals
Which of the following is the correct syntax to create a mutable map with keys "a" and "b" and values 1 and 2 respectively?
Aval map = mutableMapOf("a" to 1, "b" to 2)
Bval map = mapOf("a" -> 1, "b" -> 2)
Cval map = mutableMapOf("a" = 1, "b" = 2)
Dval map = mapOf("a" to 1, "b" to 2)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct syntax for mutableMapOf

    The correct syntax uses mutableMapOf with pairs using to keyword.
  2. Step 2: Check each option

    val map = mutableMapOf("a" to 1, "b" to 2) uses mutableMapOf and to correctly. val map = mapOf("a" -> 1, "b" -> 2) uses wrong arrow ->. val map = mutableMapOf("a" = 1, "b" = 2) uses = which is invalid here. val map = mapOf("a" to 1, "b" to 2) uses mapOf which creates an immutable map.
  3. Final Answer:

    val map = mutableMapOf("a" to 1, "b" to 2) -> Option A
  4. Quick Check:

    mutableMapOf + to = correct syntax [OK]
Quick Trick: Use mutableMapOf with "key" to value pairs [OK]
Common Mistakes:
MISTAKES
  • Using -> instead of to for pairs
  • Using = inside mapOf or mutableMapOf
  • Confusing mapOf and mutableMapOf

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes