Kotlin Interview Questions and Answers for Android App Developer (2022)

Golap Gunjan Barman
17 min readDec 31, 2021

Kotlin Interview Questions and Answers for Freshers and Experienced (2022)

Kotlin Interview Questions for Freshers

1. What are the various data types available in Kotlin? Explain them.

Primitive data types are the most basic data types in Kotlin, and all others are reference types like array and string. Kotlin contains all data types as objects. Different data types that are available in Kotlin:-

2. How are variables declared in Kotlin? What are the different types of variables in Kotlin? Explain with examples.

Every variable in Kotlin is declared before it is used. The type of data you are authorized to put in the memory address is determined by the variable type declaration.

For example,

var website = “GBAndroidBlogs”

The above code declares a variable “website” of type String because the value with which the variable is initialized is a String.

There are broadly two types of variables in Kotlin. They are as follows:-

Immutable Variables — Immutable variables are also known as read-only variables. They are declared using the val keyword. Once these variables have been declared, we cannot change their values. The syntax is as follows :

val variableName = value

Mutable Variables — In a mutable variable, the value of the variable can be changed. We use the keyword “var” to declare such variables.

The syntax is as follows :

var variableName = value

3. What are data classes in Kotlin? Explain with a proper example.

The Data class is a simple class that holds data and provides typical functions. To declare a class as a data class, use the data keyword.

Syntax:

data class className ( list_of_parameters)

The following functions are automatically derived by the compiler for the data classes:

  • equals() — The equals() function returns true if two objects have the identical contents. It operates similarly to “==,” although for Float and Double values it works differently.
  • hashCode() — The hashCode() function returns the object’s hashcode value.
  • copy() — The copy() function is used to duplicate an object, changing only a few of its characteristics while leaving the rest unaltered.
  • toString() — This function returns a string containing all of the data class’s parameters.
  • At least one parameter is required for the primary constructor.
  • val or var must be used for all primary constructor parameters.
  • Abstract, open, sealed, or inner data classes are not possible.
  • Only interfaces may be implemented by data classes.

To ensure consistency, data classes must meet the following requirements:

Example:

data class Sample(var input1 : Int, var input2 : Int)

The above code snippet creates a data class Sample with two parameters.

fun main(agrs: Array<String>) {
val temp = Sample(1, 2)
println(temp)
}

Here, we create an instance of the data class Sample and pass the parameters to it.

Output:-

Sample(input1=1, input2=2)

4. Explain the concept of null safety in Kotlin.

Kotlin’s type system aims to eradicate null references from the code. 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.

The Kotlin type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). Null cannot be stored in a String variable. We get a compiler error if we try to assign null to the variable.

var a: String = “interview”
a = null // results in compilation error

If we want the above string to be able to hold null value as well, we can declare it of type nullable using the ‘?’ operator after the String keyword as follows :

var a: String? = “interview”
a = null // no compilation error

Kotlin provides Safe Call (?.), Elvis (?:) and Not Null Assertion (!!) operators which define what needs to be done in case of a null encounter. This makes the code more reliable and less prone to errors. Thus, Kotlin enforces null safety by having nullable, non-nullable type variables and the different operators to tackle null encounters.

5. Explain Safe call, Elvis, and Not Null Assertion operator in the context of Kotlin.

Safe Call operator ( ?. ) — Null comparisons are trivial, but the number of nested if-else expressions can be exhausting. So, in Kotlin, there’s a Safe call operator,?, that simplifies things by only doing an action when a specified reference holds a non-null value.

For example,
The following expression in Kotlin

name?.toLowerCase()

is equivalent to the following

if(name != null)
name.toLowerCase()
else
null

Elvis Operator ( ?: ) — When the original variable is null, the Elvis operator is used to return a non-null value or a default value.

For example,
The following expression in Kotlin

val sample1 = sample2 ?: “Undefined”

is equivalent to the following

val sample1 = if(sample2 != null)
sample2
else
“Undefined”

Not Null Assertion Operator ( !! ) — If the value is null, the not-null assertion (!!) operator changes it to a non-null type and throws an exception.
Anyone who wants a NullPointerException can ask for it explicitly with this operator.
For example,

// KOTLIN
fun main(args: Array<String>) {
var sample : String? = null
str!!.length
}

The above code snippet gives the following output:-

Exception in thread “main” kotlin.KotlinNullPointerException

