Kotlin interview questions and answers 👇

  1. Kotlin Interview Questions

Kotlin Interview Questions

What are companion objects in Kotlin?

View answer

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

What are visibility modifiers in Kotlin?

View answer

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

Explain constructors in Kotlin

View answer

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

Compare FlatMap and Map in Kotlin.

View answer

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

What is suspend function in Kotlin Coroutines?

View answer

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.

What are Higher-Order functions?

View answer

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

How are variables declared in Kotlin?

View answer

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.

What is the benefit of Kotlin over Java?

View answer

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.

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

View answer

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.

What is an infix function in Kotlin?

View answer

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.

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

View answer

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.

What is an inline function in Kotlin?

View answer

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.

What is the open keyword?

View answer

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.

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

View answer

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.

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

View answer

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.

What are lambdas expressions?

View answer

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.

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

View answer

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.

Explain null safety in Kotlin.

View answer

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.

Explain types of visibility modifiers in Kotlin.

View answer

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.

What is the difference between lateinit and lazy in Kotlin?

View answer

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