Kotlin is a modern, statically typed programming language that runs on the JVM, and is fully interoperable with Java. It's officially supported by Google for Android development.
Kotlin Interview Questions
1. What is Kotlin?
2. How is Kotlin better than Java?
- Null Safety
- Less boilerplate code
- Extension functions
- Coroutines for async code
- Data classes
- Smart casting
3. What is a val vs var in Kotlin?
- val: Immutable (like final in Java)
- var: Mutable (value can change)
val name = "Sahand" // read-only Var age = 30 // mutable
4. What is null safety in Kotlin?
Kotlin avoids NullPointerException by making types non-nullable by default.
var name: String = "Kotlin" name = null // compile error var name2: String? = null // Nullable
5. What is a data class in Kotlin?
Data classes automatically provide equals(), hashCode(), toString(), and copy() methods.
data class User(val name: String, val age: Int)
6. What are extension functions?
They let you add functions to existing classes without modifying them.
fun String.capitalizeFirst(): String {
return this.replaceFirstChar { it.uppercase() }
} 7. What is a companion object?
Used to define static members in a class.
class MyClass {
companion object {
fun greet() = "Hello!"
}
} 8. What are higher-order functions?
Functions that take functions as parameters or return functions.
fun operate(x: Int, y: Int, op: (Int, Int) -> Int): Int {
return op(x, y)
} 9. What is the Elvis operator (?:) in Kotlin?
Used to provide a default value when an expression is null.
val name: String? = null Val displayName = name ?: "Guest"
10. What is safe call operator (?.) in Kotlin?
Used to safely access a property or call a function on a nullable object.
val length = name?.length
11. What is the difference between == and === in Kotlin?
- =: structural equality (like equals() in java)
- ==: referential equality (compares memory reference)
12. What are coroutines in Kotlin?
Coroutines simplify asynchronous code. They're lightweight threads managed by Kotlin.
GlobalScope.launch {
delay(1000)
println("Hello from coroutine")
} 13. What is a sealed class in Kotlin?
Restricts class inheritance to a limited set of types — good for representing state.
sealed class Result class Success(val data: String) : Result() Class Error(val message: String) : Result()
14. What are default and named arguments?
fun greet(name: String = "Guest", message: String = "Hi") {
println("$message, $name!")
}
Greet() // Hi, Guest!
greet(message = "Hello") // Hello, Guest! 15. How to create a singleton in Kotlin?
Using the object keyword.
object AppConfig {
val version = "1.0"
} 16. What is a lambda in Kotlin?
A lambda is an anonymous function.
val add = { x: Int, y: Int -> x + y }17. What is a lateinit keyword?
Used to declare a non-null variable that will be initialized later.
lateinit var name: String
18. What is the use of is keyword in Kotlin?
Checks the type of an object.
if (user is String) {
println(user.length)
} 19. What are ranges in Kotlin?
Used to define a range of values and iterate through them.
for (i in 1..5) { print(i) } // 1 to 5
For (i in 5 downTo 1 step 2) { } // 5,3,1 20. What is Destructuring Declaration?
Used to unpack a class into multiple variables.
data class User(val name: String,
Val age: Int) val (n, a) = User("Sahand", 30)