A companion object is a special object inside a class that allows you to define members that belong to the class itself, not to instances. It's like a static member in other languages.
Click to reveal answer
intermediate
Can a companion object implement an interface in Kotlin?
Yes, a companion object can implement interfaces. This allows the companion object to provide specific behavior defined by the interface.
Click to reveal answer
beginner
How do you call a function defined in a companion object that implements an interface?
You call it using the class name, like ClassName.functionName(), because companion object members behave like static members.
Click to reveal answer
intermediate
Example: What does this code do?
<pre>interface Logger {
fun log(message: String)
}
class MyClass {
companion object : Logger {
override fun log(message: String) {
println("Log: $message")
}
}
}</pre>
This code defines an interface Logger with a log function. The companion object of MyClass implements Logger and provides the log function. You can call MyClass.log("Hello") to print "Log: Hello".
Click to reveal answer
intermediate
Why use interfaces with companion objects?
Using interfaces with companion objects helps organize code by defining contracts for static-like behavior. It makes the companion object behave predictably and allows polymorphism.
Click to reveal answer
What keyword defines a companion object inside a Kotlin class?
Astatic
Bcompanion
Cobject
Dinterface
✗ Incorrect
The keyword 'companion' defines a companion object inside a Kotlin class.
Can a companion object implement an interface in Kotlin?
5. You want to create a Kotlin class Calculator with a companion object that implements an interface Operations having a method add(a: Int, b: Int): Int. How can you call add without creating an instance of Calculator?
interface Operations {
fun add(a: Int, b: Int): Int
}
class Calculator {
companion object : Operations {
override fun add(a: Int, b: Int): Int = a + b
}
}
hard
A. Call Operations.add(5, 3)
B. Create instance and call Calculator().add(5, 3)
C. Call Calculator.add(5, 3)
D. Call Calculator.companion.add(5, 3)
Solution
Step 1: Understand companion object access
Companion object members can be called directly on the class name like Calculator.add() (syntactic sugar) or explicitly via Calculator.Companion.
Step 2: Check method call options
Calculator.add(5, 3) is the standard direct call. B requires an instance which doesn't have the method. C calls the interface directly (wrong). D uses incorrect casing ("companion" instead of "Companion").
Final Answer:
Call Calculator.add(5, 3) -> Option C
Quick Check:
Class.method() = companion access [OK]
Hint: Companion object methods called on class directly [OK]
Common Mistakes:
Using lowercase 'companion' instead of 'Companion'
Creating instance unnecessarily
Trying to call interface name directly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.