0
0
Kotlinprogramming~20 mins

Companion vs top-level functions decision in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Companion vs Top-level Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of companion object function call
What is the output of this Kotlin code when calling MyClass.greet()?
Kotlin
class MyClass {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun main() {
    println(MyClass.greet())
}
AHello from companion
BCompilation error: greet is not a member of MyClass
CHello from top-level function
DRuntime error: Unresolved reference greet
Attempts:
2 left
💡 Hint
Companion object functions can be called using the class name.
🧠 Conceptual
intermediate
2:00remaining
Choosing between companion and top-level functions
Which situation is best suited for using a companion object function instead of a top-level function in Kotlin?
AWhen you want to define a function that can be called without importing
BWhen the function is a utility unrelated to any class
CWhen you want to avoid creating any class in your code
DWhen the function logically belongs to a class and needs access to its private members
Attempts:
2 left
💡 Hint
Think about encapsulation and access to class internals.
Predict Output
advanced
2:00remaining
Output of top-level vs companion function with same name
What is the output of this Kotlin code?
Kotlin
fun greet() = "Hello from top-level"

class MyClass {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun main() {
    println(greet())
    println(MyClass.greet())
}
ACompilation error: greet is ambiguous
B
Hello from top-level
Hello from companion
C
Hello from companion
Hello from top-level
DRuntime error: greet not found
Attempts:
2 left
💡 Hint
Top-level and companion functions with the same name are called differently.
🔧 Debug
advanced
2:00remaining
Why does this companion object function cause a compilation error?
Consider this Kotlin code snippet: class MyClass { companion object { fun printMessage() { println(message) } } private val message = "Hello" } Why does this code cause a compilation error?
ACompanion object cannot access instance properties like 'message'
BThe 'message' property is not initialized
CThe function printMessage must be marked as static
DThe companion object must be named explicitly
Attempts:
2 left
💡 Hint
Think about what companion objects can access inside a class.
🧠 Conceptual
expert
2:00remaining
Best practice for organizing utility functions in Kotlin
You have several utility functions that do not require access to any class members. What is the best practice for organizing these functions in Kotlin?
APlace them inside an object declaration to simulate static methods
BPlace them inside a companion object of a class
CPlace them as top-level functions in a Kotlin file
DPlace them as member functions inside a class instance
Attempts:
2 left
💡 Hint
Consider simplicity and idiomatic Kotlin style.