Kotlin interview questions and answers 👇

  1. Kotlin Interview Questions


Kotlin Interview Questions

1.

What are companion objects in Kotlin?

In Kotlin, if we want to write a function or any member of the class that can be called without having the instance of the class then we can write the same as a member of a companion object inside the class. So, by declaring the companion object, we can access the members of the class by class name only (without explicitly creating the instance of the class).

2.

What are visibility modifiers in Kotlin?

In Kotlin, visibility modifiers are used to restrict the accessibility of classes, objects, interfaces, constructors, functions, properties, and their setters to a certain level.

3.

Explain constructors in Kotlin

A constructor is a special member function that is invoked when an object of the class is created primarily to initialize variables or properties. A class needs to have a constructor and if we do not declare a constructor, then the compiler generates a default constructor.

Kotlin has two types of constructors:

  • Primary Constructor
  • Secondary Constructor
4.

Compare FlatMap and Map in Kotlin.

  • FlatMap is used to combine all the items of lists into one list.
  • Map is used to transform a list based on certain conditions.
5.

What is suspend function in Kotlin Coroutines?

Suspending functions are at the core of everything coroutines. A suspending function is a function that can be paused and resumed at a later time. They can execute a long running operation and wait for it to complete without blocking.

6.

What are Higher-Order functions?

A higher-order function is a function that takes functions as parameters, or returns a function.

7.

How are variables declared in Kotlin?

Every variable in Kotlin must be declared before it can be used. An attempt to use a variable without declaring it results in a syntax error.

8.

What is the benefit of Kotlin over Java?

The Kotlin programing language is simpler and cleaner than Java. It removes a lot of redundancies in code as compared to Java. Kotlin also offers some useful features that Java doesn't yet support, making the code more idiomatic. Kotlin has been added to Android Studio's list of supported languages.

9.

What is the difference between == operator and === operator?

The == operator is used to compare the values stored in variables and the === operator is used to check if the reference of the variables are equal or not. But in the case of primitive types, the === operator also checks for the value and not reference.

10.

What is an infix function in Kotlin?

Kotlin allows some functions to be called without using period and brackets. These are called infix methods, and their use can result in code that looks much more like a natural language.

11.

What is the difference between the variable declaration with var and val?

If you want to declare a mutable (changeable) variable, then you can use var. For the immutable variable, you should use val i.e. val variables can't be changed once assigned.

12.

What is an inline function in Kotlin?

An inline function is declared with the keyword inline. The use of inline function enhances the performance of higher order function. The inline function tells the compiler to copy parameters and functions to the call site.

13.

What is the open keyword?

In Kotlin, all classes are final by default i.e. they can’t be inherited. This is opposite of what happens in Java- you have to make your class final explicitly.

So, to make a class inheritable to the other classes, you must mark it with the open keyword otherwise you will get an error.

14.

What is the difference between Launch and Async in Kotlin Coroutines?

There are mainly two functions in Kotlin to start the coroutines.

  • launch{ }
  • async{ }

Launch Function The launch will not block the main thread, but on the other hand, the execution of the rest part of the code will not wait for the launch result since launch is not a suspend call.

Async Function Async is also used to start the coroutines, but it blocks the main thread at the entry point of the await() function in the program.

15.

How would you use primitive types such as int, double, float in Kotlin?

In Kotlin, we can't use primitive types directly. We can use classes like Int, Double, etc. as an object wrapper for primitives. But the compiled bytecode has these primitive types.

16.

What are lambdas expressions?

Lambdas expressions are anonymous functions that can be treated as values i.e. we can pass the lambdas expressions as arguments to a function return them, or do any other thing we could do with a normal object.

17.

What is lateinit in Kotlin and when would you use it?

lateinit means late initialization. If you do not want to initialize a variable in the constructor- you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized. You cannot use lateinit for primitive type properties like Int, Long etc.

18.

Explain null safety in Kotlin.

Kotlin's type system aims to eradicate null references from the code. One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java this would be the equivalent of a NullPointerException or NPE for short.

If a program throws NullPointerExceptions at runtime it might result in application failure or system crashes. If the Kotlin compiler finds a null reference it throws a NullPointerException.

19.

Explain types of visibility modifiers in Kotlin.

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.
20.

What is the difference between lateinit and lazy in Kotlin?

  • lazy can only be used for val properties, whereas lateinit can only be applied to var because it can’t be compiled to a final field, thus no immutability can be guaranteed.
  • If you want your property to be initialized from outside in a way probably unknown beforehand, you should use lateinit.