Complete the code to define a custom getter that returns the name in uppercase.
class Person(val name: String) { val upperName: String get() = [1] }
The custom getter returns the name in uppercase using uppercase().
Complete the code to define a custom setter that sets the age only if it is positive.
class Person { var age: Int = 0 set(value) { if ([1]) field = value } }
The setter checks if the new value is greater than zero before setting it.
Fix the error in the custom setter to avoid infinite recursion.
class Rectangle { var width: Int = 0 set(value) { [1] = value } }
Use field inside the setter to assign the backing field and avoid recursion.
Fill both blanks to create a custom getter that returns the area of the rectangle.
class Rectangle(val height: Int, val width: Int) { val area: Int get() = [1] * [2] }
The getter multiplies height and width to calculate the area.
Fill all three blanks to create a property with a custom getter and setter that keeps the score between 0 and 100.
class Game { var score: Int = 0 get() = [1] set(value) { field = when { value < 0 -> [2] value > 100 -> [3] else -> value } } }
The getter returns the backing field. The setter clamps the score between 0 and 100.