Bird
Raised Fist0
Kotlinprogramming~20 mins

Companion vs top-level functions decision in Kotlin - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Companion vs Top-level Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of companion object function call
What is the output of this Kotlin code when calling MyClass.greet()?
Kotlin
class MyClass {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun main() {
    println(MyClass.greet())
}
AHello from companion
BCompilation error: greet is not a member of MyClass
CHello from top-level function
DRuntime error: Unresolved reference greet
Attempts:
2 left
💡 Hint
Companion object functions can be called using the class name.
🧠 Conceptual
intermediate
2:00remaining
Choosing between companion and top-level functions
Which situation is best suited for using a companion object function instead of a top-level function in Kotlin?
AWhen you want to define a function that can be called without importing
BWhen the function is a utility unrelated to any class
CWhen you want to avoid creating any class in your code
DWhen the function logically belongs to a class and needs access to its private members
Attempts:
2 left
💡 Hint
Think about encapsulation and access to class internals.
❓ Predict Output
advanced
2:00remaining
Output of top-level vs companion function with same name
What is the output of this Kotlin code?
Kotlin
fun greet() = "Hello from top-level"

class MyClass {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun main() {
    println(greet())
    println(MyClass.greet())
}
ACompilation error: greet is ambiguous
B
Hello from top-level
Hello from companion
C
Hello from companion
Hello from top-level
DRuntime error: greet not found
Attempts:
2 left
💡 Hint
Top-level and companion functions with the same name are called differently.
🔧 Debug
advanced
2:00remaining
Why does this companion object function cause a compilation error?
Consider this Kotlin code snippet: class MyClass { companion object { fun printMessage() { println(message) } } private val message = "Hello" } Why does this code cause a compilation error?
ACompanion object cannot access instance properties like 'message'
BThe 'message' property is not initialized
CThe function printMessage must be marked as static
DThe companion object must be named explicitly
Attempts:
2 left
💡 Hint
Think about what companion objects can access inside a class.
🧠 Conceptual
expert
2:00remaining
Best practice for organizing utility functions in Kotlin
You have several utility functions that do not require access to any class members. What is the best practice for organizing these functions in Kotlin?
APlace them inside an object declaration to simulate static methods
BPlace them inside a companion object of a class
CPlace them as top-level functions in a Kotlin file
DPlace them as member functions inside a class instance
Attempts:
2 left
💡 Hint
Consider simplicity and idiomatic Kotlin style.

Practice

(1/5)
1. Which statement best describes when to use a companion object function in Kotlin?
easy
A. When the function logically belongs to a class and should be called with the class name.
B. When the function is a general helper unrelated to any class.
C. When you want to create an instance method that requires an object.
D. When you want to define a function inside a function.

Solution

  1. Step 1: Understand companion object purpose

    Companion object functions belong to a class and are called using the class name without needing an instance.
  2. Step 2: Compare with other options

    Top-level functions are for general helpers, instance methods require an object, and functions inside functions are local functions.
  3. Final Answer:

    When the function logically belongs to a class and should be called with the class name. -> Option A
  4. Quick Check:

    Companion functions = class-related [OK]
Hint: Use companion for class-related static-like functions [OK]
Common Mistakes:
  • Confusing companion functions with instance methods
  • Using companion for unrelated helper functions
  • Thinking companion functions need an object instance
2. Which of the following is the correct syntax to declare a companion function named create inside a Kotlin class MyClass?
easy
A. class MyClass { fun companion create() { } }
B. class MyClass { companion object { fun create() { } } }
C. class MyClass { companion fun create() { } }
D. companion object MyClass { fun create() { } }

Solution

  1. Step 1: Recall companion object syntax

    In Kotlin, companion functions are declared inside a companion object block within the class.
  2. Step 2: Check each option

    class MyClass { companion object { fun create() { } } } correctly declares a companion object with a function. Syntaxes like fun companion create() { } and companion fun create() { } are invalid. companion object MyClass { fun create() { } } incorrectly places companion object outside the class.
  3. Final Answer:

    class MyClass { companion object { fun create() { } } } -> Option B
  4. Quick Check:

    Companion object syntax = class MyClass { companion object { fun create() { } } } [OK]
Hint: Companion functions go inside companion object block [OK]
Common Mistakes:
  • Writing companion fun instead of inside companion object
  • Placing companion object outside the class
  • Using fun companion instead of companion object
3. What will be the output of the following Kotlin code?
class Example {
    companion object {
        fun greet() = "Hello from companion"
    }
}

fun greet() = "Hello from top-level"

fun main() {
    println(Example.greet())
    println(greet())
}
medium
A. Compilation error due to function name conflict
B. "Hello from top-level" followed by "Hello from companion"
C. "Hello from companion" followed by "Hello from top-level"
D. Runtime error due to ambiguous call

Solution

  1. Step 1: Identify function calls

    Example.greet() calls the companion object function returning "Hello from companion". The top-level greet() returns "Hello from top-level".
  2. Step 2: Understand call order in main()

    First println prints Example.greet() output, second println prints top-level greet() output.
  3. Final Answer:

    "Hello from companion" followed by "Hello from top-level" -> Option C
  4. Quick Check:

    Companion vs top-level calls = "Hello from companion" followed by "Hello from top-level" [OK]
Hint: ClassName.func() calls companion; direct call is top-level [OK]
Common Mistakes:
  • Assuming name conflict causes error
  • Mixing up which greet() is called
  • Expecting runtime ambiguity error
4. Consider this Kotlin code snippet:
class Data {
    companion object {
        fun info() = "Data info"
    }
}

fun main() {
    println(info())
}

What is the error and how to fix it?
medium
A. Error: Unresolved reference 'info'. Fix: Call as Data.info()
B. Error: Companion object missing. Fix: Add companion object
C. Error: info() must be top-level. Fix: Move info() outside class
D. No error, prints "Data info"

Solution

  1. Step 1: Identify the call to info()

    The main function calls info() directly, but info() is inside Data's companion object, so it must be called with Data.info().
  2. Step 2: Fix the call

    Change println(info()) to println(Data.info()) to access the companion function correctly.
  3. Final Answer:

    Error: Unresolved reference 'info'. Fix: Call as Data.info() -> Option A
  4. Quick Check:

    Companion functions need class name prefix [OK]
Hint: Call companion functions with ClassName.function() [OK]
Common Mistakes:
  • Calling companion function without class name
  • Thinking companion functions are top-level
  • Trying to move companion function outside class
5. You want to create a utility function formatDate() used by multiple unrelated classes in your Kotlin project. Which approach is best and why?
hard
A. Put formatDate() inside a companion object of one class and call it via that class.
B. Define formatDate() inside a nested object inside each class.
C. Make formatDate() an instance method in each class that needs it.
D. Define formatDate() as a top-level function in a utility file for easy reuse.

Solution

  1. Step 1: Analyze function usage

    The function is a utility used by many unrelated classes, so it should be accessible without class dependency.
  2. Step 2: Choose best location

    Top-level functions are ideal for general helpers to avoid unnecessary class coupling. Companion objects tie functions to one class, which is not suitable here.
  3. Final Answer:

    Define formatDate() as a top-level function in a utility file for easy reuse. -> Option D
  4. Quick Check:

    General helpers = top-level functions [OK]
Hint: Use top-level for shared helpers, companion for class-specific [OK]
Common Mistakes:
  • Placing utility in companion object of unrelated class
  • Duplicating instance methods in multiple classes
  • Using nested objects unnecessarily
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