0
0
Kotlinprogramming~20 mins

Observable property delegation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observable Master
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 using observable delegation?
Consider the following Kotlin code that uses observable property delegation. What will be printed when the code runs?
Kotlin
import kotlin.properties.Delegates

class User {
    var name: String by Delegates.observable("Unknown") { prop, old, new ->
        println("Property '${prop.name}' changed from '$old' to '$new'")
    }
}

fun main() {
    val user = User()
    user.name = "Alice"
    user.name = "Bob"
}
AProperty 'name' changed from 'Unknown' to 'Bob'
BProperty 'name' changed from 'Alice' to 'Bob'
C
Property 'name' changed from 'Unknown' to 'Alice'
Property 'name' changed from 'Alice' to 'Bob'
DNo output
Attempts:
2 left
💡 Hint
Think about how observable delegation calls the callback on every property change.
🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of observable property delegation in Kotlin?
Why would a Kotlin developer use observable property delegation?
ATo automatically run code whenever a property value changes
BTo make a property immutable
CTo delay the initialization of a property until first use
DTo create a property that cannot be accessed outside the class
Attempts:
2 left
💡 Hint
Think about what happens when a property changes.
🔧 Debug
advanced
2:30remaining
Why does this Kotlin code using observable delegation cause a stack overflow?
Examine the code below. Why does it cause a stack overflow error when run?
Kotlin
import kotlin.properties.Delegates

class Counter {
    var count: Int by Delegates.observable(0) { _, _, new ->
        if (new < 10) {
            count = new + 1
        }
    }
}

fun main() {
    val counter = Counter()
    counter.count = 1
    println(counter.count)
}
ABecause the println statement is inside the observable callback
BBecause the initial value 0 is invalid for observable properties
CBecause the property count is immutable and cannot be changed
DBecause the observable callback modifies the property it observes, causing infinite recursion
Attempts:
2 left
💡 Hint
Think about what happens when the property is changed inside its own observer.
📝 Syntax
advanced
1:30remaining
Which option correctly declares an observable property in Kotlin?
Select the correct Kotlin syntax to declare an observable property 'score' initialized to 0 that prints changes.
Avar score: Int = Delegates.observable(0) { prop, old, new -> println("Score changed from ${old} to ${new}") }
Bvar score: Int by Delegates.observable(0) { prop, old, new -> println("Score changed from ${old} to ${new}") }
Cval score: Int by Delegates.observable(0) { prop, old, new -> println("Score changed from ${old} to ${new}") }
Dvar score: Int by observable(0) { prop, old, new -> println("Score changed from ${old} to ${new}") }
Attempts:
2 left
💡 Hint
Remember the correct syntax for property delegation uses 'by' and the delegate object.
🚀 Application
expert
2:30remaining
How many times will the observable callback run in this Kotlin code?
Given the code below, how many times will the observable callback print a message?
Kotlin
import kotlin.properties.Delegates

class Temperature {
    var celsius: Int by Delegates.observable(20) { _, old, new ->
        println("Temperature changed from ${old} to ${new}")
    }
}

fun main() {
    val temp = Temperature()
    temp.celsius = 25
    temp.celsius = 25
    temp.celsius = 30
}
A2
B1
C0
D3
Attempts:
2 left
💡 Hint
Consider if setting the same value triggers the observable callback.