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 { }
✗ 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?
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
✗ 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?
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
✗ 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?
AThey can be instantiated multiple times
BThey can hold functions and properties
CThey are created once per class
DThey can implement interfaces
✗ Incorrect
Companion objects are singletons per class and cannot be instantiated multiple times.
What keyword is used to declare a companion object in Kotlin?
Aobject
Bstatic
Ccompanion
Dclass
✗ 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.
Practice
(1/5)
1. What is the main purpose of naming a companion object in Kotlin?
easy
A. To give a clear and specific name to the singleton object inside a class
B. To create multiple instances of the companion object
C. To make the companion object inherit from another class
D. To prevent the companion object from being accessed outside the class
Solution
Step 1: Understand companion objects
Companion objects are singletons inside a class used to hold members related to the class itself.
Step 2: Purpose of naming companion objects
Giving a name to a companion object helps to clearly identify and access its members, improving code readability.
Final Answer:
To give a clear and specific name to the singleton object inside a class -> Option A
Quick Check:
Named companion object = clear singleton name [OK]
Hint: Named companion objects clarify singleton usage [OK]
2. Which of the following is the correct syntax to declare a named companion object in Kotlin?
easy
A. class MyClass { companionobject Named { fun greet() = "Hi" } }
B. class MyClass { companion Named object { fun greet() = "Hi" } }
C. class MyClass { companion object Named { fun greet() = "Hi" } }
D. class MyClass { object companion Named { fun greet() = "Hi" } }
Solution
Step 1: Recall correct companion object syntax
The correct syntax is 'companion object Name { ... }' inside the class.
Step 2: Check each option
class MyClass { companion object Named { fun greet() = "Hi" } } matches the correct syntax exactly. Others have misplaced keywords or wrong order.
Final Answer:
class MyClass { companion object Named { fun greet() = "Hi" } } -> Option C
Quick Check:
companion object Name { } syntax [OK]
Hint: Remember: 'companion object Name' order is fixed [OK]
Common Mistakes:
Swapping 'object' and 'companion' keywords
Writing 'companionobject' as one word
Placing the name before 'companion'
3. What will be the output of this Kotlin code?
class Box {
companion object Factory {
fun create() = "Box created"
}
}
fun main() {
println(Box.Factory.create())
}
medium
A. Factory created
B. Box created
C. Compilation error
D. Runtime exception
Solution
Step 1: Understand named companion object usage
The companion object named 'Factory' holds the function 'create' which returns "Box created".
Step 2: Analyze the main function call
Calling 'Box.Factory.create()' accesses the named companion object and calls 'create', printing "Box created".
Final Answer:
Box created -> Option B
Quick Check:
Box.Factory.create() prints "Box created" [OK]
Hint: Call named companion with ClassName.Name.method() [OK]
Common Mistakes:
Trying to call create() without Factory prefix
Expecting 'Factory created' output
Confusing syntax causing compile errors
4. Identify the error in this Kotlin code snippet:
class Sample {
companion object Companion {
fun info() = "Info"
}
}
fun main() {
println(Sample.companion.info())
}
medium
A. Incorrect companion object name usage; should be 'Companion' not 'companion'
B. Missing 'object' keyword in companion declaration
C. Function 'info' must be static
D. No error; code runs fine
Solution
Step 1: Check companion object name and usage
The companion object is named 'Companion' with uppercase C.
Step 2: Analyze the call in main()
The code calls 'Sample.companion.info()' with lowercase 'companion', which does not match the name and causes a compile error.
Final Answer:
Incorrect companion object name usage; should be 'Companion' not 'companion' -> Option A
Quick Check:
Use exact companion object name when calling [OK]
Hint: Match companion object name exactly (case sensitive) [OK]
Common Mistakes:
Using lowercase 'companion' instead of named object
Forgetting 'object' keyword
Assuming companion functions must be static
5. You want to create a Kotlin class Logger with a named companion object Factory that provides a method create() returning a new Logger instance. Which code correctly implements this?
hard
A. class Logger {
companion Factory object {
fun create() = Logger()
}
}
B. class Logger {
companion object {
fun create() = Logger()
}
}
C. class Logger {
object Factory companion {
fun create() = Logger()
}
}
D. class Logger {
companion object Factory {
fun create() = Logger()
}
}
Solution
Step 1: Recall correct syntax for named companion object
The correct syntax is 'companion object Factory { ... }' inside the class.
Step 2: Check method implementation
The method 'create()' returns a new instance of Logger using 'Logger()'. class Logger {
companion object Factory {
fun create() = Logger()
}
} matches both syntax and logic.
Final Answer:
class Logger {
companion object Factory {
fun create() = Logger()
}
} -> Option D
Quick Check:
Named companion object with factory method = class Logger {
companion object Factory {
fun create() = Logger()
}
} [OK]
Hint: Use 'companion object Name' and return new instance inside [OK]
Common Mistakes:
Swapping 'object' and 'companion' keywords
Omitting the companion object name
Incorrect order of keywords
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.