Kotlin has gained immense popularity as a modern programming language, especially for Android development and backend services using Spring Boot. If you are preparing for a Kotlin interview, here are the top 20 Kotlin questions with answers to help you succeed.
Answer: Kotlin is a modern, statically typed programming language developed by JetBrains. It is fully interoperable with Java and is widely used for Android development, backend applications, and multiplatform projects.
Answer:
Concise – Kotlin reduces boilerplate code compared to Java.
Null Safety – Eliminates null pointer exceptions with nullable and non-nullable types.
Coroutines – Supports lightweight threads for asynchronous programming.
Smart Casts – Automatically detects variable types without explicit casting.
Answer:
✅ Interoperability with Java
✅ Null Safety
✅ Type Inference
✅ Extension Functions
✅ Coroutines for Asynchronous Programming
✅ Smart Casts
✅ Data Classes
Answer: A data class is a special class used to store data with automatic generation of equals(), hashCode(), and toString() methods.
Example:
data class User(val name: String, val age: Int)
Answer: Extension functions allow you to add new functionality to existing classes without modifying their source code.
Example:
fun String.reverseText(): String = this.reversed() println("Kotlin".reverseText()) // Output: niltoK
Answer: Kotlin provides nullable (?) and non-nullable types to avoid null pointer exceptions.
Example:
var name: String? = "Kotlin" name = null // Allowed println(name?.length) // Safe call operator
Answer: It provides a default value when a nullable variable is null.
val name: String? = null val length = name?.length ?: 0 // If null, assign default value 0
Answer: Coroutines are lightweight threads used for asynchronous programming in Kotlin.
Example:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
delay(1000L)
println("Coroutine executed")
}
println("Main function running")
}
Answer:
val (Immutable) – Read-only, cannot be reassigned.
var (Mutable) – Can be changed after assignment.
val name = "Kotlin" // Cannot be changed var age = 25 // Can be changed
Answer: It is used to initialize a non-null variable later without using nullable types.
lateinit var username: String fun initUser() {
username = "BindTheLogic"
println(username)
}
Answer: A sealed class restricts inheritance, allowing only a predefined set of subclasses.
sealed class Shape class Circle(val radius: Double) : Shape() class Rectangle(val width: Double, val height: Double) : Shape()
Answer:
== – Checks structural equality (content comparison).
=== – Checks referential equality (memory reference).
val a = "Kotlin"
val b = "Kotlin"
println(a == b) // true (content is the same)
println(a === b) // true (same memory reference)
Answer: An object declaration creates a singleton instance of a class.
object Singleton {
fun greet() = "Hello from Singleton"
}
println(Singleton.greet())
Answer: Inline functions reduce memory overhead by avoiding object creation for higher-order functions.
inline fun greet(name: String, action: () -> Unit) {
println("Hello, $name")
action()
}
greet("Kotlin") {
println("Inside lambda")
}
Answer:
Array – Fixed-size, mutable data structure.
List – Immutable by default but can be mutable using MutableList.
val numbers = arrayOf(1, 2, 3) // Array
val list = listOf(1, 2, 3) // Immutable List
val mutableList = mutableListOf(1, 2, 3) // Mutable List
Answer: A companion object allows defining static-like functions in Kotlin classes.
class MyClass {
companion object {
fun create() = MyClass()
}
}
val obj = MyClass.create()
Answer: You can assign default values to function parameters.
fun greet(name: String = "Guest") {
println("Hello, $name")
}
greet() // Output: Hello, Guest
greet("Alice") // Output: Hello, Alice
Answer:
mapOf() – Immutable key-value pairs.
mutableMapOf() – Allows modifying the map.
val immutableMap = mapOf(1 to "One", 2 to "Two")
val mutableMap = mutableMapOf(1 to "One", 2 to "Two")
mutableMap[3] = "Three" // Allowed
Answer: A function that takes another function as a parameter.
fun operate(num: Int, action: (Int) -> Int): Int {
return action(num)
}
val result = operate(5) { it * 2 } // Output: 10
Answer: Flow is used for handling streams of data asynchronously, similar to RxJava.
import kotlinx.coroutines.flow.*
fun numbersFlow(): Flow<Int> = flow {
for (i in 1..5) {
emit(i) // Emits values one by one
}
}
Mastering these Kotlin interview questions will boost your confidence in Android development, backend (Spring Boot), and multiplatform projects. Stay updated with Kotlin features and keep practicing! 🚀