0
0
Kotlinprogramming~20 mins

Why object declarations create singletons in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Singleton Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of accessing object properties
What is the output of this Kotlin code using an object declaration?
Kotlin
object Config {
    val version = 1
    val name = "App"
}

fun main() {
    println(Config.version)
    println(Config.name)
}
ACompilation error
B
version
name
C
Config.version
Config.name
D
1
App
Attempts:
2 left
💡 Hint
Object declarations create a single instance accessible by the name.
🧠 Conceptual
intermediate
2:00remaining
Why does Kotlin object declaration create a singleton?
Why does an object declaration in Kotlin create a singleton instance?
ABecause it defines a class with a private constructor and a single instance is created automatically
BBecause it is a function that returns a new object each call
CBecause it is a variable holding a mutable object
DBecause it defines a class and creates a new instance every time
Attempts:
2 left
💡 Hint
Think about how Kotlin restricts instance creation for object declarations.
Predict Output
advanced
2:00remaining
Comparing object instances
What is the output of this Kotlin code comparing two references to the same object declaration?
Kotlin
object Logger {
    fun log(msg: String) = println("Log: $msg")
}

fun main() {
    val a = Logger
    val b = Logger
    println(a === b)
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
The '===' operator checks if two references point to the same object.
🔧 Debug
advanced
2:00remaining
Why does this code fail to compile?
Why does this Kotlin code fail to compile?
Kotlin
object Counter {
    var count = 0
}

fun main() {
    val c = Counter()
    println(c.count)
}
AThe 'count' property is private and inaccessible
BObject declarations cannot be instantiated with parentheses
CThe object declaration must have a constructor
DThe variable 'c' is declared with val instead of var
Attempts:
2 left
💡 Hint
Remember how object declarations are accessed in Kotlin.
🚀 Application
expert
3:00remaining
Using object declaration for thread-safe singleton
Which Kotlin object declaration ensures thread-safe lazy initialization of a singleton?
Kotlin
object Database {
    init {
        println("Initializing database connection")
    }
    fun query(sql: String) = "Result for: $sql"
}

fun main() {
    println(Database.query("SELECT * FROM users"))
    println(Database.query("SELECT * FROM orders"))
}
AThe object declaration is initialized lazily and thread-safely on first access
BThe object declaration creates a new instance on each access
CThe object declaration requires manual synchronization for thread safety
DThe object declaration is initialized eagerly at program start
Attempts:
2 left
💡 Hint
Kotlin object declarations are thread-safe by default and initialized on first use.