Bird
Raised Fist0
Kotlinprogramming~20 mins

Extension properties in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Extension Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of extension property with backing field
What is the output of this Kotlin code using an extension property?
Kotlin
var String.lastChar: Char
    get() = this[length - 1]
    set(value) { println("Setter called with: $value") }

fun main() {
    val word = "hello"
    println(word.lastChar)
    word.lastChar = 'a'
}
ACompilation error: Extension properties cannot have backing fields
B
o
Setter called with: a
C
Setter called with: o
Setter called with: a
D
o
a
Attempts:
2 left
💡 Hint
Extension properties cannot store data, but can define custom getters and setters.
❓ Predict Output
intermediate
2:00remaining
Extension property on nullable type
What is the output of this Kotlin code with an extension property on a nullable String?
Kotlin
val String?.isNullOrEmpty: Boolean
    get() = this == null || this.isEmpty()

fun main() {
    val a: String? = null
    val b: String? = ""
    val c: String? = "abc"
    println(a.isNullOrEmpty)
    println(b.isNullOrEmpty)
    println(c.isNullOrEmpty)
}
A
true
true
false
B
false
true
false
CCompilation error: Extension properties cannot be declared on nullable types
D
true
false
false
Attempts:
2 left
💡 Hint
Extension properties can be declared on nullable types and can check for null.
🔧 Debug
advanced
2:00remaining
Why does this extension property cause a compilation error?
This Kotlin code tries to declare an extension property with a backing field. Why does it fail?
Kotlin
var String.extensionProp: Int = 0
    get() = field
    set(value) { field = value }

fun main() {
    println("test".extensionProp)
}
AExtension properties cannot have backing fields, so 'field' is not allowed.
BThe property must be declared as 'val' not 'var' for extensions.
CExtension properties must be top-level functions, not inside main.
DThe getter and setter must be omitted for extension properties.
Attempts:
2 left
💡 Hint
Remember how extension properties work internally in Kotlin.
❓ Predict Output
advanced
2:00remaining
Extension property with custom getter logic
What is the output of this Kotlin code using an extension property with a custom getter?
Kotlin
val List<Int>.sumOfSquares: Int
    get() = this.sumOf { it * it }

fun main() {
    val numbers = listOf(1, 2, 3)
    println(numbers.sumOfSquares)
}
A6
BRuntime error: Unresolved reference sumOf
CCompilation error: sumOf requires Kotlin 1.4+
D14
Attempts:
2 left
💡 Hint
The property calculates the sum of squares of the list elements.
❓ Predict Output
expert
2:00remaining
Extension property shadowing and receiver resolution
What is the output of this Kotlin code with extension properties and shadowing?
Kotlin
open class A {
    open val String.prop: String
        get() = "A"

    fun printProp(s: String) {
        println(s.prop)
    }
}

class B : A() {
    override val String.prop: String
        get() = "B"
}

fun main() {
    val b = B()
    b.printProp("hello")
}
AA
BCompilation error: Cannot override extension property
CB
DRuntime error: NoSuchMethodError
Attempts:
2 left
💡 Hint
Member extension properties declared in classes can be overridden in subclasses.

Practice

(1/5)
1. What is an extension property in Kotlin?
easy
A. A property that replaces an existing property in a class
B. A property that stores data inside the original class
C. A property that can only be used inside the class it is declared
D. A property added to an existing class without modifying its source code

Solution

  1. Step 1: Understand extension properties concept

    Extension properties allow adding new properties to classes without changing their original code.
  2. Step 2: Identify correct description

    "A property added to an existing class without modifying its source code" correctly describes this as adding properties externally without modifying the class.
  3. Final Answer:

    A property added to an existing class without modifying its source code -> Option D
  4. Quick Check:

    Extension property = added externally [OK]
Hint: Extension properties add features without changing original class [OK]
Common Mistakes:
  • Thinking extension properties store data inside the class
  • Confusing extension properties with member properties
  • Believing extension properties can replace existing ones
