0
0
Kotlinprogramming~20 mins

Extensions vs member functions priority in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Extensions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of member function vs extension function
What is the output of this Kotlin code snippet?
Kotlin
class Sample {
    fun greet() = "Member"
}

fun Sample.greet() = "Extension"

fun main() {
    val s = Sample()
    println(s.greet())
}
AExtension
BMember
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Member functions have higher priority than extension functions when called on an instance.
Predict Output
intermediate
2:00remaining
Extension function called on nullable receiver
What is the output of this Kotlin code?
Kotlin
class Box {
    fun info() = "Member Box"
}

fun Box?.info() = "Extension Box?"

fun main() {
    val b: Box? = null
    println(b.info())
}
AExtension Box?
BMember Box
CNullPointerException
DCompilation error
Attempts:
2 left
💡 Hint
Extension functions can be called on nullable receivers, but member functions cannot.
🔧 Debug
advanced
2:00remaining
Why does this extension function not override the member function?
Consider this Kotlin code. Why does calling foo() print "Member" instead of "Extension"?
Kotlin
open class Base {
    fun foo() = "Member"
}

fun Base.foo() = "Extension"

fun main() {
    val b: Base = Base()
    println(b.foo())
}
AExtension functions cannot override member functions; member functions always have priority.
BExtension functions override member functions only if marked with override keyword.
CExtension functions override member functions only if the class is open.
DExtension functions override member functions only if they have different signatures.
Attempts:
2 left
💡 Hint
Extension functions are resolved statically and do not override member functions.
🧠 Conceptual
advanced
2:00remaining
Extension function dispatch behavior
Which statement best describes how Kotlin resolves extension functions compared to member functions?
AMember functions are resolved statically, extension functions are resolved dynamically.
BBoth extension and member functions are resolved dynamically at runtime based on the actual object type.
CExtension functions override member functions if they have the same signature and receiver type.
DExtension functions are resolved statically based on the declared type, member functions are resolved dynamically based on the runtime type.
Attempts:
2 left
💡 Hint
Think about how Kotlin decides which function to call when both member and extension functions exist.
Predict Output
expert
3:00remaining
Output with extension function and inheritance
What is the output of this Kotlin program?
Kotlin
open class Parent {
    fun greet() = "Parent member"
}

class Child : Parent()

fun Parent.greet() = "Parent extension"
fun Child.greet() = "Child extension"

fun main() {
    val c: Parent = Child()
    println(c.greet())
}
AChild extension
BParent extension
CParent member
DCompilation error
Attempts:
2 left
💡 Hint
Member functions have priority over extension functions even when the runtime type is a subclass.