Bird
Raised Fist0
Kotlinprogramming~10 mins

Extensions resolved statically in Kotlin - Interactive Code Practice

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

Complete the code to call the extension function on a String.

Kotlin
fun String.greet() = "Hello, $[1]!"

fun main() {
    val name = "Alice"
    println(name.greet())
}
Drag options to blanks, or click blank then click option'
Aname
Bthis
Cit
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name like 'name' instead of 'this' inside the extension function.
Using 'it' which is for lambda implicit parameter, not extension receiver.
2fill in blank
medium

Complete the code to call the correct extension function based on the variable type.

Kotlin
fun String.describe() = "String extension"
fun Int.describe() = "Int extension"

fun main() {
    val x: Any = 42
    println((x as [1]).describe())
}
Drag options to blanks, or click blank then click option'
AInt
BString
CAny
DNumber
Attempts:
3 left
💡 Hint
Common Mistakes
Casting to 'Any' or 'Number' which do not have the 'describe' extension.
Casting to 'String' which causes a runtime error.
3fill in blank
hard

Fix the error in the extension function call to use the correct receiver type.

Kotlin
open class Base
class Derived : Base()

fun Base.foo() = "Base"
fun Derived.foo() = "Derived"

fun printFoo(b: Base) {
    println(b.[1]())
}

fun main() {
    val d = Derived()
    printFoo(d)
}
Drag options to blanks, or click blank then click option'
Abaz
Bbar
Cfoo
Dqux
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name not defined as an extension causes a compile error.
Expecting Derived.foo() to be called dynamically, but Kotlin resolves extensions statically.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, filtering words longer than 3 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val lengths = {word: [1] for word in words if word [2] 3}
Drag options to blanks, or click blank then click option'
Alength
Bcount
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which is a function, not a property.
Using '<' which filters shorter words instead of longer.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, filtering words with length greater than 3.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = mapOf([1] to [2] for word in words if word.[3] > 3)
Drag options to blanks, or click blank then click option'
Aword.uppercase()
Bword.length
Clength
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word' instead of 'word.uppercase()' for keys.
Using 'word' instead of 'word.length' for values.
Using 'word.length' without '.length' property in the condition.

Practice

(1/5)
1. What does it mean that Kotlin extension functions are resolved statically?
easy
A. Extension functions override member functions dynamically.
B. The extension function called depends on the variable's declared type, not the actual object type.
C. The extension function is chosen based on the runtime type of the object.
D. Extension functions can change the original class's source code.

Solution

  1. Step 1: Understand static resolution

    Static resolution means decisions are made at compile time, not runtime.
  2. Step 2: Apply to extension functions

    Kotlin chooses the extension function based on the variable's declared type, ignoring the actual object type.
  3. Final Answer:

    The extension function called depends on the variable's declared type, not the actual object type. -> Option B
  4. Quick Check:

    Extensions resolved statically = based on declared type [OK]
Hint: Remember: extensions use variable type, not object type [OK]
Common Mistakes:
  • Thinking extension functions override member functions dynamically
  • Assuming extension functions change the original class
  • Confusing static with dynamic dispatch
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 use the syntax: fun ClassName.functionName() { ... }
  2. Step 2: Match syntax to options

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

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

    Extension syntax = fun ClassName.functionName() [OK]
Hint: Extension syntax: fun ClassName.functionName() [OK]
Common Mistakes:
  • Using 'extension' keyword which doesn't exist
  • Incorrect parameter placement
  • Missing receiver type before function name
3. Consider the code:
open class Base
class Derived : Base()

fun Base.foo() = "Base"
fun Derived.foo() = "Derived"

fun printFoo(b: Base) {
    println(b.foo())
}

fun main() {
    val d: Derived = Derived()
    printFoo(d)
}

What is the output when main() runs?
medium
A. Compilation error
B. Derived
C. Base
D. Runtime exception

Solution

  1. Step 1: Analyze parameter type in printFoo

    printFoo takes a parameter of type Base, so inside printFoo, b is of type Base.
  2. Step 2: Understand extension resolution

    Extension functions are resolved statically by the declared type, so b.foo() calls Base.foo(), not Derived.foo().
  3. Final Answer:

    Base -> Option C
  4. Quick Check:

    Extension call depends on declared type = Base [OK]
Hint: Extension call uses parameter type, not actual object type [OK]
Common Mistakes:
  • Expecting Derived.foo() to be called dynamically
  • Confusing extension with member function dispatch
  • Assuming runtime type affects extension resolution
4. Given the code below, what is the error and how to fix it?
open class Animal
class Dog : Animal()

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

fun main() {
    val animal: Animal = Dog()
    println(animal.sound())
}
medium
A. No error; output is "Some sound"
B. Compilation error: extension functions cannot be overloaded
C. Runtime error: unresolved extension function
D. Output is "Bark" but should be "Some sound"

Solution

  1. Step 1: Check for compilation errors

    Extension functions can be overloaded by receiver type; no syntax error here.
  2. Step 2: Understand output based on static resolution

    Variable animal is declared as Animal, so animal.sound() calls Animal.sound(), outputting "Some sound".
  3. Final Answer:

    No error; output is "Some sound" -> Option A
  4. Quick Check:

    Extension resolved by declared type = Animal.sound() [OK]
Hint: Extensions overload by receiver type; call depends on variable type [OK]
Common Mistakes:
  • Expecting Dog.sound() to be called dynamically
  • Thinking extension functions cause runtime errors
  • Confusing extension overloading with overriding
5. You want to add an extension function describe() to a class hierarchy where Cat inherits from Animal. You want describe() to print "Animal description" when called on an Animal variable, and "Cat description" when called on a Cat variable. Given Kotlin's static resolution of extensions, how should you design your code to achieve this behavior?
hard
A. Declare two extension functions: one for Animal and one for Cat; call describe() on variables typed accordingly.
B. Override describe() as a member function in Cat and Animal classes.
C. Use a when expression inside a single extension function on Animal to check the runtime type.
D. Use reflection to call the correct extension function at runtime.

Solution

  1. Step 1: Understand static resolution impact

    Extension functions are chosen by variable type, so different extensions for Animal and Cat can coexist.
  2. Step 2: Design with two extensions and variable types

    Define describe() extensions for Animal and Cat separately, then call describe() on variables declared as Animal or Cat to get different outputs.
  3. Final Answer:

    Declare two extension functions: one for Animal and one for Cat; call describe() on variables typed accordingly. -> Option A
  4. Quick Check:

    Different extensions + variable types = desired output [OK]
Hint: Use separate extensions and variable types to get different outputs [OK]
Common Mistakes:
  • Trying to override extensions as member functions
  • Relying on runtime type for extension dispatch
  • Using reflection unnecessarily
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