0
0
Kotlinprogramming~10 mins

Map-backed delegated properties in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a map-backed delegated property for 'name'.

Kotlin
val user = mapOf("name" to "Alice", "age" to 30)
val name: String by [1]
Drag options to blanks, or click blank then click option'
Amap
Buser
CmutableMapOf()
DHashMap()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a new empty map instead of the existing 'user' map.
2fill in blank
medium

Complete the code to access the 'age' property delegated to the map.

Kotlin
val user = mapOf("name" to "Bob", "age" to 25)
val age: Int by [1]
println(age)
Drag options to blanks, or click blank then click option'
Amap
BmutableMapOf()
CHashMap()
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Delegating to a new empty map which has no data.
3fill in blank
hard

Fix the error in the code to correctly delegate 'email' property to the map.

Kotlin
val user = mapOf("email" to "test@example.com")
val email: String by [1]
println(email)
Drag options to blanks, or click blank then click option'
Amap
BmutableMapOf()
Cuser
DHashMap()
Attempts:
3 left
💡 Hint
Common Mistakes
Delegating to a new empty map instead of the existing one.
4fill in blank
hard

Fill both blanks to create a map-backed delegated property 'city' and print its value.

Kotlin
val data = mapOf("city" to "Paris", "country" to "France")
val city: String by [1]
println([2])
Drag options to blanks, or click blank then click option'
Adata
Bcity
Ccountry
DdataMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names for delegation or printing.
5fill in blank
hard

Fill all three blanks to create map-backed delegated properties 'firstName' and 'lastName', then print full name.

Kotlin
val personData = mapOf("firstName" to "John", "lastName" to "Doe")
val firstName: String by [1]
val lastName: String by [2]
println("Full name: $[3] $lastName")
Drag options to blanks, or click blank then click option'
ApersonData
CfirstName
DlastName
Attempts:
3 left
💡 Hint
Common Mistakes
Using different map variables for delegation or printing the map variable instead of the property.