0
0
Kotlinprogramming~20 mins

Custom getters and setters in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Custom Getters and Setters 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 with a custom getter?

Look at this Kotlin class with a custom getter. What will be printed when println(box.area) runs?

Kotlin
class Box(val width: Int, val height: Int) {
    val area: Int
        get() = width * height
}

fun main() {
    val box = Box(3, 4)
    println(box.area)
}
A12
B7
C0
DCompilation error
Attempts:
2 left
💡 Hint

Remember the getter calculates the area by multiplying width and height.

Predict Output
intermediate
2:00remaining
What happens when setting a property with a custom setter?

Consider this Kotlin class with a custom setter. What will be printed after person.age = 25 and println(person.age)?

Kotlin
class Person {
    var age: Int = 0
        set(value) {
            field = if (value >= 0) value else 0
        }
}

fun main() {
    val person = Person()
    person.age = 25
    println(person.age)
}
A-25
B0
CCompilation error
D25
Attempts:
2 left
💡 Hint

The setter sets the field only if the value is non-negative.

🔧 Debug
advanced
2:30remaining
Why does this Kotlin code cause a stack overflow error?

Examine this Kotlin class. Why does setting counter.value = 5 cause a stack overflow?

Kotlin
class Counter {
    var value: Int = 0
        set(value) {
            field = value
        }
}

fun main() {
    val counter = Counter()
    counter.value = 5
    println(counter.value)
}
AThe code has a syntax error due to missing 'field' keyword.
BThe property 'value' is immutable and cannot be set.
CThe setter recursively calls itself because it assigns to 'value' instead of 'field'.
DThe getter is missing, causing infinite recursion.
Attempts:
2 left
💡 Hint

Check what happens when the setter assigns to 'value' instead of 'field'.

🧠 Conceptual
advanced
1:30remaining
What is the purpose of backing fields in Kotlin custom setters/getters?

Why do Kotlin custom setters and getters use the field keyword inside their bodies?

ATo call the superclass implementation of the property.
BTo refer to the actual storage of the property and avoid recursive calls.
CTo declare a new local variable inside the setter or getter.
DTo make the property immutable.
Attempts:
2 left
💡 Hint

Think about what happens if you assign to the property name inside its setter.

Predict Output
expert
2:30remaining
What is the output of this Kotlin code with a custom setter modifying input?

Analyze this Kotlin code. What will be printed after account.balance = -100 and println(account.balance)?

Kotlin
class BankAccount {
    var balance: Int = 0
        set(value) {
            field = if (value < 0) 0 else value
        }
}

fun main() {
    val account = BankAccount()
    account.balance = -100
    println(account.balance)
}
A0
B100
CCompilation error
D-100
Attempts:
2 left
💡 Hint

The setter changes negative values to zero.