6. Differentiate between Kotlin and Java.

Following are the differences between Kotlin and Java:-

7. What are the different types of constructors available in Kotlin? Explain them with proper examples.

There are two types of Kotlin constructors:

Primary Constructor — This type of constructor is initialized in the class header and is provided after the class name. It is declared using the “constructor” keyword. Parameters are optional in this type of constructor. For example,

class Sample constructor(val a: Int, val b: Int) {
// code
}

For example,

// KOTLIN
fun main(args: Array<String>) {
val s1 = Sample(1, 2)
}
class Sample(a : Int , b: Int) {
val p: Int
var q: Int
// initializer block
init {
p = a
q = b
println(“The first parameter value is : $p”)
println(“The second parameter value is : $q”)
}
}

Output:-

The first parameter value is: 1
The second parameter value is: 2

Secondary Constructor — Secondary constructors allow for the initialization of variables as well as the addition of logic to the class. They have the constructor keyword prefixed to them. For example,

// KOTLIN
fun main(args: Array<String>) {
val s1 = Sample(1, 2)
}
class Sample {
constructor(a: Int, b: Int) {
println(“The first parameter value is : $p”)
println(“The second parameter value is : $q”)
}
}

Output:-

The first parameter value is: 1
The second parameter value is: 2

8. Explain the various methods to iterate over any data structure in Kotlin with examples.

Following are the different ways to iterate over any data structure in Kotlin :

For Loop — The for loop is used to scan any data structure that supplies an iterator in this case.
In Kotlin, the for loop has the following Syntax:

for(item in collection) {
// code
}

Here, collection refers to the data structure to be iterated and item refers to each element of the data structure.

For example,

// KOTLIN
fun main(args: Array<String>) {
var numbersArray = arrayOf(1,2,3,4,5,6,7,8,9,10)

for (num in numbersArray){
if(num % 2 == 0){
print(“$num “)
}
}
}

Output -
2 4 6 8 10

While Loop — It is made up of a code block and a condition to be checked for each iteration. First, the while condition is assessed, and if it is true, the code within the block is executed. Because the condition is verified every time before entering the block, it repeats until the condition turns false.
The while loop’s syntax is as follows:

while(condition) {
// code
}

For example,

// KOTLIN
fun main(args: Array<String>) {
var number = 1
while(number <= 5) {
println(number)
number++;
}
}

Output -
1
2
3
4
5

Do While Loop — The condition is assessed after all of the statements inside the block have been executed. If the do-while condition is true, the code block is re-executed. As long as the expression evaluates to true, the code block execution procedure is repeated. The loop ends if the expression becomes false, and control is passed to the sentence following the do-while loop. Because it verifies the condition after the block is executed,
The do-while loop’s syntax is as follows:

