Skip to content
Tutorials Center
My learning

Topics

Lesson 132 of 134 ยท Practice

Kotlin Interview Questions

Forty Kotlin interview questions with clear answers on the language, null safety, functions, classes, collections, coroutines and design, plus runnable examples that prove the tricky ones.

Kotlin interviews tend to follow a pattern. They open with the language basics (val and var, null safety, when), move to what makes Kotlin different from Java (extensions, data classes, sealed types), then test collections, and finish with coroutines and design judgment. This lesson collects forty questions in that order, each with a short answer you could say out loud, and five runnable examples for the questions where seeing the output settles the argument.

A good interview answer has three parts: the rule, a small example, and the reason the language works this way. Use the answers below as a starting point, and follow the links when you want the full explanation.

Language basics (1 to 10)

1. What is the difference between val and var? val is a read-only reference (assigned once); var can be reassigned. A val holding a mutable object can still change inside. See Variables.

2. What is the difference between val and const val? const val is a compile-time constant of a primitive or String, declared at top level or in an object. A plain val is set at run time and can hold any value.

3. How does null safety work? Types are non-null by default; T? allows null. The compiler requires ?., ?:, a null check or !! before use. See Null Safety.

4. What does ?: do? The elvis operator returns its left side unless it is null, otherwise the right side, which may be return or throw.

5. What is a smart cast? After a check such as if (x is String) or x != null the compiler treats x as the narrower type. It works only when x cannot change in between (a val, or a local var not captured and modified).

6. What is the difference between lateinit and by lazy? lateinit var is a non-null var you promise to set before reading (reading early throws). by lazy is a val computed on first access and then cached.

7. Is if a statement or an expression? An expression: val x = if (a) 1 else 2. So are when and try. That is why Kotlin has no ternary operator.

8. What are Unit, Nothing and Any? Any is the root of non-null types, Unit is the "no useful value" return type, and Nothing is the type of expressions that never return (throw, error()). See Any, Unit and Nothing.

9. What is the difference between == and ===? == is structural equality (calls equals), === is reference identity.

10. Are there implicit numeric conversions? No. val l: Long = intValue does not compile; write intValue.toLong(). Even 5 == 5L is rejected.

Kotlin Lazy, lateinit, elvis and expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Functions and classes (11 to 22)

11. What are default and named arguments good for? They replace overload chains and builder classes: connect(host, secure = true). See Default and Named Arguments.

12. What is an extension function? A function declared outside a class that you call as if it were a member: fun String.shout() = uppercase(). It cannot access private members and does not modify the class.

13. Are extension functions dispatched dynamically? No. They are resolved statically from the declared type of the receiver, and a member always wins over an extension with the same signature.

14. What are scope functions? let, run, with, apply and also run a block with an object as it or this; they differ in that and in what they return. See Scope Functions.

15. What does inline do and why use reified? An inline function is copied into its call sites, which removes lambda allocation and enables non-local returns. reified type parameters keep the real type at run time, so T::class and is T work. See Inline Functions.

16. What is a data class? A class with generated equals, hashCode, toString, copy and componentN based on the primary constructor properties. copy is shallow.

17. What is a sealed class or interface? A closed hierarchy whose subtypes are known to the compiler, so when over it is exhaustive without else. See Sealed Classes and Interfaces.

18. What does object do? Declares a singleton. companion object holds members associated with a class, and object : Type { } creates an anonymous object.

19. What are classes' defaults? Final, public. Use open to allow inheritance, abstract for incomplete classes.

20. What is a value class? A single-property wrapper marked @JvmInline value class that the compiler can unbox, giving type safety (UserId versus OrderId) with little cost.

21. What is fun interface? An interface with exactly one abstract method that can be implemented with a lambda (SAM conversion).

22. What is delegation? class A(b: B) : B by b forwards interface calls; val x by lazy {} delegates a property to an object with getValue. See Delegation.

Kotlin Extensions are static; sealed and value types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Collections (23 to 29)

23. What is the difference between List and MutableList? List is a read-only interface; MutableList adds modification. A List may be backed by a mutable list elsewhere, so it is a view, not guaranteed immutability. See Collections Overview.

