0
0
Kotlinprogramming~10 mins

Observable property delegation 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 an observable property with initial value 10.

Kotlin
var score: Int by [1](10) { prop, old, new ->
    println("Score changed from $old to $new")
}
Drag options to blanks, or click blank then click option'
ADelegates.observable
Blazy
Cval
Dlateinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'lazy' instead of 'observable' delegate.
Trying to declare the property as 'val' which is immutable.
Using 'lateinit' which is not a delegate.
2fill in blank
medium

Complete the code to print the old and new values when the property changes.

Kotlin
var name: String by Delegates.observable("John") { [1], old, new ->
    println("Name changed from $old to $new")
}
Drag options to blanks, or click blank then click option'
Aproperty
Bprop
Cthis
Dobj
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'this' which refers to the enclosing class, not the property.
Using 'obj' which is not a standard parameter name here.
3fill in blank
hard

Fix the error in the observable delegate usage.

Kotlin
var age: Int by Delegates.observable([1]) { prop, old, new ->
    println("Age changed from $old to $new")
}
Drag options to blanks, or click blank then click option'
Anull
B"25"
C25
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string "25" instead of integer 25.
Using null which is not allowed here.
4fill in blank
hard

Fill both blanks to create an observable property that prints a message only when the new value is greater than the old value.

Kotlin
var level: Int by Delegates.observable(1) { prop, old, new ->
    if (new [1] old) {
        println("Level increased to $new")
    }
}
Drag options to blanks, or click blank then click option'
A>
B<
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' which triggers on decrease.
Using '==' which triggers only on equality.
5fill in blank
hard

Fill the two blanks to create an observable property that prints the change only when the new value is different from the old value.

Kotlin
var status: String by Delegates.observable("idle") { [1], old, new ->
    if (new [2] old) {
        println("Status changed from $old to $new")
    }
}
Drag options to blanks, or click blank then click option'
Aprop
B!=
C==
Dproperty
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' causing print on no change.
Using wrong parameter names causing errors.