0
0
Kotlinprogramming~5 mins

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

Choose your learning style9 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.