All interview guides

Kotlin Interview Questions and Answers

24 questions that come up in Kotlin technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What is CoroutineScope?

Advanced

Answer: Defines scope for coroutine lifecycle

CoroutineScope defines coroutine lifecycle. Provides context and manages child coroutines. Use GlobalScope, lifecycleScope, viewModelScope. Structured concurrency.

2. What are higher-order functions?

Intermediate

Answer: Functions taking functions as parameters or returning functions

Higher-order functions take functions as parameters or return functions. Example: fun operate(x: Int, op: (Int) -> Int) = op(x). Foundation of functional programming.

3. What is Kotlin?

Beginner

Answer: Modern statically-typed programming language for JVM, Android, JS

Kotlin is a modern, statically-typed programming language developed by JetBrains. It runs on JVM, compiles to JavaScript, and is Google's preferred language for Android development.

4. What is CoroutineContext?

Advanced

Answer: Set of elements defining coroutine behavior

CoroutineContext is indexed set of elements: Job, Dispatcher, CoroutineName, ExceptionHandler. Combine with +. Child inherits parent context.

5. What is the main advantage of Kotlin over Java?

Beginner

Answer: Concise syntax, null safety, modern features

Kotlin offers concise syntax (less boilerplate), null safety, extension functions, coroutines, smart casts, and many modern features while maintaining 100% Java interoperability.

6. What is the apply function?

Intermediate

Answer: Configures object and returns it

apply executes block on object and returns object: person.apply { name = "John"; age = 30 }. Use for object initialization. this refers to object.

7. What is the let function?

Intermediate

Answer: Executes lambda and returns result

let executes lambda with object as it and returns lambda result: name?.let { println(it.length) }. Good for null checks. it refers to object.

8. How do you declare a variable in Kotlin?

Beginner

Answer: val (immutable) or var (mutable)

Use val for immutable (read-only) variables: val x = 5. Use var for mutable variables: var y = 10. val is preferred for immutability.

9. What is Dispatcher?

Advanced

Answer: Determines thread for coroutine execution

Dispatcher determines thread: Dispatchers.Main (UI), IO (blocking IO), Default (CPU-intensive), Unconfined (inherits). Set with withContext or CoroutineScope.

10. What is withContext?

Advanced

Answer: Switches coroutine context temporarily

withContext switches context for block: withContext(Dispatchers.IO) { readFile() }. Suspends, switches, executes, returns to original. Returns last expression.

11. What is the difference between val and var?

Beginner

Answer: val is immutable, var is mutable

val (value) is read-only, assigned once. var (variable) is mutable, can be reassigned. val x = 5 cannot be changed. var y = 5 can be changed to y = 10.

12. What is the also function?

Intermediate

Answer: Performs action and returns object

also performs action and returns original object: list.also { println("Size: ${it.size}") }. Use for side effects. it refers to object.

13. What is structured concurrency?

Advanced

Answer: Parent waits for children, propagates cancellation

Structured concurrency ensures parent waits for children. Canceling parent cancels children. Exceptions propagate. Prevents coroutine leaks. Foundation of Kotlin coroutines.

14. How do you declare a function in Kotlin?

Beginner

Answer: fun functionName(params): ReturnType

Use fun keyword: fun add(a: Int, b: Int): Int { return a + b }. Single-expression functions: fun add(a: Int, b: Int) = a + b.

15. What is the run function?

Intermediate

Answer: Executes block and returns result

run executes block and returns result: val result = person.run { "$name is $age" }. Use for computing value. this refers to object.

16. What is Job?

Advanced

Answer: Cancellable thing with lifecycle

Job represents coroutine lifecycle. Returned by launch. States: New, Active, Completing, Completed, Cancelling, Cancelled. Cancel: job.cancel(). Join: job.join().

17. What is type inference in Kotlin?

Beginner

Answer: Compiler automatically determines type

Type inference allows compiler to deduce types: val x = 5 (Int inferred). Makes code more concise while maintaining type safety.

18. What is the with function?

Intermediate

Answer: Calls block with receiver as this

with is non-extension function taking receiver: with(person) { println(name) }. Returns lambda result. Use for multiple operations on object. this refers to receiver.

19. What is a sealed class?

Intermediate

Answer: Restricted class hierarchy with known subclasses

Sealed class restricts inheritance: sealed class Result. Subclasses must be in same file. Great with when for exhaustive handling. Represents restricted alternatives.

20. What is SupervisorJob?

Advanced

Answer: Job where child failure doesn't affect siblings

SupervisorJob: child failure doesn't cancel parent or siblings. Regular Job: any child failure cancels all. Use for independent operations that shouldn't affect each other.

21. What is null safety in Kotlin?

Beginner

Answer: Type system distinguishes nullable and non-nullable types

Null safety prevents NullPointerException. Types are non-nullable by default. Use ? for nullable: String? can be null, String cannot. Compile-time null safety.

22. How do you declare a nullable variable?

Beginner

Answer: Add ? after type: String?

Append ? to type for nullable: var name: String? = null. Without ?, variable cannot be null: var name: String = "John" (cannot assign null).

23. What is Flow?

Advanced

Answer: Asynchronous stream of values

Flow is cold asynchronous stream: flow { emit(1); emit(2) }. Suspending, cancellable. Terminal operators: collect, first, reduce. Operators: map, filter, transform.

24. What is destructuring declaration?

Intermediate

Answer: Unpacks object into multiple variables

Destructuring unpacks objects: val (name, age) = person. Works with data classes, Pair, List. Uses componentN() functions. Max 5 components without custom implementation.

Ready to test yourself?

The full Kotlin bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Kotlin quiz