Bird
Raised Fist0
Kotlinprogramming~10 mins

Custom getters and setters in Kotlin - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a custom getter that returns the name in uppercase.

Kotlin
class Person(val name: String) {
    val upperName: String
        get() = [1]
}
Drag options to blanks, or click blank then click option'
Aname.lowercase()
Bname.uppercase()
Cname.length
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase() instead of uppercase()
Returning the original name without conversion
2fill in blank
medium

Complete the code to define a custom setter that sets the age only if it is positive.

Kotlin
class Person {
    var age: Int = 0
        set(value) {
            if ([1]) field = value
        }
}
Drag options to blanks, or click blank then click option'
Avalue > 0
Bvalue < 0
Cvalue == 0
Dvalue >= 0
Attempts:
3 left
💡 Hint
Common Mistakes
Using value < 0 which allows negative ages
Using value >= 0 which allows zero age
3fill in blank
hard

Fix the error in the custom setter to avoid infinite recursion.

Kotlin
class Rectangle {
    var width: Int = 0
        set(value) {
            [1] = value
        }
}
Drag options to blanks, or click blank then click option'
Afield
Bwidth
Cthis.width
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to the property name instead of field
Using this.width which also calls the setter
4fill in blank
hard

Fill both blanks to create a custom getter that returns the area of the rectangle.

Kotlin
class Rectangle(val height: Int, val width: Int) {
    val area: Int
        get() = [1] * [2]
}
Drag options to blanks, or click blank then click option'
Aheight
Bwidth
Carea
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using area inside its own getter causing recursion
Using this which is not a number
5fill in blank
hard

Fill all three blanks to create a property with a custom getter and setter that keeps the score between 0 and 100.

Kotlin
class Game {
    var score: Int = 0
        get() = [1]
        set(value) {
            field = when {
                value < 0 -> [2]
                value > 100 -> [3]
                else -> value
            }
        }
}
Drag options to blanks, or click blank then click option'
Afield
B0
C100
Dvalue
Attempts:
3 left
💡 Hint
Common Mistakes
Getter returning value instead of field
Setter not clamping values correctly

Practice

(1/5)
1. What is the main purpose of custom getters and setters in Kotlin?
easy
A. To create new classes automatically
B. To define functions outside a class
C. To control how properties are read and written
D. To import external libraries

Solution

  1. Step 1: Understand property access

    Custom getters and setters allow you to define how a property value is retrieved or changed.
  2. Step 2: Identify their purpose

    They help control and protect the data by customizing read/write behavior.
  3. Final Answer:

    To control how properties are read and written -> Option C
  4. Quick Check:

    Custom getters/setters = control property access [OK]
Hint: Remember: getters/setters control property reading/writing [OK]
Common Mistakes:
  • Confusing getters/setters with class creation
  • Thinking they define functions outside classes
  • Assuming they import libraries
2. Which of the following is the correct syntax to define a custom setter for a Kotlin property age?
easy
A. var age: Int set(value) { field = value }
B. var age: Int get(value) { field = value }
C. val age: Int set(value) { field = value }
D. var age: Int set { value = field }

Solution

  1. Step 1: Check property mutability

    Setter can only be defined for var, not val.
  2. Step 2: Verify setter syntax

    The setter uses set(value) { field = value } to assign the new value.
  3. Final Answer:

    var age: Int set(value) { field = value } -> Option A
  4. Quick Check:

    Setter syntax = set(value) { field = value } [OK]
Hint: Setter needs var and set(value) syntax [OK]
Common Mistakes:
  • Using get instead of set for setter
  • Defining setter on val property
  • Reversing field and value in setter
3. What will be the output of this Kotlin code?
class Person {
    var name: String = ""
        get() = field.uppercase()
        set(value) { field = value.trim() }
}

fun main() {
    val p = Person()
    p.name = "  alice  "
    println(p.name)
}
medium
A. ALICE
B. alice
C. ecila
D. Alice

Solution

  1. Step 1: Analyze setter behavior

    Setter trims the input, so " alice " becomes "alice" stored in field.
  2. Step 2: Analyze getter behavior

    Getter returns the stored value in uppercase, so "alice" becomes "ALICE".
  3. Final Answer:

    ALICE -> Option A
  4. Quick Check:

    Setter trims, getter uppercases = ALICE [OK]
Hint: Setter trims, getter uppercases output [OK]
Common Mistakes:
  • Ignoring trimming in setter
  • Not converting to uppercase in getter
  • Assuming original input prints
4. Identify the error in this Kotlin class with custom getter and setter:
class Box {
    var size: Int = 0
        get() = field
        set(value) {
            if (value > 0) field = value
            else field = 0
        }

    fun printSize() {
        println(size)
    }
}
medium
A. Setter does not assign value correctly
B. No error, code works fine
C. Getter should not use field
D. Property size should be val, not var

Solution

  1. Step 1: Check setter logic

    Setter assigns field = value if positive, else 0. This is valid.
  2. Step 2: Check getter and property type

    Getter returns field, which is correct. Property is var, so setter allowed.
  3. Final Answer:

    No error, code works fine -> Option B
  4. Quick Check:

    Setter and getter correct = no error [OK]
Hint: Setter with condition is valid; getter uses field correctly [OK]
Common Mistakes:
  • Thinking getter can't use field
  • Assuming setter must always assign value
  • Confusing var and val for setters
5. You want a Kotlin property score that never stores a negative value. Which custom setter implementation ensures this correctly?
hard
A. var score: Int = 0 set(value) { field = value * -1 }
B. var score: Int = 0 set(value) { if (value < 0) field = 0 }
C. var score: Int = 0 set(value) { field = value }
D. var score: Int = 0 set(value) { field = if (value < 0) 0 else value }

Solution

  1. Step 1: Understand requirement

    Property must never store negative values; negative inputs become zero.
  2. Step 2: Evaluate each setter

    var score: Int = 0 set(value) { field = if (value < 0) 0 else value } sets field to 0 if value is negative, else stores value correctly.
  3. Step 3: Check other options

    A stores negative of value, which can be positive or negative incorrectly; B only sets field if negative, ignoring positive values; C stores negatives unchanged.
  4. Final Answer:

    var score: Int = 0 set(value) { field = if (value < 0) 0 else value } -> Option D
  5. Quick Check:

    Setter with if-else ensures no negatives [OK]
Hint: Use if-else in setter to block negatives [OK]
Common Mistakes:
  • Not assigning field for positive values
  • Allowing negative values to be stored
  • Incorrectly negating values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard