Classes and Objects
1. What is a class in Kotlin?
A class is a blueprint for creating objects. It defines properties and behaviors that the objects created from the class will have.
2. How do you define a class in Kotlin?
Use the class
keyword followed by the class name.
Kotlin
Copied
3. What is an object in Kotlin?
An object is an instance of a class. It represents a real-world entity and has its own unique state.
4. How do you create an instance of a class in Kotlin?
Use the val
or var
keyword followed by the instance name and the class
constructor.
Kotlin
Copied
5. Explain the difference between a class and an object.
A class is a blueprint or template for creating objects, while an object is an instance of a class.
6. What are properties in Kotlin classes?
Properties are the characteristics or attributes of a class. They define the state of an object.
Kotlin
Copied
7. What are methods in Kotlin classes?
Methods are functions defined within a class. They represent the behavior of the objects created from the class.
Kotlin
Copied
8. How is inheritance implemented in Kotlin?
Use the :
symbol followed by the name of the superclass after the subclass declaration.
Kotlin
Copied
9. Explain the concept of visibility modifiers in Kotlin.
Visibility modifiers (private
, protected
, internal
, public
) control the visibility of classes, properties, and methods.
Kotlin
Copied
10. What is the primary constructor in Kotlin?
The primary constructor is declared in the class header. It defines the properties of the class.
Kotlin
Copied
11. How do you create secondary constructors in Kotlin?
Use the constructor
keyword along with this
to define secondary constructors.
Kotlin
Copied
12. What is a data class in Kotlin?
A data class is a class specifically designed for holding data. It automatically generates equals()
, hashCode()
, toString()
, and copy()
methods.
Kotlin
Copied
13. Explain the init
block in Kotlin.
The init
block is executed when an instance of the class is created. It is used for additional initialization.
Kotlin
Copied
14. What is the purpose of the companion object
in Kotlin?
The companion object
is used to create static members and methods in a class.
Kotlin
Copied
15. Explain the difference between val
and var
in the context of class properties.
val
is used for read-only (immutable) properties, and var
is used for mutable properties.
Kotlin
Copied
16. How does Kotlin handle method overloading?
Method overloading is supported in Kotlin by defining multiple methods with the same name but different parameter lists.
Kotlin
Copied
17. What is an abstract class in Kotlin?
An abstract class cannot be instantiated and may contain abstract (unimplemented) methods. Subclasses must provide implementations.
Kotlin
Copied
18. How is a sealed class different from a regular class in Kotlin?
A sealed class is a limited hierarchy where all subclasses are known. It is often used in when
expressions to ensure exhaustive checks.
Kotlin
Copied
19. What is the purpose of the by lazy
delegate in Kotlin?
The by lazy
delegate is used for lazy initialization of properties. It computes the value only when the property is accessed for the first time.
Kotlin
Copied
20. Explain the object
keyword in Kotlin.
The object
keyword is used to define a singleton object – a class that can have only one instance.
Kotlin
Copied