Recall & Review
beginner
What is a companion object in Kotlin?
A companion object is a special object inside a class that allows you to call its members like static members in other languages. It is created once per class and can hold functions and properties.Click to reveal answer
intermediate
Why would you use a named companion object instead of the default unnamed one?
You use a named companion object to give it a specific name, which can improve code readability and allow you to implement interfaces or distinguish between multiple companion objects if needed.
Click to reveal answer
beginner
How do you declare a named companion object in Kotlin?
Inside a class, write `companion object Name { ... }` where `Name` is the chosen name for the companion object.
Click to reveal answer
intermediate
Can you access members of a named companion object without specifying the name?
Yes, Kotlin allows you to access members of the companion object directly through the class name without using the companion object's name, but you can also use the name explicitly if you want.Click to reveal answer
intermediate
What is a practical example of using a named companion object?
A named companion object can implement an interface or hold factory methods with a clear name, making the code easier to understand and organize.
Click to reveal answer
How do you declare a named companion object in Kotlin?
✗ Incorrect
The correct syntax is 'companion object Name { }' where 'Name' is the name you give to the companion object.
What is the main benefit of naming a companion object?
✗ Incorrect
Naming a companion object helps with readability and allows it to implement interfaces or be referenced explicitly.
Can you access a named companion object's members without using its name?
✗ Incorrect
Kotlin lets you access companion object members directly via the class name, even if the companion object has a name.
Which of these is NOT true about companion objects?
✗ Incorrect
Companion objects are singletons per class and cannot be instantiated multiple times.
What keyword is used to declare a companion object in Kotlin?
✗ Incorrect
The keyword 'companion' is used to declare a companion object inside a class.
Explain what a named companion object is and why you might use one in Kotlin.
Think about how naming helps organize code and implement interfaces.
You got /4 concepts.
Describe how to declare and access members of a named companion object with a simple example.
Write a small code snippet in your mind or on paper.
You got /3 concepts.