• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

Question - 61 : - Can we use the new keyword to instantiate a class object in Kotlin?

Answer - 61 : -

No, in Kotlin we don't have to use the new keyword to instantiate a class object. To instantiate a class object, simply we use:

var varName = ClassName()

Question - 62 : - What are visibility modifiers in Kotlin?

Answer - 62 : -

A visibility modifier or access specifier or access modifier is a concept that is used to define the scope of something in a programming language. In Kotlin, we have four visibility modifiers:

  • private: visible inside that particular class or file containing the declaration.
  • protected: visible inside that particular class or file and also in the subclass of that particular class where it is declared.
  • internal: visible everywhere in that particular module.
  • public: visible to everyone.

Question - 63 : - How to create a Singleton class in Kotlin?

Answer - 63 : -

A singleton class is a class that is defined in such a way that only one instance of the class can be created and is used where we need only one instance of the class like in logging, database connections, etc.

To create a Singleton class in Kotlin, you need to use the object keyword.

object AnySingletonClassName

Question - 64 : - What are init blocks in Kotlin?

Answer - 64 : -

init blocks are initializer blocks that are executed just after the execution of the primary constructor. A class file can have one or more init blocks that will be executed in series. If you want to perform some operation in the primary constructor, then it is not possible in Kotlin, for that, you need to use the init block.

Question - 65 : - What are the types of constructors in Kotlin?

Answer - 65 : -

  • Primary constructor: These constructors are defined in the class header and you can't perform some operation in it, unlike Java's constructor.
  • Secondary constructor: These constructors are declared inside the class body by using the constructor keyword. You must call the primary constructor from the secondary constructor explicitly. Also, the property of the class can’t be declared inside the secondary constructor. There can be more than one secondary constructors in Kotlin.

Question - 66 : -
Is there any relationship between primary and secondary constructors?

Answer - 66 : -

Yes, when using a secondary constructor, you need to call the primary constructor explicitly.

Question - 67 : - What is the default type of argument used in a constructor?

Answer - 67 : -

By default, the type of arguments of a constructor in val. But you can change it to var explicitly.

Question - 68 : - What is suspend function in Kotlin Coroutines?

Answer - 68 : -

Suspend function is the building block of the Coroutines in Kotlin. Suspend function is a function that could be started, paused, and resume. To use a suspend function, we need to use the suspend keyword in our normal function definition.

Question - 69 : - How to choose between a switch and when in Kotlin?

Answer - 69 : -

Whenever we want to handle many if-else conditions, then we generally use switch-case statements. But Kotlin provides a more concise option i.e. in Kotlin, we can use when in place of the switch. And, when can be used as:

arbitrary condition expression
without argument
with two or more choices
For example:

when(number) {
 1 -> println("One")
 2, 3 -> println("Two or Three")
 4 -> println("Four")
 else -> println("Number is not between 1 and 4")

Question - 70 : - What is the open keyword in Kotlin used for?

Answer - 70 : - By default, the classes and functions are final in Kotlin. So, you can't inherit the class or override the functions. To do so, you need to use the open keyword before the class and function.

NCERT Solutions


Share your email for latest updates


Our partners