0
0
Kotlinprogramming~10 mins

Why object declarations create singletons in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a singleton object named Logger.

Kotlin
object [1] {
    fun log(message: String) {
        println(message)
    }
}
Drag options to blanks, or click blank then click option'
Alogger
BLog
CLogger
DLogObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase names for object declarations.
Trying to declare an object with class keyword.
2fill in blank
medium

Complete the code to call the log function on the singleton object.

Kotlin
fun main() {
    [1].log("Hello Singleton")
}
Drag options to blanks, or click blank then click option'
ALogger
BLogger()
Cnew Logger()
Dlogger
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to create an instance with parentheses.
Using lowercase name that does not match the object.
3fill in blank
hard

Fix the error in the code to ensure only one instance of the object is used.

Kotlin
val logger1 = [1]
val logger2 = Logger
println(logger1 === logger2)
Drag options to blanks, or click blank then click option'
ALogger()
Blogger
Cnew Logger()
DLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses to create new instances.
Using lowercase names that do not match the object.
4fill in blank
hard

Fill both blanks to create a singleton object with a property and access it.

Kotlin
object [1] {
    val version = [2]
}
fun main() {
    println(AppInfo.version)
}
Drag options to blanks, or click blank then click option'
AAppInfo
B"1.0"
C1.0
DVersion
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number without quotes for a string property.
Using lowercase or incorrect object names.
5fill in blank
hard

Fill all three blanks to define a singleton object with a function and call it.

Kotlin
object [1] {
    fun greet(name: String) = "Hello, [2]!"
}
fun main() {
    println(Greeter.greet([3]))
}
Drag options to blanks, or click blank then click option'
AGreeter
B$name
C"Alice"
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using \$name instead of $name for string interpolation.
Passing a variable name instead of a string literal.