0
0
Kotlinprogramming~10 mins

Interface with default implementations in Kotlin - Interactive Code Practice

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

Complete the code to declare an interface with a default implementation for the greet function.

Kotlin
interface Greeter {
    fun greet() {
        println([1])
    }
}
Drag options to blanks, or click blank then click option'
A"Hello from interface!"
Bgreet()
Cprintln("Hello")
Dfun greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call greet() inside its own body causing recursion.
Using function declaration syntax inside the function body.
2fill in blank
medium

Complete the code to implement the Greeter interface and override the greet function.

Kotlin
class FriendlyGreeter : Greeter {
    override fun greet() {
        println([1])
    }
}
Drag options to blanks, or click blank then click option'
Agreet()
B"Hi there!"
Cprintln("Hello")
Dsuper.greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling greet() inside itself causing infinite recursion.
Trying to call super.greet() without a proper context.
3fill in blank
hard

Fix the error in the code to call the default greet implementation from the interface inside the override.

Kotlin
class PoliteGreeter : Greeter {
    override fun greet() {
        [1].greet()
        println("Have a nice day!")
    }
}
Drag options to blanks, or click blank then click option'
Athis
Bsuper
CGreeter.super
DPoliteGreeter.super
Attempts:
3 left
💡 Hint
Common Mistakes
Using just super.greet() which is invalid for interfaces.
Using this.greet() causing recursion.
4fill in blank
hard

Fill both blanks to define an interface with a default method and a class that calls it.

Kotlin
interface Speaker {
    fun speak() {
        println([1])
    }
}

class Person : Speaker {
    fun sayHello() {
        [2].speak()
    }
}
Drag options to blanks, or click blank then click option'
A"Hello!"
BSpeaker.super
Cthis
D"Hi!"
Attempts:
3 left
💡 Hint
Common Mistakes
Using this.speak() instead of Speaker.super.speak().
Not providing a string literal in the interface method.
5fill in blank
hard

Fill all three blanks to create an interface with a default method, a class overriding it, and calling both versions.

Kotlin
interface Animal {
    fun sound() {
        println([1])
    }
}

class Dog : Animal {
    override fun sound() {
        println([2])
        [3].sound()
    }
}
Drag options to blanks, or click blank then click option'
A"Generic animal sound"
B"Bark!"
CAnimal.super
D"Meow!"
Attempts:
3 left
💡 Hint
Common Mistakes
Using super.sound() instead of Animal.super.sound() for interface default call.
Not overriding the sound method properly.