Bird
Raised Fist0
Kotlinprogramming~20 mins

Named companion objects in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Companion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of accessing a named companion object property
What is the output of this Kotlin code?
Kotlin
class Example {
    companion object Factory {
        val value = 42
    }
}

fun main() {
    println(Example.Factory.value)
}
A42
BFactory
Cnull
DCompilation error
Attempts:
2 left
💡 Hint
Access the property using the named companion object.
🧠 Conceptual
intermediate
1:30remaining
Purpose of naming a companion object
Why would you name a companion object in Kotlin instead of leaving it unnamed?
ATo provide a specific name for better code clarity and explicit access
BTo allow multiple companion objects in the same class
CTo make the companion object private
DTo enable inheritance from the companion object
Attempts:
2 left
💡 Hint
Think about how naming affects code readability and access.
🔧 Debug
advanced
2:00remaining
Identify the error when accessing an unnamed companion object with a name
What error occurs when you try to access an unnamed companion object using a name?
Kotlin
class Sample {
    companion object {
        val data = "Hello"
    }
}

fun main() {
    println(Sample.Factory.data)
}
ACompilation succeeds and prints 'Hello'
BUnresolved reference: Factory
CNullPointerException at runtime
DType mismatch error
Attempts:
2 left
💡 Hint
Check if the companion object has the name you are using.
📝 Syntax
advanced
1:30remaining
Correct syntax to declare a named companion object
Which of the following is the correct syntax to declare a named companion object in Kotlin?
A
class MyClass {
    object companion Factory {
        val x = 10
    }
}
B
class MyClass {
    companion Factory {
        val x = 10
    }
}
C
class MyClass {
    companion object Factory {
        val x = 10
    }
}
D
class MyClass {
    companion object {
        val x = 10
    } Factory
}
Attempts:
2 left
💡 Hint
Remember the keyword order for companion object naming.
🚀 Application
expert
2:30remaining
Using named companion object to implement a factory method
Given the class below, which option correctly uses the named companion object to create an instance with a factory method?
Kotlin
class User private constructor(val name: String) {
    companion object Creator {
        fun create(name: String): User {
            return User(name)
        }
    }
}

fun main() {
    val user = ???
    println(user.name)
}
AUser.create("Alice")
BUser.Creator("Alice")
CUser.Companion.create("Alice")
DUser.Creator.create("Alice")
Attempts:
2 left
💡 Hint
Use the named companion object to call the factory method.

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

  1. Step 1: Understand companion objects

    Companion objects are singletons inside a class used to hold members related to the class itself.
  2. 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.
  3. Final Answer:

    To give a clear and specific name to the singleton object inside a class -> Option A
  4. Quick Check:

    Named companion object = clear singleton name [OK]
Hint: Named companion objects clarify singleton usage [OK]
Common Mistakes:
  • Thinking companion objects create multiple instances
  • Confusing naming with inheritance
  • Assuming naming restricts access
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

  1. Step 1: Recall correct companion object syntax

    The correct syntax is 'companion object Name { ... }' inside the class.
  2. 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.
  3. Final Answer:

    class MyClass { companion object Named { fun greet() = "Hi" } } -> Option C
  4. 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

  1. Step 1: Understand named companion object usage

    The companion object named 'Factory' holds the function 'create' which returns "Box created".
  2. Step 2: Analyze the main function call

    Calling 'Box.Factory.create()' accesses the named companion object and calls 'create', printing "Box created".
  3. Final Answer:

    Box created -> Option B
  4. 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

  1. Step 1: Check companion object name and usage

    The companion object is named 'Companion' with uppercase C.
  2. 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.
  3. Final Answer:

    Incorrect companion object name usage; should be 'Companion' not 'companion' -> Option A
  4. 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

  1. Step 1: Recall correct syntax for named companion object

    The correct syntax is 'companion object Factory { ... }' inside the class.
  2. 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.
  3. Final Answer:

    class Logger { companion object Factory { fun create() = Logger() } } -> Option D
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard