Challenge - 5 Problems
Map Delegation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of map-backed delegated properties with default values
What is the output of this Kotlin code using map-backed delegated properties?
Kotlin
class User(val map: Map<String, Any?>) { val name: String by map val age: Int by map } fun main() { val user = User(mapOf("name" to "Alice", "age" to 30)) println("Name: ${user.name}, Age: ${user.age}") }
Attempts:
2 left
💡 Hint
The delegated properties read values from the map by their keys.
✗ Incorrect
The properties 'name' and 'age' are delegated to the map. The map contains both keys with correct types, so the values are retrieved and printed.
❓ Predict Output
intermediate2:00remaining
Behavior when map key is missing in map-backed delegated properties
What happens when a delegated property tries to access a missing key in the map?
Kotlin
class User(val map: Map<String, Any?>) { val name: String by map val age: Int by map } fun main() { val user = User(mapOf("name" to "Bob")) println("Name: ${user.name}, Age: ${user.age}") }
Attempts:
2 left
💡 Hint
Accessing a missing key in the map throws an exception when using map delegation.
✗ Incorrect
The delegated property 'age' tries to get a value from the map but the key 'age' is missing, causing a NoSuchElementException at runtime.
🔧 Debug
advanced2:00remaining
Identify the runtime error in map-backed delegated properties
What runtime error occurs when running this Kotlin code?
Kotlin
class User(val map: Map<String, Any?>) { val name: String by map val age: Int by map } fun main() { val user = User(mapOf("name" to "Charlie", "age" to "thirty")) println("Name: ${user.name}, Age: ${user.age}") }
Attempts:
2 left
💡 Hint
The map contains a String for 'age' but the property expects an Int.
✗ Incorrect
The property 'age' expects an Int but the map provides a String, causing a ClassCastException at runtime.
📝 Syntax
advanced2:00remaining
Correct syntax for mutable map-backed delegated properties
Which option correctly declares mutable map-backed delegated properties in Kotlin?
Kotlin
class User(val map: MutableMap<String, Any?>) { var name: String by map var age: Int by map }
Attempts:
2 left
💡 Hint
Mutable properties require var and a mutable map for delegation.
✗ Incorrect
Using 'var' with a MutableMap allows the delegated properties to be mutable and backed by the map. Using 'val' or immutable maps does not allow mutation.
🚀 Application
expert2:00remaining
How many entries are in the map after property mutation?
Given this Kotlin code, how many entries does the map contain after the mutation?
Kotlin
fun main() { val data = mutableMapOf<String, Any?>("name" to "Dana", "age" to 25) class User(val map: MutableMap<String, Any?>) { var name: String by map var age: Int by map } val user = User(data) user.age = 26 user.name = "Dana S." println(data.size) }
Attempts:
2 left
💡 Hint
Mutating delegated properties updates existing map entries, not adding new ones.
✗ Incorrect
The map initially has 2 entries. Changing values via delegated properties updates those entries without adding or removing keys, so size remains 2.