This quiz tests your understanding of the fundamental concepts of Object-Oriented Programming (OOP) in Kotlin. It covers key topics such as classes, objects, data encapsulation, constructors, methods, properties, companion objects, and nested classes. Each question is designed to reinforce your knowledge of how these concepts are implemented in Kotlin and how they contribute to building reusable, efficient, and organized code structures.
1.
What will happen if a class in Kotlin has both a primary constructor and a secondary constructor?
2.
In Kotlin, how do you call a method of a class instance?
3.
What is the purpose of the 'companion object' in a Kotlin class?
4.
How many primary constructors can a Kotlin class have?
5.
What is the output of the following code snippet?
class MyClass(val x: Int) {
init {
println(x * 2)
}
}
fun main() {
val instance = MyClass(5)
}
6.
Which keyword is used to initialize a property with a specific value when an instance is created in Kotlin?
7.
When should you use a custom accessor in Kotlin?
8.
What is a nested class in Kotlin?
9.
Which of the following code snippets creates an instance of a class using a secondary constructor?
10.
What is the purpose of the 'val' keyword in a primary constructor parameter?
11.
Which of the following Kotlin code snippets is correct for adding a secondary constructor?
12.
What is an object in Kotlin?
13.
How do you access a property of a class instance in Kotlin?
14.
What keyword is used to declare a nested class that can access properties of the outer class?
15.
Which statement is true about companion objects in Kotlin?
16.
How do you declare a custom getter for a property in Kotlin?
17.
What is the purpose of an initializer block in Kotlin?
18.
In Kotlin, which keyword is used to refer to the current class instance within a class?
19.
What is a primary constructor in Kotlin?
20.
Which of the following code snippets correctly creates an instance of a class named Customer?
21.
How do you declare a class in Kotlin?
22.
What is the purpose of data encapsulation in object-oriented programming?
23.
Which of the following best defines a class in Kotlin?