Challenge - 5 Problems
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"])
Attempts:
2 left
💡 Hint
Look at how the map is created and how the value is accessed by key.
✗ Incorrect
The map contains keys "a", "b", and "c" with values 1, 2, and 3 respectively. Accessing map["b"] returns 2.
❓ Predict Output
intermediate2: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"])
Attempts:
2 left
💡 Hint
mutableMapOf allows changing values after creation.
✗ Incorrect
The mutable map allows updating the value for key "x" from 10 to 15, so printing mutableMap["x"] outputs 15.
❓ Predict Output
advanced2: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])
Attempts:
2 left
💡 Hint
Kotlin maps can have null keys unless specified otherwise.
✗ Incorrect
The map contains a null key with value "empty". Accessing map[null] returns "empty".
❓ Predict Output
advanced2: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
Attempts:
2 left
💡 Hint
mapOf creates an immutable map, so you cannot change values.
✗ Incorrect
Trying to assign a value to a key in an immutable map results in a compilation error: Unresolved reference: set.
🧠 Conceptual
expert2: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")
Attempts:
2 left
💡 Hint
Consider all insertions, updates, and removals carefully.
✗ Incorrect
Initially, the map has 2 entries. Adding "three" to 3 makes 3 entries. Updating "two" does not change count. Removing "one" leaves 2 entries.