Kotlin Interview Questions And Answers

What is Kotlin Native?

Kotlin Native is technology for compiling kotlin code to native binaries which can run without virtual machine, like iOS(arm32, arm64), MacOS(x86_64), Android(arm32, arm64), Linux(x86_64, arm32).
Define Kotlin Programming Language?

Kotlin is a general-purpose, open-source programming language. It can be used to develop android apps, server-side apps and much more. It is a high level strongly, statically typed language and its code is more concise, readable and easy to understand. Its popularity has grown day by day.
Who created Kotlin programming language and when?

Kotlin was developed by JetBrains team. This project development started in 2010 and officially, first released in February 2016. This language was developed under the Apache 2.0 license.
What are the major features of Kotlin?

These are some major features of Kotlin.
  • Kotlin is open source, statically typed programming language.
  • As it is concise language, it is very easy to read and understand the code pattern.
  • In a very short period of time, it can give better performance.
  • It is interoperable and null safety language, it eliminates the NullPointerException.
  • Kotlin is compatible with all the Java based framework.
Differenciate between val and var?

The var is equivalent to declaring a variable in Java, it is a mutable variable, which can be changed to another value by reassigning it. But, val is an immutable variable or read only variable which cannot be changed later in the program once the value is assigned. They can be assigned a value only once. This is similar to the final variable of Java.

Give example to declare variable using both val and var?

fun main(args : Array<String>)  {   
	var x: String  = "Chair"
    x = "Table" //valid
    val y: String  = "Bed"
    println("$x  and $y")
Define String datatype with example?

The string is a sequence of characters enclosed within double and triple quotes. It is immutable means you cannot change the individual character of a string. It is represented by the type String.
fun main(args : Array<String>)  {    
val str1 = "Welcome Alia"
	var str2 = """We hope you will enjoy more."""
	println("$str1 $str2")
What is inner class?

A inner class can access members of outer class. It carries a reference to an object of an outer class. Just like nested class, it is created inside an outer class and unlike a nested class, it is marked with the keyword inner. It has advantages over nested class is that, it can access private members of outer class.
How to write string template expression?

In Kotlin, string can contain template expressions, means we can evaluate the code string. This string expression starts with dollar sign.
fun main(args : Array<String>)  { 
	var i = 5
	println("i = $i")
How to write multiline string in kotlin?

The raw string is multiline string placed inside triple quotes. There is no need to escape characters in raw string.
fun main(args : Array<String>)  {    
    val str = """Welcome, Somya!
    you are learning
    kotlin programming language"""
How many ways to create array in kotlin?

Kotlin provides two ways to create arrays - arrayOf() and array() constructor.
fun main(args : Array<String>)  {    
	val x = arrayOf(22,12,43)
    for (a in x) {
	val y = Array(5){0}
	for(b in y) {
Can we create uninitialized array in kotlin?

In kotlin, we can create an uninitialized array or declare it to nullable using arrayOfNulls() function. This can be initialized later.
fun main(args : Array<String>)  {    
	var x = arrayOfNulls (10)
    for(a in x) {
What is the use of any() function?

The any() function is used to check existence of any element in an array.
fun main(args : Array<String>)  {    
	val arr = arrayOf(22,12,43)
Which method is used to get array last index?

Kotlin provides lastIndex() method to get last element index of an array.
fun main(args : Array<String>)  {    
    val arr = arrayOf(22,12,43)
What is !in operator?

The !in operator is used to check whether an object not belongs to a collection.
a !in b
Define when expression?

In kotlin, the when expression is used if there is need to match the value from many different cases and execute the statements within the matched case. When statement executes line by line and read all the cases. This is replacement of switch statement from other language like PHP, C, C++ and more powerful then switch statement.
What is Range?

Kotlin range is a sequence between the given start value and end value. The (..) operator is used to create a range expression which is complemented by in and !in.
fun main(args : Array<String>)  {    
	for(i in 1..10) {
Differenciate between break and continue?

Kotlin provides the break statement to implement the middle-existing control logic. The break statement causes the immediate exit from the body of the loop.
The continue statement is similar to the break statement. When continue statement is executed, it stops the current iteration of the loop and continue with the next iteration of the loop.
What is the use of rangeTo() function?

The rangeTo() function is itegral type which calls the constructor of Range class. The function is used to return the value from the start to end as mentioned in the range in increasing order.
fun main(args : Array<String>)  {    
	for (i in 1.rangeTo(10))
In kotlin, can we create an empty array?

Yes, In kotlin, we can create an empty array with the help of emptyArray() function.
fun main(args : Array<String>)  {    
val empty = emptyArray()	
Define step() function?

The step() function is used to return the range value in interval of given step value. The step value is required to be always positive, therefore this function never changes the direction of iteration.
fun main(args : Array<String>)  {    
	for (x in 1..10 step 5)
What is the role of matches() method in regular expression?

It is a boolean function that checks all the matched input character sequence in a regular expression.
fun main(args : Array<String>)  {    
	val regex = """(abcd|efgh)""".toRegex()  
	val match = regex.matches("abcd")?.value  
What are the requirements of an infix function?

The following requirements must be fulfill to make a function infix.
  • The function must be a member functions or extension functions.
  • They must have a single argument.
  • The parameter must not have default values or variable number of arguments.
Define high order functions?

In high order function, we can pass a function as a parameter to the other function, means in addition to passing variables as parameters to the function, we can pass the function and the function return type can also be a function.
Give an example of high order functions?

fun main(args : Array<String>)  {    
    val x = "Hello John! "
    val y = "How are you? "
    val msg:(String,String)->String={x,y->"$x $y"}
fun highOrderFunc(x: String,y: String, msg: (String,String) -> String): Unit {
    val message = msg(x,y)
Define Lambda Function?

A lambda function is a function that does not need to declare, but we can pass immediately as an expression. This is an anonymous function which is usually created during a function call to act as a function parameter.
How to declare an initialization block in kotlin class?

Kotlin provides init keyword to declare an initialization block in kotlin class.
class checkInit() {
    init {
        println("Our Mission - ")
    init {
        println("About school - ")
fun main(args : Array<String>)  {    
What is the role of open keyword in Kotlin?

In Kotlin, by default, all the classes are final, means they cannot be inherited from. The open keyword is used to allow inheritance of a class.
What is Data Class?

Kotlin provides facility of data classes. The main purpose of data class is to hold data and it is very simple to create. This can frequently be created without writing a lot of boilerplate code.

List down some kotlin data class built in methods?

In kotlin, the compiler automatically created the following members with data class.
  • equals()
  • hashcode()
  • toString()
  • copy()
  • componentN()
Define enum?

Enum stands for enumeration, it is a special type of class. This is used to set some predefined constants. The enum keyword is used to create an enumeration class.
What is nested class?

In Kotlin, we can create one class within the body of another class. It is called nested class. A nested class cannot access the data member of an outer class. It is static by default. So we can access its properties and methods without instantiating this.
Differenciate between generics in and out?

The modifier in is used to apply contravariant type parameter, means to assign generic type as input to its function. The modifier out is used to apply covariance type parameter, means to assign a generic type as the output of its function.