24. map versus flatMap? map transforms each element to one result; flatMap maps each to a collection and concatenates them.

25. filter versus partition versus groupBy? filter keeps matches, partition returns a pair of matches and non-matches, groupBy builds a map from key to list.

26. fold versus reduce? fold takes an initial value and works on empty collections; reduce starts with the first element and throws on an empty collection.

27. What is a Sequence? A lazy pipeline: elements flow through all steps one at a time and only as far as needed. Lists are eager, building an intermediate list per step. See Sequences.

28. When do sequences help? For large inputs, many chained steps, or early exits (first, take). For small collections lists are usually as fast and simpler.

29. How do you remove items safely while iterating? Use removeAll { }, filter into a new list or an explicit Iterator.remove(); modifying inside a for loop throws ConcurrentModificationException.

Kotlin Eager lists versus lazy sequences

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Coroutines (30 to 36)

30. What is a coroutine? A lightweight, suspendable computation. Thousands can run on a few threads because a suspended coroutine does not hold a thread. See Coroutines.

31. What does suspend mean? The function may pause without blocking the thread and can only be called from another suspend function or a coroutine builder.

32. launch versus async? launch starts a job and returns Job (fire and forget); async returns a Deferred<T> whose await() yields the result. See Coroutine Builders.

33. What is structured concurrency? Every coroutine lives in a scope; a parent waits for its children, cancelling the parent cancels them, and a child's failure propagates upward. No orphaned work. See Structured Concurrency.

34. What does a dispatcher do? It chooses the thread(s): Dispatchers.Default for CPU work, Dispatchers.IO for blocking I/O, Main for UI. Switch with withContext. See Dispatchers and Context.

35. How does cancellation work? Cooperatively: suspension points throw CancellationException. Never swallow it; check isActive in tight loops.

36. Flow versus Sequence, and StateFlow? A flow produces values asynchronously (its operators can suspend); a sequence is synchronous. StateFlow is a hot flow that always holds a current value. See Flows.

Kotlin async, structured failure and supervisorScope

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Design and practice (37 to 40)

37. How do you model "this or that" results? With a sealed interface (Success/Failure) and an exhaustive when, or Result<T> for simple cases. Exceptions are for unexpected failures. See The Result Type.

38. How do you make code testable? Pass dependencies in through constructors (interfaces), avoid hidden globals, inject dispatchers and clocks, and write small fakes. See Test Doubles.

39. When would you avoid !!, lateinit and GlobalScope? Nearly always. !! hides a null the type should have handled, lateinit fails late, and GlobalScope creates work nobody owns. See Common Kotlin Mistakes.

40. What would you tell a Java team adopting Kotlin? Start with tests and new code, rely on interop, enable null-safe types at the boundary, and learn the idioms rather than translating line by line. See Kotlin for Java Developers.

Kotlin A small design answer: sealed results with parsing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with readln()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A class has fun Animal.sound() = "generic" and fun Dog.sound() = "woof". What does val a: Animal = Dog(); a.sound() return?

    Extension functions are dispatched statically on the declared receiver type, not the run-time type.
  2. 2. Which one throws on an empty collection?

    reduce has no initial value, so an empty collection is an error. fold, firstOrNull and sumOf handle it.
  3. 3. What happens if a coroutine catches Exception around delay and does not rethrow when cancelled?

    CancellationException is how cancellation travels. Swallowing it stops the cancellation.
  4. 4. Which is the best description of by lazy?

    lazy runs its initializer once, on first access, and stores the result.
  5. 5. Why use a sealed interface for a result type?

    The compiler knows every subtype, so it checks that all cases are handled.

Summary

  • Know the basics cold: val/var, null safety, expressions, smart casts, no implicit numeric conversion.
  • Explain Kotlin's class tools: data, sealed, object, value classes, extensions (statically dispatched) and delegation.
  • Understand collections: read-only views, eager versus lazy, fold versus reduce.
  • For coroutines, be ready to explain suspension, structured concurrency, dispatchers and cooperative cancellation.
  • For design questions, favour sealed result types, injected dependencies and small, testable functions.

That is the end of the Kotlin tutorial. Keep practicing with the Projects and the Kotlin Cheat Sheet, and build something of your own.