Top Kotlin Interview Questions and Answers (with Code Examples)

Dear Readers, these Kotlin Interview Questions have been outlined particularly to get you familiar with nature of questions that you may experience during your interview.

If there is any question that has been asked to you during interview kindly post it in the comments section to help future readers.

These Kotlin Interview Questions are also helpful for Android Developers.

I will also keep adding Kotlin Interview Questions on this list.

Why can a developer prefer Kotlin over Java?

Answer:-

Because Kotlin has several features which even modern versions of Java lack.

Let’s list down few of them.

  • Kotlin support best features of functional programming and OO programming language.
  • Kotlin handles Null Pointer Exceptions easily.
  • Kotlin support lambdas, higher order functions, smart casting.
  • Approximately 40 % less number of code lines as compared to Java.

Reference:-

Kotlin comparison to Java

What is the difference between const and val in Kotlin?

Answer:-

Properties which needs to be initialized at compile time and which cannot be assigned during run time are declared using keyword const.

Whereas properties which can be assigned a value at runtime but their value cannot be reassigned are declared using keyword val.

What is the difference between == and === in Kotlin?

Answer:-

These are two different types of equality in Kotlin.

== represents structural equality in Kotlin.

Whereas === represents referential equality in Kotlin.

Can Kotlin code be executed without JVM?

Answer:-

Yes, this is a feature of Kotlin / Native which is under development.

It compiles Kotlin code into native code which can be executed without JVM.

What are Data classes in Kotlin and how they can be compared with POJO?

Answer:-

Creating Data Classes in Kotlin (DTO / POJO) example

What is Kotlin String Interpolation?

Answer:-

String Templates in Kotlin can be evaluated (interpolated).

Reference:-

String Interpolation and String Templates in Kotlin example

How do you define Singleton Object in Kotlin?

We can use companion object in Kotlin to declare a class Singleton.

Kotlin companion object also solves the problem of static member and static functions.

Because any property inside companion object can be called without creating an instance of a class.

Example:-

class AppClass : Application() {
    companion object {
        var instance: AppClass
}
    override fun onCreate() {
        super.onCreate()
        instance = this
    }
}

Can we assign variable of type Byte to variable of type Int?

Answer:-

No, Kotlin does not support implicit conversion of smaller types to bigger ones.

We need to explicitly convert Byte to Int before assigning it to an Int variable.

val b: Byte = 1 
val i: Int = b.toInt()

How Kotlin handle Null Pointer Exceptions?

Answer:-

Kotlin is null safety language. Means we cannot have a null variable especially in case of primitive data types they can never be null.

But sometimes there are few variables which need to be initialized lately.

So for that Kotlin has a lateinit keyword which tells the compiler that this particular keyword will be initialized lately.

Consider an example  lateinit var myadapter:MyAdapter, to use it the proper syntax will be myadapter?.let { Do your stuff in body } in a newer version of Kotlin.

We also have a keyword isInitialzed, other then this we can also use !! to tell the compiler that we are aware of Null Pointer Exception let ignore it.

Reference:-

https://kotlinlang.org/docs/reference/null-safety.html

What are three structural expressions in Kotlin?

Answer:-

What are different Types of Constructors in Kotlin, explain them.

Answer:-

There are two different types of constructors in Kotlin.

  • Primary constructor
  • Secondary constructor

A Primary constructor is part of a class header and is declared after the class name.

A Secondary constructor is declared inside a class body.

A Class can have one or more Secondary constructor.

What is the equivalent of Switch Statement in Java in Kotlin?

Answer:-

In Kotlin when is alternate of Java switch statement.

Example:-

when (x) { is Foo -> ...  (true if x is of type Foo)

 is Bar -> ...  (true if x is of type Bar)

 else -> ...    (true if none of the above condition is met)
}

Reference:-

Instance checks or Type checking in Kotlin example

Can a when statement in Kotlin be used without passing any argument?

Answer:-

Yes, in that case, each branch of a when statement will work as if else if.

If any branch fulfills its condition its body will be executed.

What is the difference between a member function and extension function in Kotlin?

Answer:-

 

Does Kotlin support Checked Exceptions like in Java?  if yes give an example.

Answer:-

No, Kotlin does not support Checked Exceptions.

Does Kotlin allow to declare multiple main functions in the same package?

package com.developine.learnkotlin

fun main(args: Array<String>) {
    println("hello World")
}

Answer:

Yes, In Kotlin you can declare multiple main functions in different files in same package.

What is difference between Explicit conversion and Implicit conversion? Does Kotlin support both conversions?

Answer:-

Kotlin does not support implicit conversions.

What is Elvis Expression in Kotlin?

What are Kotlin/Native and Kotlin Multiplatform project?

Answer:-

Kotlin Native iOS Development using Multiplatform Project

What are primitive Data Types in kotlin?

Answer:-

Kotlin does not support primitive Data Types like in Java.

What are open, final and abstract Modifiers in Kotlin?

Answer:-

Open, final and abstract modifiers in Kotlin

Can we pass a different number of arguments to the same Function in Kotlin?

Answer:-

Yes.

What is the difference between Return Type Unit and Return Type Nothing in Kotlin?

Answer:-

Why is keyword vararg in Kotlin used for?

Answer:-

What is spread operator / *(sterik) in Kotlin?

Answer:-

What are inline functions in Kotlin?

Just like C++ in Koltin we can use functional as well as Object-Oriented Programming approach.

Means We can have functions without class but on compile time Kotlin compiler converts the function, make a static object for it and then call that function by an anonymous class anonymous instance which cause extra memory consumption.

To come over it inline keyword is used, it copies the code into a class which is calling this method and our method behave as a member of class calling it.

What is difference between Inline and nonInline keyword in Kotlin?

Answer:- Coming Soon…..

How to declare mutable and immutable Objects in Kotlin?

Answer:-

We use var and val keywords to declare any property mutable or immutable in Kotlin.

What is Difference between Safe ?. and non-null asserted !! calls in Kotlin?

Answer:- Coming soon….

What are High Level Functions in Kotlin?

Answer:-

A function which can:

  • Accept another function as a parameter.
  • Can return another function.
  • Or can do both of the above.

What is the difference between object { } block and companion object { } code block in Kotlin?

Answer:- Coming Soon….

If this article has helped you, please share it 🙂

And if you found any other Kotlin Android Interview questions which can be added in this list, please let me know through comments.

Contact Us