Challenge - 5 Problems
Extension Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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' }
Attempts:
2 left
💡 Hint
Extension properties cannot store data, but can define custom getters and setters.
✗ Incorrect
The getter returns the last character 'o'. The setter only prints a message; it does not store the value. So output is 'o' then 'Setter called with: a'.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Extension properties can be declared on nullable types and can check for null.
✗ Incorrect
The property returns true if the string is null or empty. So null and empty string print true, non-empty prints false.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Remember how extension properties work internally in Kotlin.
✗ Incorrect
Extension properties do not actually insert fields into the class, so backing fields like 'field' are not allowed and cause compilation errors.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
The property calculates the sum of squares of the list elements.
✗ Incorrect
1*1 + 2*2 + 3*3 = 1 + 4 + 9 = 14, so the output is 14.
❓ Predict Output
expert2: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") }
Attempts:
2 left
💡 Hint
Member extension properties declared in classes can be overridden in subclasses.
✗ Incorrect
Member extension properties can be overridden. When calling b.printProp (where b is B), the dispatch receiver is B, so the overridden getter from B is used, printing 'B'.