Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call greet() inside its own body causing recursion.
Using function declaration syntax inside the function body.
✗ Incorrect
The greet function in the interface has a default implementation that prints the greeting message.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling greet() inside itself causing infinite recursion.
Trying to call super.greet() without a proper context.
✗ Incorrect
The FriendlyGreeter class overrides greet to print a custom message.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using just super.greet() which is invalid for interfaces.
Using this.greet() causing recursion.
✗ Incorrect
To call the interface's default implementation, use InterfaceName.super.methodName() syntax.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using this.speak() instead of Speaker.super.speak().
Not providing a string literal in the interface method.
✗ Incorrect
The interface method prints "Hello!" and the class calls the default speak using Speaker.super.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using super.sound() instead of Animal.super.sound() for interface default call.
Not overriding the sound method properly.
✗ Incorrect
The interface prints a generic sound, Dog overrides with "Bark!" and calls the default using Animal.super.sound().