Challenge - 5 Problems
Kotlin Interface Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of interface method call
What is the output of this Kotlin code when calling
greet() on an instance of FriendlyPerson?Kotlin
interface Greeter { fun greet(): String } class FriendlyPerson : Greeter { override fun greet(): String { return "Hello, friend!" } } fun main() { val person: Greeter = FriendlyPerson() println(person.greet()) }
Attempts:
2 left
💡 Hint
Check how the
greet() method is implemented in the class.✗ Incorrect
The class
FriendlyPerson implements the Greeter interface and overrides the greet() method to return "Hello, friend!". So calling greet() prints that string.🧠 Conceptual
intermediate1:30remaining
Interface property declaration
Which of the following is a valid way to declare a read-only property in a Kotlin interface?
Attempts:
2 left
💡 Hint
Interfaces can declare properties without initial values.
✗ Incorrect
In Kotlin, interfaces can declare abstract properties without initial values. Option B declares a read-only property
wheels without initializing it, which is valid. Option B tries to initialize a var property, which is not allowed. Option B declares a function, not a property. Option B tries to initialize a val property, which is not allowed in interfaces.🔧 Debug
advanced2:00remaining
Identify the error in interface implementation
What error will this Kotlin code produce?
Kotlin
interface Speaker { fun speak() } class Person : Speaker { fun speak() { println("Hi!") } } fun main() { val p = Person() p.speak() }
Attempts:
2 left
💡 Hint
Check if the method correctly overrides the interface method.
✗ Incorrect
In Kotlin, when a class implements an interface method, it must use the 'override' keyword. The code misses 'override' before 'fun speak()', causing a compilation error.
❓ Predict Output
advanced2:30remaining
Multiple interface inheritance output
What is the output of this Kotlin code?
Kotlin
interface A { fun hello() = "Hello from A" } interface B { fun hello() = "Hello from B" } class C : A, B { override fun hello(): String { return super<A>.hello() + " & " + super<B>.hello() } } fun main() { val c = C() println(c.hello()) }
Attempts:
2 left
💡 Hint
Look at how the class C calls the super implementations explicitly.
✗ Incorrect
Class C overrides hello() and calls both interface implementations explicitly using super and super. So it prints "Hello from A & Hello from B".
🧠 Conceptual
expert3:00remaining
Interface delegation behavior
Given the following Kotlin code, what will be printed when
main() runs?Kotlin
interface Logger { fun log(message: String) } class ConsoleLogger : Logger { override fun log(message: String) { println("Console: $message") } } class Application(logger: Logger) : Logger by logger { fun run() { log("Application started") } } fun main() { val logger = ConsoleLogger() val app = Application(logger) app.run() }
Attempts:
2 left
💡 Hint
Interface delegation forwards calls to the provided object.
✗ Incorrect
The Application class delegates Logger interface to the passed logger instance. So calling log() inside Application calls ConsoleLogger's log(), printing "Console: Application started".