Bird
Raised Fist0
Kotlinprogramming~20 mins

Object declaration syntax in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Object Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Kotlin object declaration?
Consider the following Kotlin code. What will be printed when Main.main() is run?
Kotlin
object Counter {
    var count = 0
    fun increment() {
        count++
    }
}

fun main() {
    Counter.increment()
    Counter.increment()
    println(Counter.count)
}
A2
B0
CCompilation error
D1
Attempts:
2 left
💡 Hint
Remember that object declarations create a singleton instance.
❓ Predict Output
intermediate
2:00remaining
What error does this Kotlin object declaration cause?
What error will this Kotlin code produce when compiled?
Kotlin
object Sample {
    val x: Int
}

fun main() {
    println(Sample.x)
}
ACompilation error: Property must be initialized or be abstract
BRuntime error: UninitializedPropertyAccessException
CPrints 0
DNo error, prints null
Attempts:
2 left
💡 Hint
Check if properties in objects need initialization.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin object declaration cause a runtime error?
This Kotlin code compiles but throws an exception at runtime. What causes the error?
Kotlin
object Config {
    val data = loadData()

    fun loadData(): String {
        throw IllegalStateException("Failed to load")
    }
}

fun main() {
    println(Config.data)
}
ANull pointer exception because data is null
BException thrown during object initialization blocks main thread
CCompilation error due to missing return type
DNo error, prints "Failed to load"
Attempts:
2 left
💡 Hint
Object initialization runs when first accessed.
📝 Syntax
advanced
2:00remaining
Which Kotlin object declaration syntax is correct?
Which of the following Kotlin object declarations is syntactically correct?
A
object Logger {
    fun log(message: String)
        println(message)
}
B
object Logger {
    fun log(message: String) = {
        println(message)
    }
}
C
object Logger {
    fun log(message: String) {
        println(message)
    }
}
D
}
}    
)egassem(nltnirp        
{ )gnirtS :egassem(gol nuf    
{ reggoL tcejbo
Attempts:
2 left
💡 Hint
Check for proper function body braces and object closing brace.
🚀 Application
expert
2:00remaining
How many instances of this Kotlin object are created?
Given this Kotlin code, how many instances of Database are created during program execution?
Kotlin
object Database {
    init {
        println("Database initialized")
    }

    fun query() = "data"
}

fun main() {
    println(Database.query())
    println(Database.query())
}
AMultiple instances, one per println
BTwo instances, one per query call
CNo instances, object is static
DOne instance, initialized once
Attempts:
2 left
💡 Hint
Objects in Kotlin are singletons by design.

Practice

(1/5)
1. What does the object keyword in Kotlin do?
easy
A. Creates a function that returns an object
B. Defines a class that can be instantiated multiple times
C. Declares a variable of type Object
D. Creates a single instance with properties and functions

Solution

  1. Step 1: Understand the object keyword purpose

    The object keyword creates a singleton, meaning one single instance with properties and functions.
  2. Step 2: Compare with other options

    Classes can be instantiated multiple times, variables don't define singletons, and functions returning objects are different concepts.
  3. Final Answer:

    Creates a single instance with properties and functions -> Option D
  4. Quick Check:

    object keyword = single instance [OK]
Hint: Remember: object = one single instance [OK]
Common Mistakes:
  • Confusing object with class
  • Thinking object creates multiple instances
  • Mixing object with variable declaration
2. Which of the following is the correct syntax to declare an object named Config in Kotlin?
easy
A. object Config { }
B. val Config = object { }
C. class Config { }
D. fun Config() { }

Solution

  1. Step 1: Identify the object declaration syntax

    The correct syntax to declare a singleton object is using the object keyword followed by the name and curly braces.
  2. Step 2: Check other options

    class Config { } declares a class, B declares an anonymous object assigned to a val, and D declares a function, so they are incorrect.
  3. Final Answer:

    object Config { } -> Option A
  4. Quick Check:

    object keyword + name + braces = correct syntax [OK]
Hint: Use 'object Name { }' to declare singleton objects [OK]
Common Mistakes:
  • Using class instead of object
  • Assigning anonymous object to val instead
  • Using function syntax by mistake
3. What is the output of this Kotlin code?
object Logger {
    fun log(msg: String) = println("Log: $msg")
}

fun main() {
    Logger.log("Start")
}
medium
A. Log: Start
B. Start
C. Logger.log(Start)
D. Compilation error

Solution

  1. Step 1: Understand object usage

    The object Logger has a function log that prints "Log: " plus the message.
  2. Step 2: Trace the main function call

    Calling Logger.log("Start") prints "Log: Start" to the console.
  3. Final Answer:

    Log: Start -> Option A
  4. Quick Check:

    Logger.log("Start") prints "Log: Start" [OK]
Hint: Object functions called by object name print their output [OK]
Common Mistakes:
  • Expecting just the message without prefix
  • Thinking it prints the function call text
  • Assuming compilation error due to object usage
4. Identify the error in this Kotlin object declaration:
object Data {
    var count: Int
    fun increment() {
        count++
    }
}
medium
A. Cannot use 'var' inside object
B. Missing initialization for 'count' property
C. Function 'increment' should return Int
D. Object cannot have properties

Solution

  1. Step 1: Check property declaration

    The property 'count' is declared but not initialized or marked as abstract, which is invalid in objects.
  2. Step 2: Validate other options

    Objects can have vars, functions can be Unit-returning, and objects can have properties, so other options are incorrect.
  3. Final Answer:

    Missing initialization for 'count' property -> Option B
  4. Quick Check:

    Properties in objects must be initialized [OK]
Hint: Always initialize properties in objects or use lateinit [OK]
Common Mistakes:
  • Forgetting to initialize properties
  • Thinking vars are disallowed in objects
  • Expecting functions to always return a value
5. You want to create a singleton object Cache that stores key-value pairs and provides a function getValue(key: String): String?. Which of the following is the best Kotlin object declaration?
hard
A. fun Cache() = mutableMapOf() fun getValue(key: String) = Cache()[key]
B. class Cache { val map = mutableMapOf() fun getValue(key: String) = map[key] }
C. object Cache { private val map = mutableMapOf() fun getValue(key: String) = map[key] }
D. val Cache = mapOf() fun getValue(key: String) = Cache[key]

Solution

  1. Step 1: Identify singleton pattern

    Using object creates a singleton Cache with a private mutable map and a function to get values.
  2. Step 2: Compare other options

    class Cache { val map = mutableMapOf() fun getValue(key: String) = map[key] } is a class needing instances, C is a function returning a new map each time, D uses an immutable map and separate function, not encapsulated.
  3. Final Answer:

    object Cache { private val map = mutableMapOf() fun getValue(key: String) = map[key] } -> Option C
  4. Quick Check:

    Singleton object with private mutable map = object Cache { private val map = mutableMapOf() fun getValue(key: String) = map[key] } [OK]
Hint: Use object with private mutable map for singleton cache [OK]
Common Mistakes:
  • Using class instead of object for singleton
  • Returning new map each call instead of storing
  • Using immutable map when mutation is needed
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