0
0
Kotlinprogramming~5 mins

Named companion objects in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Acompanion object Name { }
Bobject companion Name { }
Ccompanion Name object { }
Dobject Name companion { }
What is the main benefit of naming a companion object?
ATo create multiple companion objects with the same name
BTo improve code readability and allow interface implementation
CTo prevent access from outside the class
DTo make the companion object private
Can you access a named companion object's members without using its name?
AOnly if the companion object is private
BNo, you must always use the companion object's name
COnly if the companion object is unnamed
DYes, through the class name directly
Which of these is NOT true about companion objects?
AThey can be instantiated multiple times
BThey can hold functions and properties
CThey are created once per class
DThey can implement interfaces
What keyword is used to declare a companion object in Kotlin?
Aobject
Bstatic
Ccompanion
Dclass
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.