0
0
Kotlinprogramming~20 mins

Map-backed delegated properties in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Delegation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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}")
}
ACompilation error due to missing property
BName: Alice, Age: 30
CName: null, Age: 30
DName: Alice, Age: null
Attempts:
2 left
💡 Hint
The delegated properties read values from the map by their keys.
Predict Output
intermediate
2: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}")
}
AThrows NoSuchElementException at runtime
BName: Bob, Age: null
CCompilation error due to missing key
DName: Bob, Age: 0
Attempts:
2 left
💡 Hint
Accessing a missing key in the map throws an exception when using map delegation.
🔧 Debug
advanced
2: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}")
}
ANoSuchElementException due to missing key
BNullPointerException due to null value
CClassCastException due to wrong type for 'age'
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
The map contains a String for 'age' but the property expects an Int.
📝 Syntax
advanced
2: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
}
A
val name: String by mutableMapOf()
val age: Int by mutableMapOf()
B
val name: String by map
val age: Int by map
C
var name: String by map.toMap()
var age: Int by map.toMap()
D
var name: String by map
var age: Int by map
Attempts:
2 left
💡 Hint
Mutable properties require var and a mutable map for delegation.
🚀 Application
expert
2: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)
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Mutating delegated properties updates existing map entries, not adding new ones.