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?
Adelegate
Bby
Cwith
Dusing
✗ 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?
Aoperator fun getValue(...)
Boperator fun setValue(...)
Coperator fun getValue(...) and setValue(...)
DNo operator functions needed
✗ Incorrect
A read-only property requires only getValue operator function.
In the operator function getValue(thisRef, property), what is property?
AThe delegate instance
BThe property’s value
CThe property’s metadata
DThe class name
✗ Incorrect
property provides metadata like the property’s name and type.
What is a common use case for custom delegated properties?
ATo create new classes automatically
BTo replace all functions in a class
CTo change Kotlin syntax
DTo add validation logic when setting a property
✗ 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?
Adelegate
Bproperty
Cvalue
DthisRef
✗ 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.
Practice
(1/5)
1. What is the main purpose of using custom delegated properties in Kotlin?
easy
A. To define extension functions for a class
B. To create multiple constructors for a class
C. To control how a property's value is stored and retrieved
D. To override the default toString() method
Solution
Step 1: Understand property delegation concept
Delegated properties let you customize how a property's value is handled behind the scenes.
Step 2: Identify the purpose of custom delegation
Custom delegated properties specifically allow control over getting and setting property values.
Final Answer:
To control how a property's value is stored and retrieved -> Option C
2. Which of the following is the correct syntax to declare a custom delegated property in Kotlin?
easy
A. val prop = Delegate()
B. val prop by Delegate()
C. val prop: Delegate by this
D. val prop delegate Delegate()
Solution
Step 1: Recall Kotlin delegation syntax
Delegated properties use the by keyword followed by a delegate instance.
Step 2: Match correct syntax
val prop by Delegate() correctly declares a delegated property.
Final Answer:
val prop by Delegate() -> Option B
Quick Check:
Delegation uses 'by' keyword [OK]
Hint: Look for 'by' keyword with delegate instance [OK]
Common Mistakes:
Using '=' instead of 'by' for delegation
Incorrect delegate type declaration
Missing parentheses for delegate constructor
3. What will be the output of the following Kotlin code?
class Delegate {
operator fun getValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>): String {
return "Hello from delegate"
}
}
class Example {
val message: String by Delegate()
}
fun main() {
val e = Example()
println(e.message)
}
medium
A. Hello from delegate
B. null
C. Compilation error
D. Hello from Example
Solution
Step 1: Understand getValue operator
The getValue function returns the string "Hello from delegate" when the property is accessed.
Step 2: Trace property access in main()
Accessing e.message calls getValue and prints the returned string.
Final Answer:
Hello from delegate -> Option A
Quick Check:
Delegate getValue returns string [OK]
Hint: getValue defines returned property value [OK]
Common Mistakes:
Expecting default property value instead of delegate's
Confusing getValue with setValue
Assuming compilation error due to missing parameters
4. Identify the error in this Kotlin code using a custom delegated property:
class Delegate {
operator fun getValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>): Int {
return 42
}
}
class Example {
var number: Int by Delegate()
}
fun main() {
val e = Example()
e.number = 10
println(e.number)
}
medium
A. Delegate class must be an interface
B. Property number should be val, not var
C. getValue must return String, not Int
D. Delegate class lacks setValue operator function
Solution
Step 1: Check property mutability and delegation
The property number is var, so delegate must support both get and set.
Step 2: Verify delegate methods
The Delegate class only defines getValue, missing setValue needed for var.
Final Answer:
Delegate class lacks setValue operator function -> Option D
Quick Check:
var needs getValue and setValue [OK]
Hint: var properties need both getValue and setValue [OK]
Common Mistakes:
Forgetting setValue for var properties
Assuming getValue return type must be String
Thinking delegate must be interface
5. You want to create a custom delegated property that logs every time the property is read or written. Which of the following delegate implementations correctly achieves this?
class LoggingDelegate<T>(var value: T) {
operator fun getValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>): T {
println("Getting value of ${property.name}")
return value
}
operator fun setValue(thisRef: Any?, property: kotlin.reflect.KProperty<*>, newValue: T) {
println("Setting value of ${property.name} to $newValue")
value = newValue
}
}
class Example {
var data: String by LoggingDelegate("initial")
}
fun main() {
val e = Example()
e.data = "new"
println(e.data)
}
hard
A. Logs on get and set, prints 'Setting value of data to new' then 'Getting value of data' and 'new'
B. Only logs on get, no output on set
C. Throws error because delegate class is not generic
D. Does not compile due to missing constructor parameters
Solution
Step 1: Analyze delegate methods
The delegate defines getValue and setValue with print statements logging property access.
Step 2: Trace main function output
Setting e.data = "new" prints setting log, then printing e.data triggers get log and prints value.
Final Answer:
Logs on get and set, prints 'Setting value of data to new' then 'Getting value of data' and 'new' -> Option A
Quick Check:
Delegate logs on get/set correctly [OK]
Hint: Check both getValue and setValue print logs [OK]
Common Mistakes:
Missing setValue causes no set logging
Assuming delegate must be non-generic
Forgetting to pass initial value to delegate
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.