Challenge - 5 Problems
Observable Master
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 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" }
Attempts:
2 left
💡 Hint
Think about how observable delegation calls the callback on every property change.
✗ Incorrect
The observable delegate calls the lambda every time the property changes. Initially, the property is 'Unknown'. When set to 'Alice', the lambda prints the change from 'Unknown' to 'Alice'. Then when set to 'Bob', it prints the change from 'Alice' to 'Bob'.
🧠 Conceptual
intermediate1:30remaining
What is the main purpose of observable property delegation in Kotlin?
Why would a Kotlin developer use observable property delegation?
Attempts:
2 left
💡 Hint
Think about what happens when a property changes.
✗ Incorrect
Observable property delegation allows running custom code whenever the property's value changes, such as logging or validation.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Think about what happens when the property is changed inside its own observer.
✗ Incorrect
The observable callback sets the property 'count' again, which triggers the callback again, causing infinite recursion and stack overflow.
📝 Syntax
advanced1: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.
Attempts:
2 left
💡 Hint
Remember the correct syntax for property delegation uses 'by' and the delegate object.
✗ Incorrect
Option B correctly uses 'var' with 'by Delegates.observable' and a lambda for the callback. Option B assigns the delegate directly to the property which is invalid. Option B uses 'val' which cannot be delegated with observable since it must be mutable. Option B uses 'observable' without 'Delegates.' which is undefined.
🚀 Application
expert2: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 }
Attempts:
2 left
💡 Hint
Consider if setting the same value triggers the observable callback.
✗ Incorrect
The observable callback runs only when the property value actually changes. Setting the same value (25 to 25) does not trigger it. So it runs when changing from 20 to 25 and from 25 to 30, total 2 times.