0
0
Kotlinprogramming~20 mins

Extension properties in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 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.