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?
✗ Incorrect
mapOf creates an immutable map that cannot be changed after creation.How do you add a new key-value pair to a mutable map in Kotlin?
✗ Incorrect
You add or update entries in a mutable map using the bracket syntax:
mutableMap["key"] = value.What type of map does
mutableMapOf create?✗ Incorrect
mutableMapOf creates a mutable map that can be changed after creation.Which of these is a correct way to create a map with keys "x" and "y" and values 10 and 20?
✗ Incorrect
Use the
to keyword to pair keys and values: mapOf("x" to 10, "y" to 20).What will happen if you try to do
map["newKey"] = 5 on a map created with mapOf?✗ Incorrect
Maps created with
mapOf are immutable, so trying to add or update entries causes a compile-time error.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.