do {
// code
{
while(condition)

For example,

// KOTLIN
fun main(args: Array<String>) {
var number = 4
var sum = 0

do {
sum += number
number —
}while(number > 0)
println(“Sum of first four natural numbers is $sum”)
}

Output -Sum of first four natural numbers is 10

9. How can you concatenate two strings in Kotlin?

Following are the different ways by which we can concatenate two strings in Kotlin:

Using String Interpolation:- We use the technique of string interpolation to concatenate the two strings. We substitute the strings in place of their placeholders in the initialization of the third string.

val s1 = “Interview”
val s2 = “Bit”
val s3 = “$s1 $s2” // stores “Interview Bit”

Using the + or plus() operator:- We use the ‘+’ operator to concatenate the two strings and store them in a third variable.

val s1 = “Interview”
val s2 = “Bit”
val s3 = s1 + s2 // stores “InterviewBit”
val s4 = s1.plus(s2) // stores “InterviewBit”

Using StringBuilder:- We concatenate two strings using the StringBuilder object. First, we append the first string and then the second string.

val s1 = “Interview”
val s2 = “Bit”
val s3 = StringBuilder()
s3.append(s1).append(s2)
val s4 = s3.toString() // stores “InterviewBit”

10. What do you understand about function extension in the context of Kotlin? Explain.

In Kotlin, we can add or delete method functionality using extensions, even without inheriting or altering them. Extensions are statistically resolved.

Function Extension — We’ll see how the extension is implemented at the functional level in the following example:

// KOTLIN
class Sample {
var str : String = “null”

fun printStr() {
print(str)
}
}
fun main(args: Array<String>) {
var a = Sample()
a.str = “Interview”
var b = Sample()
b.str = “Bit”
var c = Sample()
c.str = a.add(b)
c.printStr()
}
// function extension
fun Sample.add(a : Sample):String{
var temp = Sample()
temp.str = this.str + “ “ +a.str
return temp.str
}

Output:-
Interview Bit

11. What do you understand about Companion Object in the context of Kotlin?

In Kotlin, there is nothing called the “static” keyword. So, if we want to achieve the functionality of static member functions, we use the companion objects. This is also referred to as Object Extension.
We must use the companion keyword in front of the object definition to construct a companion object.

// Syntax in KOTLIN
class CompanionClass {
companion object CompanionObjectName {
// code
}
}
val obj = CompanionClass.CompanionObjectName

We can also remove the CompanionObject name and replace it with the term companion, resulting in the companion object’s default name being Companion, as shown below:

// KOTLIN
class CompanionClass {
companion object {
// code
}
}
val obj = CompanionClass.Companion

12. Differentiate between open and public keywords in Kotlin.

The keyword “open” refers to the term “open for expansion”. The open annotation on a class is the polar opposite of the final annotation in Java: it allows others to inherit from it. By default, a class cannot be inherited in Kotlin. In Kotlin, an open method signifies that it can be overridden, whereas it cannot be by default. Instead, any methods in Java can be overridden by default.

In Kotlin, all the classes are public by default. If no visibility modifier is specified, the public is used by default, which means our declarations will be accessible everywhere inside the program

13. Explain about the “when” keyword in the context of Kotlin.

The “when” keyword is used in Kotlin to substitute the switch operator in other languages such as Java. We do not need a break statement at the end of each case, unlike switch cases in Java or any other programming language.

For example,

// KOTLIN
fun main(args: Array<String>) {

var temp = “Interview”
when(temp) {
“Interview” -> println(“Interview Bit is the solution.”)
“Job” -> println(“Interview is the solution.”)
“Success” -> println(“Hard Work is the solution.”)
}
}

Output:-
Interview Bit is the solution.

14. What are the advantages of Kotlin over Java?

Following are the advantages of Kotlin over Java:-

  • Data class: In Java, you must create getters and setters for each object, as well as properly write hashCode (or allow the IDE to build it for you, which you must do every time you update the class), toString, and equals. Alternatively, you could utilize lombok, but that has its own set of issues. In Kotlin, data classes take care of everything.
  • Patterns of getter and setter: In Java, for each variable, you use it for, rewrite the getter and setter methods. You don’t have to write getter and setter in kotlin, and if you must, custom getter and setter take a lot less typing. There are additional delegates for identical getters and setters.
  • Extension Functions: In Java, there is no support for extension functions. Kotlin on the other hand provides support for extension functions which makes the code more clear and cleaner.
  • Support for one common codebase: You may extract one common codebase that will target all of them at the same time using the Kotlin Multi-Platform framework.
  • Support for Null Safety: Kotlin has built-in null safety support, which is a lifesaver, especially on Android, which is full of old Java-style APIs.
  • Less prone to errors: There is less space for error because it is more concise and expressive than Java.

Kotlin Interview Questions for Experienced

15. Which one is better to use — val mutableList or var immutableList in the context of Kotlin?

We use a mutable list if the collection will alter as part of the design. On the other hand, we use an immutable list if the model is only meant to be viewed.

Immutable lists are frequently preferred for a variety of reasons:

  • They promote functional programming, in which state is passed on to the next function, which constructs a new state based on it, rather than being altered. This is evident in Kotlin collection methods like map, filter, reduce, and so forth.
  • It’s often easier to understand and debug software that doesn’t have any side effects (you can be sure that the value of an object will always be the one at its definition).
  • Because no write access is required in multi-threaded systems, immutable resources cannot induce race conditions.

However, there are some disadvantages of using immutable lists as well. They are as follows :

  • Copying large collections simply to add/remove a single piece is very expensive.
  • When you need to alter single fields frequently, immutability can make the code more difficult. Data classes in Kotlin provide a built-in copy() method that allows you to clone an instance while changing only part of the fields’ values

16. What do you understand about lateinit in Kotlin? When would you consider using it?

lateinit is an abbreviation for late initiation. If you don’t want to initialize a variable in the constructor and instead want to do it later, and you can guarantee the initialization before using it, use the lateinit keyword to declare that variable.

For example,

// KOTLIN
lateinit var test: String
fun testFunction() {
test = “Interview”
println(“The length of string is “+test.length)
test = “Bit”
}

When the testFunction is called, we get the following output:-

9

17. Explain lazy initialization in the context of Kotlin.

Lazy initialization helps in such problems. When we declare an object using lazy initialization, the object is initialized only once when the object is used. If the object is not used throughout, the object is not initialized. This makes the code more efficient and faster.

For example, let us imagine you have a SlowClass class and you require an object of that SlowClass in a different class called FastClass:

// KOTLIN
class FastClass {
private val slowObject: SlowClass = SlowClass()
}

For example,

// KOTLIN
class FastClass {
private val slowObject: SlowClass by lazy {
println(“Slow Object initialised”)
SlowClass()
}

fun access() {
println(slowObject)
}
}
fun main(args: Array<String>) {
val fastClass = FastClass()
println(“FastClass initialised”)
fastClass.access()
fastClass.access()
}

Output:-
FastClass initialised
Slow Object initialised
SlowClass@2b12fkk7
SlowClass@2b12fkk7

18. Differentiate between lateinit and lazy initialisation. Explain the cases when you should use lateinit and when you should use lazy initialisation.

Following are the differences between lateinit and lazy initialisation:-

19. What do you understand about coroutines in the context of Kotlin?

Unlike many other languages with equivalent capabilities, async and await are neither keywords nor part of Kotlin’s standard library. JetBrains’ kotlinx.coroutines library is a comprehensive library for coroutines. It includes several high-level coroutine-enabled primitives, such as launch and async. Kotlin Coroutines provide an API for sequentially writing asynchronous code.

Coroutines are similar to thin threads. Coroutines are lightweight since they don’t allocate new threads when they’re created. Instead, they employ pre-defined thread pools as well as intelligent scheduling. The process of deciding which piece of work you will do next is known as scheduling. Coroutines can also be paused and resumed in the middle of their execution. This means you can have a long-term project that you can work on incrementally. You can pause it as many times as you want and continue it whenever you’re ready

20. Explain scope functions in the context of Kotlin. What are the different types of Scope functions available in Kotlin?

Following are the different types of Scope functions available in Kotlin:-

  • let:-
  • Context object: it
  • Return value: lambda result
  • The let function is frequently used for null safety calls. For null safety, use the safe call operator(?.) with ‘let’. It only runs the block with a non-null value.
  • apply:-
  • Context object: this
  • Return value: context object
  • “Apply these to the object,” as the name suggests. It can be used to operate on receiver object members, primarily to initialise them.
  • with:-
  • Context object: this
  • Return value: lambda result
  • When calling functions on context objects without supplying the lambda result, ‘with’ is recommended.
  • run:-
  • Context object: this
  • Return value: lambda result
  • The ‘run’ function is a combination of the ‘let’ and ‘with’ functions. When the object lambda involves both initialization and computation of the return value, this is the method to use. We can use run to make null safety calls as well as other calculations.
  • also:-
  • Context object: it
  • Return value: context object
  • It’s used when we need to do additional operations after the object members have been initialised.

21. Explain suspend function in the context of Kotlin.

A function that may be started, halted, then resumed is known as a suspend function. One of the most important things to remember about the suspend functions is that they can only be invoked from another suspend function or from a coroutine.

Suspending functions can call any other ordinary functions, but another suspending function is required to suspend the execution. Because a suspending function cannot be called from a regular function, numerous coroutine builders are supplied, allowing you to call a suspending function from a non-suspending scope like launch, async, or runBlocking.

delay() function is an example of suspend function.

22. What do you understand about sealed classes in Kotlin?

Kotlin introduces a crucial new form of class that isn’t seen in Java. These are referred to as “sealed classes.” Sealed classes, as the name implies, adhere to constrained or bounded class hierarchies. A sealed class has a set of subclasses.

The syntax is as follows:-

sealed class className

Another distinguishing aspect of sealed classes is that their constructors are by default private. Due to the fact that a sealed class is automatically abstract, it cannot be instantiated.
For example,

// KOTLIN
sealed class Sample {
class A : Sample() {
fun print()
{
println(“This is the subclass A of sealed class Sample”)
}
}
class B : Sample() {
fun print()
{
println(“This is the subclass B of sealed class Sample”)
}
}
}
fun main()
{
val obj1 = Sample.B()
obj1.print()

val obj2 = Sample.A()
obj2.print()
}

Output:-
This is the subclass B of sealed class Sample
This is the subclass A of sealed class Sample

23. What do you understand about the backing field in Kotlin?

A backing field is an auto-generated field for any property that may only be used inside accessors (getter or setter) and will only be present if it utilizes the default implementation of at least one of the accessors, or if a custom accessor refers to it through the field identifier.

Fields are not allowed in Kotlin classes. Kotlin includes an automatic backing field for these purposes, which may be accessed by the field identifier.
For example,

var marks: Int = someValue
get() = field
set(value) {
field = value
}

24. Differentiate between launch / join and async / await in Kotlin.

launch / join:-
The launch command is used to start and stop a coroutine. It’s as though a new thread has been started. If the code inside the launch throws an exception, it’s considered as an uncaught exception in a thread, which is typically written to stderr in backend JVM programs and crashes Android applications. Join is used to wait for the launched coroutine to complete before propagating its exception. A crashed child coroutine, on the other hand, cancels its parent with the matching exception.

async / await:-
The async keyword is used to initiate a coroutine that computes a result. You must use await on the result, which is represented by an instance of Deferred. Uncaught exceptions in async code are held in the resultant Deferred and are not transmitted anywhere else. They are not executed until processed.

25. What are some of the disadvantages of Kotlin?

Following are some of the disadvantages of Kotlin:

  • In Kotlin, there are a few keywords that have non-obvious meanings: internal, crossinline, expect, reified, sealed, inner, open. Java has none of these.
  • Checked exceptions are likewise absent in Kotlin. Although checked exceptions have become less prominent, many programmers believe them to be an effective technique to ensure that their code is stable.
  • A lot of what happens in Kotlin is hidden. You can almost always trace the logic of a program in Java. When it comes to bug hunting, this can be really useful. If you define a data class in Kotlin, getters, setters, equality testing, tostring, and hashcode are automatically added for you.
  • Learning resources are limited. The number of developers who are moving to Kotlin is growing, yet there is a small developer community accessible to help them understand the language or address problems during development.
  • Kotlin has variable compilation speed. In some situations, Kotlin outperforms Java, particularly when executing incremental builds. However, we must remember that when it comes to clean builds, Java is the clear winner.

Test Yourself:

1. Which of the following is used to handle null exceptions in Kotlin?

  1. Sealed Class
  2. Elvis Operator
  3. Range
  4. Open Class

Answer: 2

2. Which of the following file extensions denotes a Kotlin file?

  1. .java
  2. .kot
  3. .kt or .kts
  4. .android

Answer: 3

3. Which of the following options is used to make a multi line comment in Kotlin?

  1. //
  2. /* */
  3. / COMMENTS /
  4. None of the above

Answer: 3

4. Is it possible to execute Kotlin code without JVM?

  1. Yes, it is always possible.
  2. No, it is never possible.
  3. It is possible in certain cases.
  4. None of the above.

Answer: 1

5. Which of the following best describes a sealed class in Kotlin?

  1. It is analogous to an abstract class
  2. It refers to the default class in Kotlin
  3. It represents restricted class hierarchies
  4. None of the above

Answer: 3

6. Are macros supported in Kotlin?

  1. Yes
  2. No

Answer: 2

7. Which of the following targets is currently not supported by Kotlin?

  1. Javascript
  2. LLVM
  3. .NET CLR
  4. All of the above

Answer: 3

8. Is Kotlin Backward compatible?

  1. YES
  2. NO

Answer: 1

9. Which of the following is the one thing that an abstract class can do but not an interface in the context of Kotlin?

  1. Only abstract classes can store state reference
  2. Only abstract classes can have abstract methods
  3. Only abstract classes are inheritable by subclasses
  4. None of the above

Answer: 1

10. The following code snippet gives a compilation error.

class Sample
class Test : Sample()

Which one of the following describes the reason for it?

  1. A class must be marked as open so that it can be inherited by other classes.
  2. A class must be marked as public so that it can be inherited by other classes.
  3. A class must be marked as sealed so that it can be inherited by other classes.
  4. None of the above

Answer: 1

--

--

Golap Gunjan Barman

Hi everyone, myself Golap an Android app developer with UI/UX designer.