Challenge - 5 Problems
Companion Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of companion object implementing interface
What is the output of this Kotlin code?
Kotlin
interface Printer { fun printMessage(): String } class MyClass { companion object : Printer { override fun printMessage() = "Hello from companion" } } fun main() { println(MyClass.printMessage()) }
Attempts:
2 left
💡 Hint
Companion objects can implement interfaces and their methods can be called on the class.
✗ Incorrect
The companion object implements Printer interface and overrides printMessage. Calling MyClass.printMessage() calls the companion's method, printing "Hello from companion".
❓ Predict Output
intermediate2:00remaining
Companion object interface method call via interface reference
What will be printed when running this Kotlin code?
Kotlin
interface Logger { fun log(): String } class Service { companion object : Logger { override fun log() = "Logging from companion" } } fun main() { val logger: Logger = Service println(logger.log()) }
Attempts:
2 left
💡 Hint
Companion objects can be referenced by the class name and implement interfaces.
✗ Incorrect
Service's companion object implements Logger, so Service can be assigned to Logger. Calling log() prints "Logging from companion".
🔧 Debug
advanced2:00remaining
Identify the error in companion object interface implementation
This Kotlin code tries to implement an interface in a companion object but fails. What is the error?
Kotlin
interface Calculator { fun calculate(x: Int): Int } class Math { companion object : Calculator { fun calculate(x: Int): Int = x * x } } fun main() { println(Math.calculate(5)) }
Attempts:
2 left
💡 Hint
Check if the method signature matches the interface and if override keyword is used.
✗ Incorrect
The companion object implements Calculator but the calculate method lacks 'override' keyword, causing a compilation error.
📝 Syntax
advanced2:00remaining
Correct syntax for companion object implementing multiple interfaces
Which option shows the correct syntax for a companion object implementing two interfaces in Kotlin?
Attempts:
2 left
💡 Hint
Kotlin uses ':' to implement interfaces and separates multiple interfaces with commas.
✗ Incorrect
In Kotlin, companion objects implement interfaces using ':' and separate multiple interfaces with commas. 'implements', '&', and 'extends' are invalid here.
🚀 Application
expert3:00remaining
Using companion object with interface to provide factory method
Given this interface and class, which option correctly uses a companion object implementing the interface to create instances?
Kotlin
interface Factory<T> { fun create(): T } class Widget private constructor(val id: Int) { companion object : Factory<Widget> { private var counter = 0 override fun create(): Widget { counter++ return Widget(counter) } } } fun main() { val w1 = Widget.create() val w2 = Widget.create() println("${w1.id},${w2.id}") }
Attempts:
2 left
💡 Hint
Companion object can access private constructor and maintain state.
✗ Incorrect
The companion object implements Factory and keeps a counter. Each create call increments counter and returns a new Widget with unique id. Output is '1,2'.