0
0
Kotlinprogramming~20 mins

Map creation (mapOf, mutableMapOf) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code?
Consider the following Kotlin code snippet. What will be printed when it runs?
Kotlin
val map = mapOf("a" to 1, "b" to 2, "c" to 3)
println(map["b"])
Anull
Bb
C2
D1
Attempts:
2 left
💡 Hint
Look at how the map is created and how the value is accessed by key.
Predict Output
intermediate
2:00remaining
What happens when you modify a mutable map?
What will be the output of this Kotlin code?
Kotlin
val mutableMap = mutableMapOf("x" to 10, "y" to 20)
mutableMap["x"] = 15
println(mutableMap["x"])
A15
Bnull
C20
D10
Attempts:
2 left
💡 Hint
mutableMapOf allows changing values after creation.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with mapOf and null key?
Analyze this Kotlin code and select the output it produces.
Kotlin
val map = mapOf(null to "empty", "key" to "value")
println(map[null])
Aempty
Bnull
Cvalue
DCompilation error
Attempts:
2 left
💡 Hint
Kotlin maps can have null keys unless specified otherwise.
Predict Output
advanced
2:00remaining
What error does this Kotlin code raise?
What error will this Kotlin code produce when run?
Kotlin
val map = mapOf("a" to 1, "b" to 2)
map["a"] = 3
AUnsupportedOperationException
BUnresolved reference: set
CNo error, prints 3
DNullPointerException
Attempts:
2 left
💡 Hint
mapOf creates an immutable map, so you cannot change values.
🧠 Conceptual
expert
2:00remaining
How many entries does this Kotlin map contain?
Given the following Kotlin code, how many key-value pairs does the map contain?
Kotlin
val map = mutableMapOf("one" to 1, "two" to 2)
map.put("three", 3)
map["two"] = 22
map.remove("one")
A3
B4
C1
D2
Attempts:
2 left
💡 Hint
Consider all insertions, updates, and removals carefully.