0
0
Kotlinprogramming~5 mins

Object declaration syntax in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an object declaration in Kotlin?
An object declaration in Kotlin creates a singleton object, which means only one instance of that object exists throughout the program.
Click to reveal answer
beginner
How do you declare a simple object named Logger in Kotlin?
Use the syntax:
object Logger {
    fun log(message: String) {
        println(message)
    }
}
Click to reveal answer
intermediate
Can an object declaration have properties and functions in Kotlin?
Yes, an object declaration can have properties and functions just like a class, but it cannot have constructors because it is a singleton.
Click to reveal answer
intermediate
What is the difference between an object declaration and a class in Kotlin?
An object declaration creates a single instance automatically (singleton), while a class can have many instances created using constructors.
Click to reveal answer
beginner
How do you access a function inside an object declaration?
You access it directly by the object name, for example: Logger.log("Hello") without creating an instance.
Click to reveal answer
What keyword is used to declare a singleton object in Kotlin?
Ainstance
Bclass
Csingleton
Dobject
Can an object declaration in Kotlin have a constructor?
AOnly secondary constructors are allowed
BYes, it can have primary and secondary constructors
CNo, object declarations cannot have constructors
DOnly primary constructors are allowed
How do you call a function named printMessage inside an object named Notifier?
AprintMessage()
BNotifier.printMessage()
CNotifier().printMessage()
Dnew Notifier().printMessage()
Which of the following is true about Kotlin object declarations?
AThey are instantiated lazily and only once
BThey require explicit instantiation
CThey can have multiple instances
DThey cannot have functions
What happens if you try to create an instance of an object declaration using a constructor?
AIt throws a compile-time error
BIt creates a new instance
CIt creates a copy of the object
DIt returns null
Explain how to declare and use an object declaration in Kotlin.
Think about how you create a single shared tool or helper in your program.
You got /4 concepts.
    Describe the differences between a Kotlin object declaration and a class.
    Consider how many copies you want in your program.
    You got /4 concepts.