2. Which of the following is the correct syntax to declare an extension property isEven for Int in Kotlin?
easy
A. fun Int.isEven() = this % 2 == 0
B. val Int.isEven get() = this % 2 == 0
C. var Int.isEven = this % 2 == 0
D. val isEven: Int get() = this % 2 == 0

Solution

  1. Step 1: Recall extension property syntax

    Extension properties use val/var Type.propertyName get() = ... without a body for setter if read-only.
  2. Step 2: Check options

    val Int.isEven get() = this % 2 == 0 correctly uses val Int.isEven get() = this % 2 == 0. var Int.isEven = this % 2 == 0 wrongly uses var without setter. fun Int.isEven() = this % 2 == 0 is a function, not a property. val isEven: Int get() = this % 2 == 0 misses receiver type.
  3. Final Answer:

    val Int.isEven get() = this % 2 == 0 -> Option B
  4. Quick Check:

    Extension property syntax = val Type.prop get() [OK]
Hint: Extension properties need a getter, no setter if val [OK]
Common Mistakes:
  • Using var without setter for extension property
  • Writing extension property as a function
  • Omitting receiver type before property name
3. What will be the output of this Kotlin code?
val String.firstChar: Char
    get() = this[0]

fun main() {
    val word = "hello"
    println(word.firstChar)
}
medium
A. firstChar
B. hello
C. h
D. Compilation error

Solution

  1. Step 1: Understand extension property behavior

    The extension property firstChar returns the first character of the string using this[0].
  2. Step 2: Trace the code output

    For word = "hello", word.firstChar returns 'h', which is printed.
  3. Final Answer:

    h -> Option C
  4. Quick Check:

    Extension property returns first char = h [OK]
Hint: Extension property uses receiver object like member [OK]
Common Mistakes:
  • Expecting full string instead of first character
  • Confusing property with function call
  • Thinking extension properties cause errors
4. Identify the error in this Kotlin extension property declaration:
var String.lastChar: Char
    get() = this[length - 1]
    set(value) { this[length - 1] = value }
medium
A. You cannot assign to a character in a String
B. The getter syntax is incorrect
C. Extension properties cannot have setters
D. The property must be declared inside the class

Solution

  1. Step 1: Review extension property setter rules

    Extension properties can have setters, but they cannot modify the original object if it is immutable.
  2. Step 2: Check String mutability

    Strings in Kotlin are immutable; you cannot assign to this[length - 1]. This causes a compilation error.
  3. Final Answer:

    You cannot assign to a character in a String -> Option A
  4. Quick Check:

    Strings immutable = cannot set chars [OK]
Hint: Strings are immutable; extension setters can't modify them [OK]
Common Mistakes:
  • Assuming extension properties can store data
  • Thinking extension setters always cause errors
  • Confusing syntax errors with immutability issues
5. You want to add an extension property isAdult to the Person class that returns true if age is 18 or more, otherwise false. Given data class Person(val name: String, val age: Int), which is the correct extension property implementation?
hard
A. val Person.isAdult get() = age >= 18
B. var Person.isAdult get() = age >= 18; set(value) { age = if(value) 18 else 0 }
C. fun Person.isAdult() = age >= 18
D. val isAdult: Person get() = age >= 18

Solution

  1. Step 1: Define read-only extension property

    Since age is a val, we can only create a read-only extension property with a getter.
  2. Step 2: Check options for correct syntax and logic

    val Person.isAdult get() = age >= 18 correctly defines val Person.isAdult get() = age >= 18. var Person.isAdult get() = age >= 18; set(value) { age = if(value) 18 else 0 } tries to set age which is immutable. fun Person.isAdult() = age >= 18 is a function, not a property. val isAdult: Person get() = age >= 18 has wrong syntax and receiver placement.
  3. Final Answer:

    val Person.isAdult get() = age >= 18 -> Option A
  4. Quick Check:

    Read-only extension property with getter = val Person.isAdult get() = age >= 18 [OK]
Hint: Use val with getter for read-only extension properties [OK]
Common Mistakes:
  • Trying to create setter for immutable property
  • Confusing extension property with extension function
  • Incorrect syntax for receiver type in property
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