Object-Oriented Programming (OOP) in Kotlin focuses on concepts like inheritance, interfaces, and abstract classes. These key concepts help developers design structured and reusable code for modern applications.
Object-Oriented Programming in Kotlin: MCQs
1. Inheritance
What is the purpose of inheritance in Kotlin?
a) Reusing code from other classes
b) Creating new classes
c) Defining new interfaces
d) Protecting data
Which keyword is used to inherit a class in Kotlin?
a) extends
b) :
c) super
d) inherits
How do you prevent a class from being inherited in Kotlin?
a) Use open keyword
b) Use final keyword
c) Use abstract keyword
d) Use sealed keyword
Can a class in Kotlin inherit from multiple classes?
a) Yes, through multiple inheritance
b) No, Kotlin supports single inheritance only
c) Yes, if the classes are interfaces
d) Yes, but only from abstract classes
What is true about the constructor of a class in Kotlin?
a) The constructor is always inherited
b) The constructor must be explicitly called in the subclass
c) Constructors are not inherited in Kotlin
d) Only primary constructor is inherited
Which keyword is used to call the superclass constructor in Kotlin?
a) super()
b) base()
c) this()
d) parent()
What will happen if you don’t declare a constructor in the subclass of a class in Kotlin?
a) It will automatically call the default constructor
b) The code will fail to compile
c) It will be inherited from a superclass
d) The superclass constructor must be explicitly called
How does Kotlin handle method overriding in inheritance?
a) You cannot override methods in Kotlin
b) Methods are automatically overridden
c) Methods must be marked with the override keyword
d) Methods are abstract by default
Can a method in a superclass be private and still be inherited in Kotlin?
a) Yes, but it must be explicitly declared
b) No, private methods cannot be inherited
c) Yes, private methods are inherited as public
d) Only protected methods can be inherited
What does the open keyword do in Kotlin?
a) Allows the class or method to be overridden
b) Prevents the class or method from being overridden
c) Makes the class or method public
d) Initializes the class
2. Interfaces
In Kotlin, how can a class implement an interface?
a) By using the extends keyword
b) By using the implements keyword
c) By using the : operator
d) By using the interface keyword
What is a primary characteristic of an interface in Kotlin?
a) It can have both abstract and concrete methods
b) It can have only concrete methods
c) It can have only properties
d) It cannot have any methods
Can an interface have default implementations for methods in Kotlin?
a) No, interfaces can have only abstract methods
b) Yes, using default keyword
c) Yes, by defining methods with a body
d) Only abstract classes can have default methods
Can a class implement multiple interfaces in Kotlin?
a) No, only one interface can be implemented
b) Yes, but only if they don’t have conflicting methods
c) Yes, Kotlin supports multiple inheritance through interfaces
d) Yes, but only abstract classes can do this
Can an interface contain properties in Kotlin?
a) No, interfaces cannot contain properties
b) Yes, but the properties must be abstract
c) Yes, but the properties must be final
d) Yes, interfaces can contain properties with getters and setters
How are abstract methods defined in Kotlin interfaces?
a) Using the abstract keyword
b) Using the open keyword
c) Methods in interfaces are always abstract
d) Using the abstract and open keywords
What happens if a class implements an interface but doesn’t provide an implementation for all abstract methods?
a) The class compiles successfully with default values for the methods
b) The class becomes an abstract class
c) The code will fail to compile
d) The compiler will automatically implement the missing methods
How do you call the method from an interface when it is implemented in a class?
a) Using super.method()
b) Using interface.method()
c) Using class.method()
d) Directly calling method() without prefix
Can an interface in Kotlin extend another interface?
a) No, interfaces cannot extend other interfaces
b) Yes, interfaces can extend other interfaces
c) Yes, but only abstract classes can extend interfaces
d) No, but interfaces can implement other interfaces
How do you declare an interface in Kotlin?
a) class InterfaceName
b) interface InterfaceName
c) abstract class InterfaceName
d) sealed interface InterfaceName
3. Abstract Classes
What is the purpose of an abstract class in Kotlin?
a) To create instances of the class
b) To serve as a base class for other classes
c) To define concrete methods
d) To provide a collection of objects
Which keyword is used to define an abstract class in Kotlin?
a) abstract
b) sealed
c) interface
d) open
Can an abstract class in Kotlin have a constructor?
a) Yes, but it must be explicitly defined as abstract
b) No, abstract classes cannot have constructors
c) Yes, constructors are inherited
d) Yes, the constructor can be defined as normal
Can an abstract class have implemented methods in Kotlin?
a) No, abstract classes cannot have method implementations
b) Yes, abstract classes can have both abstract and implemented methods
c) Yes, abstract classes can only have implemented methods
d) No, all methods must be abstract
What will happen if you try to create an instance of an abstract class in Kotlin?
a) The class will compile but cannot be instantiated
b) The code will fail to compile
c) The class will be instantiated as a default class
d) The object will be created as null
What is the default visibility of methods in an abstract class in Kotlin?
a) Public
b) Private
c) Internal
d) Protected
Can you override a method from an abstract class in Kotlin?
a) No, abstract class methods cannot be overridden
b) Yes, you must use the override keyword
c) Yes, but only in abstract methods
d) Yes, using the final keyword
Can an abstract class extend another abstract class in Kotlin?
a) No, abstract classes cannot extend other abstract classes
b) Yes, abstract classes can extend other abstract classes
c) Yes, but the subclass must provide method implementations
d) No, only interfaces can extend abstract classes
How do you declare an abstract method in Kotlin?
a) abstract fun methodName()
b) fun methodName()
c) abstract methodName()
d) open fun methodName()
Can an abstract class implement an interface in Kotlin?
a) No, abstract classes cannot implement interfaces
b) Yes, abstract classes can implement interfaces
c) Yes, but only concrete classes can implement interfaces
d) No, only concrete classes can implement interfaces
Answers
QNo
Answer
1
a) Reusing code from other classes
2
b) :
3
b) Use final keyword
4
b) No, Kotlin supports single inheritance only
5
c) Constructors are not inherited in Kotlin
6
a) super()
7
a) It will automatically call the default constructor
8
c) Methods must be marked with the override keyword
9
b) No, private methods cannot be inherited
10
a) Allows the class or method to be overridden
11
c) By using the : operator
12
a) It can have both abstract and concrete methods
13
c) Yes, by defining methods with a body
14
c) Yes, Kotlin supports multiple inheritance through interfaces
15
b) Yes, but the properties must be abstract
16
c) Methods in interfaces are always abstract
17
b) The class becomes an abstract class
18
b) Using interface.method()
19
b) Yes, interfaces can extend other interfaces
20
b) interface InterfaceName
21
b) To serve as a base class for other classes
22
a) abstract
23
a) Yes, but it must be explicitly defined as abstract
24
b) Yes, abstract classes can have both abstract and implemented methods
25
b) The code will fail to compile
26
a) Public
27
b) Yes, you must use the override keyword
28
b) Yes, abstract classes can extend other abstract classes