Bird
Raised Fist0
Kotlinprogramming~10 mins

Extensions vs member functions priority in Kotlin - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to call the member function instead of the extension function.

Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.greet() = "Extension"

fun main() {
    val ex = Example()
    println(ex.[1]())
}
Drag options to blanks, or click blank then click option'
Agreeting
Bgreet
CextensionGreet
DcallGreet
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the extension function by using a different name.
Assuming extension functions override member functions.
2fill in blank
medium

Complete the code to call the extension function explicitly.

Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.greet() = "Extension"

fun main() {
    val ex = Example()
    println([1](ex))
}
Drag options to blanks, or click blank then click option'
Agreet
Bex.greet
Cex.Extension.greet
DExample.greet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the member function instead of the extension function.
Using incorrect syntax to call the extension function.
3fill in blank
hard

Fix the error in calling the extension function inside another extension function.

Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.greet() = "Extension"

fun Example.callGreet() = [1](this)

fun main() {
    val ex = Example()
    println(ex.callGreet())
}
Drag options to blanks, or click blank then click option'
AExample.greet
Bgreet
Cthis.greet
DcallGreet
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'greet()' without qualification calls the member function.
Using 'this.greet()' calls the member function, not the extension.
4fill in blank
hard

Fill both blanks to define an extension function and call the member function inside it.

Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.[1]() = "Extension calls " + this.[2]()

fun main() {
    val ex = Example()
    println(ex.greet())
    println(ex.[1]())
}
Drag options to blanks, or click blank then click option'
AgreetExtension
Bgreet
DgreetMember
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the extension function the same as the member function.
Not calling the member function correctly inside the extension.
5fill in blank
hard

Fill all three blanks to define an extension function that calls another extension and the member function.

Kotlin
class Example {
    fun greet() = "Member"
}

fun Example.[1]() = "Extension1"
fun Example.[2]() = "Extension2 calls " + [3].greet()

fun main() {
    val ex = Example()
    println(ex.greet())
    println(ex.[1]())
    println(ex.[2]())
}
Drag options to blanks, or click blank then click option'
Agreet1
Bgreet2
Cthis
DExample
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for both extension functions.
Using 'Example' instead of 'this' to call the member function.

Practice

(1/5)
1. In Kotlin, when a class has a member function and an extension function with the same name, which one is called by default?
easy
A. The member function of the class
B. The extension function
C. Both are called
D. It causes a compile-time error

Solution

  1. Step 1: Understand member vs extension functions

    Member functions are defined inside the class, while extension functions are defined outside to add functionality.
  2. Step 2: Check Kotlin's call priority

    Kotlin always prefers member functions over extension functions when both have the same name.
  3. Final Answer:

    The member function of the class -> Option A
  4. Quick Check:

    Member functions have priority = A [OK]
Hint: Member functions override extensions with same name [OK]
Common Mistakes:
  • Thinking extension functions override member functions
  • Assuming both functions run together
  • Believing it causes a compile error
2. Which of the following is the correct syntax to declare an extension function greet for class Person in Kotlin?
easy
A. fun greet() Person { println("Hello") }
B. fun greet.Person() { println("Hello") }
C. extension fun Person.greet() { println("Hello") }
D. fun Person.greet() { println("Hello") }

Solution

  1. Step 1: Recall extension function syntax

    Extension functions are declared as fun ClassName.functionName().
  2. Step 2: Match syntax with options

    fun Person.greet() { println("Hello") } matches the correct syntax: fun Person.greet().
  3. Final Answer:

    fun Person.greet() { println("Hello") } -> Option D
  4. Quick Check:

    Extension syntax = D [OK]
Hint: Extension syntax: fun ClassName.functionName() [OK]
Common Mistakes:
  • Placing function name before class name
  • Using 'extension' keyword incorrectly
  • Wrong function declaration order
3. Consider the following Kotlin code:
class Box {
    fun size() = "Member size"
}

fun Box.size() = "Extension size"

fun main() {
    val box = Box()
    println(box.size())
}

What is the output when this code runs?
medium
A. Compilation error
B. Member size
C. Extension size
D. Runtime exception

Solution

  1. Step 1: Identify member and extension functions

    Class Box has a member function size() returning "Member size". An extension function with the same name returns "Extension size".
  2. Step 2: Determine which function is called

    Kotlin calls the member function when both exist, so box.size() calls the member function.
  3. Final Answer:

    Member size -> Option B
  4. Quick Check:

    Member function called over extension = C [OK]
Hint: Member function called if name clashes [OK]
Common Mistakes:
  • Expecting extension function output
  • Thinking both outputs print
  • Assuming compile error due to name clash
4. Given this Kotlin code:
class Gadget {
    fun info() = "Member info"
}

fun Gadget.info() = "Extension info"

fun main() {
    val g: Gadget? = null
    println(g?.info())
}

What is the output when running this code?
medium
A. null
B. Member info
C. Extension info
D. NullPointerException

Solution

  1. Step 1: Analyze nullable variable and safe call

    Variable g is null. The safe call operator ?. prevents calling functions on null.
  2. Step 2: Understand safe call with extension/member functions

    Since g is null, g?.info() returns null without calling any function.
  3. Final Answer:

    null -> Option A
  4. Quick Check:

    Safe call on null returns null = B [OK]
Hint: Safe call on null returns null, no function called [OK]
Common Mistakes:
  • Expecting function output despite null
  • Thinking NullPointerException occurs
  • Confusing extension and member call on null
5. You want to add a describe() function to class Device without modifying its source code. The class already has a member function describe(). How can you call the extension describe() function explicitly in Kotlin?
hard
A. You cannot call the extension if member exists with same name
B. Use (device as Device).describe() to call extension
C. Call extension with a wrapper function or different name
D. Use Device::describe.invoke(device) to call extension

Solution

  1. Step 1: Understand member vs extension call priority

    Kotlin always calls member functions over extensions if names clash; extension cannot override member.
  2. Step 2: Find workaround to call extension

    To call extension explicitly, rename it or call via a wrapper function with a different name.
  3. Final Answer:

    Call extension with a wrapper function or different name -> Option C
  4. Quick Check:

    Rename or wrap extension to call it = A [OK]
Hint: Rename extension or wrap it to call when member exists [OK]
Common Mistakes:
  • Trying to cast to call extension directly
  • Expecting extension to override member
  • Using reflection incorrectly to call extension
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