0
0
Kotlinprogramming~5 mins

Why object declarations create singletons in Kotlin - Quick Recap

Choose your learning style9 modes available
Recall & Review
beginner
What is an object declaration in Kotlin?
An <code>object</code> declaration defines a class and creates its single instance at the same time. It is a way to create a singleton easily.
Click to reveal answer
beginner
Why does an object declaration create a singleton?
Because Kotlin creates exactly one instance of the object when it is first accessed, and reuses that instance everywhere.
Click to reveal answer
intermediate
How does Kotlin ensure only one instance of an object exists?
Kotlin initializes the object lazily and stores the instance internally, so all references point to the same object.
Click to reveal answer
beginner
What is a real-life example of a singleton?
A single remote control for a TV: you only have one remote to control the TV, just like one instance of an object controls shared behavior.
Click to reveal answer
beginner
Can you create multiple instances of an object declaration?
No, Kotlin does not allow creating multiple instances of an object. It is designed to have only one instance.
Click to reveal answer
What does an object declaration in Kotlin automatically create?
AMultiple instances
BA single instance (singleton)
CAn interface
DA function
When is the instance of an object created in Kotlin?
AWhen first accessed (lazy initialization)
BNever
CEvery time it is referenced
DAt compile time
Can you instantiate an object declaration using a constructor?
ANo, it has no constructor
BYes, like a normal class
COnly if it is a companion object
DOnly in Java
Which of these is a benefit of using object declarations for singletons?
ARequires manual instance management
BMultiple instances
CNo initialization
DAutomatic thread safety
What keyword would you use to declare a singleton in Kotlin?
Ainterface
Bclass
Cobject
Ddata
Explain why Kotlin's object declarations create singletons and how this differs from regular classes.
Think about how Kotlin manages the instance and what happens when you access it.
You got /4 concepts.
    Describe a real-life example that helps you understand the concept of a singleton created by an object declaration.
    Consider something you only have one of that controls or manages something.
    You got /3 concepts.