0
0
Kotlinprogramming~20 mins

Companion object with interfaces in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Companion Interface Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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())
}
ACompilation error: companion object cannot implement interface
BHello from companion
CRuntime error: Unresolved reference to printMessage
DHello from MyClass
Attempts:
2 left
💡 Hint
Companion objects can implement interfaces and their methods can be called on the class.
Predict Output
intermediate
2: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())
}
ALogging from companion
BNo output
CRuntime error: Service is not a Logger instance
DCompilation error: Cannot assign Service to Logger
Attempts:
2 left
💡 Hint
Companion objects can be referenced by the class name and implement interfaces.
🔧 Debug
advanced
2: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))
}
AOutput: 25
BRuntime error: Method calculate not found
CNo error, prints 5
DCompilation error: 'calculate' must override interface method
Attempts:
2 left
💡 Hint
Check if the method signature matches the interface and if override keyword is used.
📝 Syntax
advanced
2:00remaining
Correct syntax for companion object implementing multiple interfaces
Which option shows the correct syntax for a companion object implementing two interfaces in Kotlin?
A
companion object : Interface1, Interface2 {
    override fun method1() {}
    override fun method2() {}
}
B
companion object implements Interface1, Interface2 {
    override fun method1() {}
    override fun method2() {}
}
C
companion object : Interface1 & Interface2 {
    override fun method1() {}
    override fun method2() {}
}
D
companion object extends Interface1, Interface2 {
    override fun method1() {}
    override fun method2() {}
}
Attempts:
2 left
💡 Hint
Kotlin uses ':' to implement interfaces and separates multiple interfaces with commas.
🚀 Application
expert
3: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}")
}
A0,1
BRuntime error: StackOverflowError
C1,2
DCompilation error: Cannot access private constructor
Attempts:
2 left
💡 Hint
Companion object can access private constructor and maintain state.