Bird
Raised Fist0
Kotlinprogramming~5 mins

Map creation (mapOf, mutableMapOf) in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does mapOf create in Kotlin?
mapOf creates an immutable map, meaning you cannot add or remove entries after creation.
Click to reveal answer
beginner
What is the difference between mapOf and mutableMapOf?
mapOf creates an immutable map, while mutableMapOf creates a mutable map that allows adding, removing, or changing entries.
Click to reveal answer
beginner
How do you create a map with keys "apple" and "banana" and values 1 and 2 using mapOf?
Use val fruitMap = mapOf("apple" to 1, "banana" to 2).
Click to reveal answer
beginner
How can you add a new entry to a map created with mutableMapOf?
Use the assignment syntax: mutableMap["key"] = value to add or update entries.
Click to reveal answer
beginner
What happens if you try to add an entry to a map created with mapOf?
You will get a compile-time error because the map is immutable and does not support adding entries.
Click to reveal answer
Which Kotlin function creates a map that you cannot change after creation?
AmapOf
BmutableMapOf
ChashMapOf
DarrayMapOf
How do you add a new key-value pair to a mutable map in Kotlin?
AmutableMap.add("key", value)
BmutableMap.put("key", value)
CmutableMap["key"] = value
DmutableMap.insert("key", value)
What type of map does mutableMapOf create?
AImmutable map
BMutable map
CSorted map
DLinked map
Which of these is a correct way to create a map with keys "x" and "y" and values 10 and 20?
AmutableMapOf("x" -> 10, "y" -> 20)
BmapOf("x", 10, "y", 20)
CmutableMapOf("x" = 10, "y" = 20)
DmapOf("x" to 10, "y" to 20)
What will happen if you try to do map["newKey"] = 5 on a map created with mapOf?
AIt will cause a compile-time error
BIt will add the new key-value pair
CIt will update the existing key
DIt will silently fail
Explain the difference between mapOf and mutableMapOf in Kotlin.
Think about whether you can change the map after creating it.
You got /4 concepts.
    How do you create a mutable map and add a new key-value pair to it?
    Remember the syntax for adding entries to a mutable map.
    You got /3 concepts.

      Practice

      (1/5)
      1. What does mapOf create in Kotlin?
      easy
      A. A list of values without keys
      B. A mutable map that can be changed later
      C. A fixed map that cannot be changed after creation
      D. An empty map with no keys or values

      Solution

      1. Step 1: Understand mapOf purpose

        mapOf creates a map with key-value pairs that cannot be modified after creation.
      2. Step 2: Compare with other map types

        mutableMapOf allows changes, but mapOf does not.
      3. Final Answer:

        A fixed map that cannot be changed after creation -> Option C
      4. Quick Check:

        mapOf = fixed map [OK]
      Hint: Remember: mapOf = fixed map, mutableMapOf = changeable map [OK]
      Common Mistakes:
      • Confusing mapOf with mutableMapOf
      • Thinking mapOf creates an empty map
      • Assuming mapOf allows adding new entries
      2. 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
      A. val map = mutableMapOf("a" to 1, "b" to 2)
      B. val map = mapOf("a" -> 1, "b" -> 2)
      C. val map = mutableMapOf("a" = 1, "b" = 2)
      D. val map = mapOf("a" to 1, "b" to 2)

      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]
      Hint: Use mutableMapOf with "key" to value pairs [OK]
      Common Mistakes:
      • Using -> instead of to for pairs
      • Using = inside mapOf or mutableMapOf
      • Confusing mapOf and mutableMapOf
      3. What is the output of this Kotlin code?
      val map = mapOf("x" to 10, "y" to 20)
      println(map["x"])
      println(map["z"])
      medium
      A. 20 null
      B. 10 null
      C. null null
      D. 10 0

      Solution

      1. Step 1: Access existing key "x"

        map["x"] returns 10 because "x" is a key with value 10.
      2. Step 2: Access non-existing key "z"

        map["z"] returns null because "z" is not in the map.
      3. Final Answer:

        10\nnull -> Option B
      4. Quick Check:

        Existing key returns value, missing key returns null [OK]
      Hint: map[key] returns value or null if key missing [OK]
      Common Mistakes:
      • Expecting 0 instead of null for missing key
      • Confusing values of different keys
      • Assuming map is mutable and can add keys
      4. What is wrong with this Kotlin code?
      val map = mapOf("a" to 1)
      map["b"] = 2
      medium
      A. mapOf creates an immutable map, so you cannot add new entries
      B. The syntax for adding entries is incorrect; should use put()
      C. Keys must be integers, not strings
      D. mapOf requires mutableMapOf to add entries

      Solution

      1. Step 1: Understand mapOf immutability

        mapOf creates an immutable map that cannot be changed after creation.
      2. Step 2: Adding entries to immutable map causes error

        Trying to assign a new key-value pair with map["b"] = 2 causes a compile-time error because the map is immutable.
      3. Final Answer:

        mapOf creates an immutable map, so you cannot add new entries -> Option A
      4. Quick Check:

        Immutable map cannot be changed [OK]
      Hint: Use mutableMapOf to add or change entries [OK]
      Common Mistakes:
      • Trying to add entries to mapOf
      • Using wrong syntax for adding entries
      • Confusing mutableMapOf and mapOf
      5. You want to create a map that stores student names as keys and their scores as values. You also want to update scores later. Which Kotlin code snippet correctly achieves this?
      hard
      A. val scores = mutableMapOf("Alice" to 90, "Bob" to 85) scores.add("Alice", 95)
      B. val scores = mapOf("Alice" to 90, "Bob" to 85) scores["Alice"] = 95
      C. val scores = mapOf("Alice" to 90, "Bob" to 85) scores.put("Alice", 95)
      D. val scores = mutableMapOf("Alice" to 90, "Bob" to 85) scores["Alice"] = 95

      Solution

      1. Step 1: Choose a mutable map to allow updates

        Since scores need to be updated, mutableMapOf is required.
      2. Step 2: Use correct method to update value

        Assigning with scores["Alice"] = 95 correctly updates the value in a mutable map.
      3. Step 3: Check other options for errors

        val scores = mapOf("Alice" to 90, "Bob" to 85) scores["Alice"] = 95 uses immutable map and tries to update, causing error. val scores = mapOf("Alice" to 90, "Bob" to 85) scores.put("Alice", 95) tries to use put on immutable map, error. val scores = mutableMapOf("Alice" to 90, "Bob" to 85) scores.add("Alice", 95) uses non-existent add method.
      4. Final Answer:

        val scores = mutableMapOf("Alice" to 90, "Bob" to 85) scores["Alice"] = 95 -> Option D
      5. Quick Check:

        mutableMapOf + assignment updates map [OK]
      Hint: Use mutableMapOf and bracket assignment to update entries [OK]
      Common Mistakes:
      • Using mapOf when updates are needed
      • Trying to use put on immutable map
      • Using add method which does not exist
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard