0
0
Kotlinprogramming~20 mins

Extensions resolved statically in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Extensions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extension function call with base type reference
What is the output of this Kotlin code snippet?
Kotlin
open class Animal
class Dog : Animal()

fun Animal.sound() = "Animal sound"
fun Dog.sound() = "Dog sound"

fun main() {
    val animal: Animal = Dog()
    println(animal.sound())
}
ACompilation error
BRuntime exception
CAnimal sound
DDog sound
Attempts:
2 left
💡 Hint
Extension functions are resolved based on the declared type, not the runtime type.
Predict Output
intermediate
2:00remaining
Extension function call with subclass reference
What will this Kotlin program print?
Kotlin
open class Vehicle
class Car : Vehicle()

fun Vehicle.describe() = "Vehicle"
fun Car.describe() = "Car"

fun main() {
    val car = Car()
    println(car.describe())
}
AVehicle
BCompilation error
CRuntime exception
DCar
Attempts:
2 left
💡 Hint
The variable type is Car, so Car's extension function is called.
🔧 Debug
advanced
2:30remaining
Why does this extension function call print unexpected output?
Consider this Kotlin code. Why does it print "Base extension" instead of "Derived extension"?
Kotlin
open class Base
class Derived : Base()

fun Base.info() = "Base extension"
fun Derived.info() = "Derived extension"

fun printInfo(b: Base) {
    println(b.info())
}

fun main() {
    val d = Derived()
    printInfo(d)
}
AExtension functions are resolved statically by the declared type of the parameter 'b'.
BDerived.info() is private and cannot be called.
CThe code has a runtime exception causing fallback to Base.info().
DKotlin does not support extension functions on subclasses.
Attempts:
2 left
💡 Hint
Think about the type of 'b' inside printInfo function.
🧠 Conceptual
advanced
2:00remaining
Understanding extension function dispatch in Kotlin
Which statement best describes how Kotlin resolves extension functions when called on an object?
AExtension functions are resolved at compile time using the declared type of the variable.
BExtension functions are resolved at runtime using the actual type of the object.
CExtension functions override member functions if they have the same signature.
DExtension functions can be dynamically dispatched like virtual methods.
Attempts:
2 left
💡 Hint
Consider if extension functions behave like normal class methods.
Predict Output
expert
3:00remaining
Output of extension function with nullable receiver and smart cast
What is the output of this Kotlin code?
Kotlin
fun String?.describe() = if (this == null) "Null string" else "String of length ${this.length}"

fun main() {
    val s: String? = null
    println(s.describe())

    val t: String? = "hello"
    println(t.describe())
}
ANull string\nNull string
BNull string\nString of length 5
CCompilation error due to nullable receiver
DRuntime exception due to null access
Attempts:
2 left
💡 Hint
Extension functions can have nullable receivers and handle null safely.