Challenge - 5 Problems
Multiple Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple interface implementation with method override
What is the output of this Kotlin code when the
test() function is called?Kotlin
interface A { fun greet() = "Hello from A" } interface B { fun greet() = "Hello from B" } class C : A, B { override fun greet(): String { return super<A>.greet() + " and " + super<B>.greet() } } fun test() { val c = C() println(c.greet()) }
Attempts:
2 left
💡 Hint
Look at how
super is used to specify which interface's method to call.✗ Incorrect
Class C implements both interfaces A and B, each with a default greet() method. To resolve ambiguity, C overrides greet() and explicitly calls both interface methods using
super<A>.greet() and super<B>.greet(). The output concatenates both greetings.❓ Predict Output
intermediate2:00remaining
Which method is called in multiple interface implementation?
Given the following Kotlin code, what will be printed when
test() is executed?Kotlin
interface X { fun action() = "Action from X" } interface Y { fun action() = "Action from Y" } class Z : X, Y { override fun action(): String { return super<Y>.action() } } fun test() { val z = Z() println(z.action()) }
Attempts:
2 left
💡 Hint
Check which interface's method is explicitly called with
super.✗ Incorrect
Class Z overrides action() and explicitly calls
super<Y>.action(), so the method from interface Y is executed, printing "Action from Y".🔧 Debug
advanced2:00remaining
Identify the error in multiple interface implementation
What error will this Kotlin code produce when compiled?
Kotlin
interface M { fun foo() = "M" } interface N { fun foo() } class O : M, N { // No override of foo() }
Attempts:
2 left
💡 Hint
Check which interface requires foo() to be implemented and which provides a default.
✗ Incorrect
Interface M provides a default implementation of foo(), but interface N declares foo() without implementation. Class O must override foo() to resolve this conflict; otherwise, compilation fails.
📝 Syntax
advanced2:00remaining
Correct syntax for multiple interface implementation with property
Which option correctly implements two interfaces with a property of the same name in Kotlin?
Kotlin
interface P { val value: String } interface Q { val value: String } class R : P, Q { // Implement value here }
Attempts:
2 left
💡 Hint
Properties from interfaces must be overridden with 'override val' or 'override var'.
✗ Incorrect
Both interfaces declare a property 'value'. The class must override it with 'override val value = "Hello"'. Option C tries to override a property as a function, which is invalid. Option C misses 'override', C uses 'var' which is allowed but less precise here, and D correctly overrides the property.
🚀 Application
expert3:00remaining
Implement a class that combines behaviors from two interfaces
You have two interfaces in Kotlin:
Implement a class
Which of the following class implementations correctly does this?
interface Logger { fun log(msg: String) }interface Printer { fun print(msg: String) }Implement a class
Console that implements both interfaces. The log method should print the message prefixed with "LOG: ", and the print method should print the message prefixed with "PRINT: ".Which of the following class implementations correctly does this?
Attempts:
2 left
💡 Hint
Remember to use 'override' keyword for interface methods and implement both methods.
✗ Incorrect
Option B correctly overrides both interface methods with the required prefixes. Option B misses 'override' keyword, causing compilation error. Option B swaps the prefixes, so behavior is incorrect. Option B only overrides one method, missing the other.