Kotlin Interview Questions and Answers
Kotlin Interview Questions and Answers
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).
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.
As it is concise language, it is very easy to read and understand the code pattern.
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.
6. Give example to declare variable using both val and var?
x = "Table" //valid
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.
println("$str1 $str2")
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.
9. 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.
var i = 5
println("i = $i")
The raw string is multiline string placed inside triple quotes. There is no need to escape characters in
raw string.
println(str)
Kotlin provides two ways to create arrays - arrayOf() and array() constructor.
val x = arrayOf(22,12,43)
for (a in x) {
println(a)
val y = Array(5){0}
for(b in y) {
println(b)
}
12. 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.
for(a in x) {
println(a)
if(arr.any())
println(true)
else
println(false)
println(arr.lastIndex)
}
15. What is !in operator?
The !in operator is used to check whether an object not belongs to a collection.
a !in b
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.
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.
for(i in 1..10) {
println(i)
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.
19. 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.
for (i in 1.rangeTo(10))
println("$i")
Yes, In kotlin, we can create an empty array with the help of emptyArray() 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.
print("$x")
It is a boolean function that checks all the matched input character sequence in a regular expression.
}
23. What are the requirements of an infix function?
The parameter must not have default values or variable number of arguments.
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.
highOrderFunc(x,y,msg)
println(message)
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.
27. How to declare an initialization block in kotlin class?
class checkInit() {
init {
init {
checkInit()
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.
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.
In kotlin, the compiler automatically created the following members with data class.
equals()
hashcode()
toString()
copy()
componentN()
31. 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.
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.
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.