Recall & Review
beginner
What is a custom delegated property in Kotlin?
A custom delegated property in Kotlin is a way to define how a property’s getter and setter work by delegating them to another object. This lets you control property behavior without writing boilerplate code in every class.
Click to reveal answer
intermediate
Which operator functions must a delegate class implement for a var property?For a var property, the delegate class must implement <code>operator fun getValue(thisRef: Any?, property: KProperty<*>): T</code> and <code>operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T)</code>.Click to reveal answer
beginner
How do you declare a property using a custom delegate in Kotlin?
You declare it using the
by keyword followed by an instance of the delegate class. For example: var myProp by MyDelegate().Click to reveal answer
intermediate
What parameters do the
getValue and setValue operator functions receive?getValue receives two parameters: thisRef (the object containing the property) and property (metadata about the property). setValue receives three parameters: thisRef, property, and the new value to set.Click to reveal answer
beginner
Why use custom delegated properties instead of normal properties?
Custom delegated properties let you reuse property logic across classes, add extra behavior like validation or lazy loading, and keep your code clean by separating concerns.Click to reveal answer
Which keyword is used to delegate a property to a custom delegate in Kotlin?
✗ Incorrect
The
by keyword is used to delegate a property to a custom delegate instance.What must a delegate class implement to support a read-only property?
✗ Incorrect
A read-only property requires only
getValue operator function.In the operator function
getValue(thisRef, property), what is property?✗ Incorrect
property provides metadata like the property’s name and type.What is a common use case for custom delegated properties?
✗ Incorrect
Custom delegates often add behaviors like validation or lazy loading to properties.
Which of these is NOT a parameter of the
setValue operator function?✗ Incorrect
setValue receives thisRef, property, and value, but not delegate.Explain how to create and use a custom delegated property in Kotlin.
Think about how Kotlin lets you move property logic outside the class.
You got /4 concepts.
What are the benefits of using custom delegated properties compared to normal properties?
Consider how delegation helps keep your code DRY and organized.
You got /4 concepts.