Recall & Review
beginner
What is a companion object in Kotlin?
A companion object is a special object inside a class that allows you to define functions and properties that belong to the class itself, not to instances. It acts like a static member in other languages.Click to reveal answer
beginner
What are top-level functions in Kotlin?
Top-level functions are functions declared outside any class or object. They belong to the package and can be called directly without needing a class instance.Click to reveal answer
intermediate
When should you use a companion object instead of a top-level function?
Use a companion object when the function logically belongs to a class and needs access to private members or to group related functions with the class. It helps organize code inside the class.Click to reveal answer
intermediate
When is it better to use top-level functions rather than companion object functions?
Use top-level functions when the function is general, does not need access to class internals, or when you want to keep code simple and avoid unnecessary nesting inside classes.Click to reveal answer
intermediate
How does using companion objects affect code readability compared to top-level functions?
Companion objects group related functions inside the class, making it clear they belong together, which can improve readability. Top-level functions keep code flat and simple but may scatter related functions across files.
Click to reveal answer
Which Kotlin feature allows defining functions that behave like static methods inside a class?
✗ Incorrect
Companion objects let you define functions inside a class that act like static methods.
Where are top-level functions declared in Kotlin?
✗ Incorrect
Top-level functions are declared outside any class or object, directly in a package.
When should you prefer top-level functions over companion object functions?
✗ Incorrect
Top-level functions are best for general-purpose functions not tied to a class.
What is a benefit of using companion objects for functions?
✗ Incorrect
Companion object functions can access private members of their class.
Which statement about companion objects and top-level functions is true?
✗ Incorrect
Companion objects group functions inside a class, unlike top-level functions.
Explain the main differences between companion objects and top-level functions in Kotlin.
Think about where each function lives and what it can access.
You got /4 concepts.
Describe scenarios when you would choose a companion object over a top-level function.
Consider the relationship between the function and the class.
You got /3 concepts.