Expert: features and shipping · Android career path · topic 48 of 50 · 35 min
Interview questions: Kotlin
Explanation
How to answer
Use a simple structure: one-line definition, why it exists, a two-line example, and one common pitfall. Speak slowly and stop when you have answered; long answers often wander into mistakes.
Q1. val versus var?
Model answer: "val is a read-only reference that cannot be reassigned; var can be reassigned. I use val by default because values that do not change make code easier to reason about. Note that val does not make the object immutable: a val list = mutableListOf() can still be changed."
Q2. How does Kotlin handle null?
Model answer: "Types are non-null by default; String? is nullable. The compiler forces safe handling: ?. for safe calls, ?: (Elvis) for defaults, smart casts after checks, and let for running code only when not null. !! forces a value and throws if null, so I avoid it."
Q3. What does a data class give you?
Model answer: "equals and hashCode based on the primary-constructor properties, toString, copy for creating changed copies, and componentN functions for destructuring. It is ideal for models and immutable UI state."
Q4. Sealed class versus enum?
Model answer: "An enum has a fixed set of single instances with the same properties. A sealed class or interface has a fixed set of subtypes, each of which can hold different data and have many instances. I use sealed types for UI states like Loading, Success(data), Error(message), and the compiler checks when expressions are exhaustive."
Q5. Extension functions and scope functions?
Model answer: "Extensions add functions to existing types without inheritance, resolved statically. Scope functions run a block on an object: let and also refer to it as it, run, with and apply as this; apply and also return the object, let, run and with return the block result. I use apply for configuration and ?.let for null checks."
Q6. Coroutines versus threads?
Model answer: "Threads are operating-system resources and heavy; coroutines are lightweight tasks that suspend instead of blocking, so thousands can run on a few threads. Suspend functions pause without blocking the thread, and structured concurrency ties coroutines to scopes so they are cancelled with their owner."
Q7. launch versus async?
Model answer: "launch starts a coroutine for work without a result and returns a Job. async returns a Deferred, and await gives the result. I use async to run independent calls concurrently, starting all before awaiting. Exceptions in async surface at await."
Q8. Flow versus StateFlow versus SharedFlow?
Model answer: "Flow is cold: it runs for each collector when collected. StateFlow is hot and always holds a current value, which suits UI state. SharedFlow is hot without a required current value and suits events. A ViewModel exposes StateFlow and keeps MutableStateFlow private."
Q9. A quick output question
Expect small snippets such as: val list = listOf(1, 2, 3, 4); println(list.filter { it % 2 == 0 }.map { it * it }). The answer is [4, 16]. Read every step aloud and work it through rather than guessing.
Common interview mistakes
- Saying "val means immutable". It means the reference cannot be reassigned.
- Saying coroutines "are threads". They run on threads but are not threads.
- Forgetting that ?. returns a nullable result.
- Memorised answers with no example. Always give a tiny example from your own project.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Interview questions: Kotlin · 1/6
Answer Kotlin questions with a definition, the reason, a tiny example and one pitfall.
Prefer a video by a teacher? Find free videos on “kotlin interview questions android coroutines flow sealed class” on YouTube ↗
In real life: A typical live question
An interviewer at a fintech startup shares this code and asks what it prints and why. A strong candidate traces it step by step: the safe call skips the null, Elvis supplies 0, and filter plus sumOf add only positive balances.
data class Account(val name: String, val balance: Int?)
fun main() {
val accounts = listOf(Account("A", 500), Account("B", null), Account("C", -200))
val total = accounts.map { it.balance ?: 0 }.filter { it > 0 }.sumOf { it }
println(total) // 500
}Practice (0/3 done)
What does this print? val names = listOf("ravi", "asha", "om") println(names.sortedBy { it.length }.first().uppercase())
An interviewer asks: "Does val make a list immutable?" Best answer?
Record yourself answering the 8 Kotlin questions from this topic, each in under a minute, with an example from your own code. Listen back, mark weak answers, rewrite two of them and record again.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Interview questions: Android and ComposeAndroid career path · same module
- Portfolio and next stepsAndroid career path · same module
- Interview questions: DartFlutter and Dart · Flutter careers and interviews
- Interview questions: ownership, borrowing and lifetimesRust Programming · Rust careers and interviews
- Interview questions: traits, errors, pointers and concurrencyRust Programming · Rust careers and interviews
- Interview questions: FlutterFlutter and Dart · Flutter careers and interviews
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which is the best description of a coroutine?
Answer: A lightweight task that can suspend without blocking a thread
Coroutines run on threads but are much lighter.
Which Kotlin feature should you avoid because it can throw a NullPointerException?
Answer: !!
!! asserts non-null.
Which type should a ViewModel expose for screen state?
Answer: StateFlow
Read-only, always has a value.
When is a sealed interface better than an enum?
Answer: When cases carry different data
Subtypes can hold their own data.
Which Dart 3 feature makes a switch over UI states exhaustive?Related
What is the best fix for "returns a reference to data owned by the current function"?Related
Answer: Return an owned value
Move ownership out instead of borrowing a local.
From Interview questions: ownership, borrowing and lifetimes
Is RefCell<T> safe to share between threads?Related
Answer: No, it is not Sync
Use Mutex or RwLock across threads.
From Interview questions: traits, errors, pointers and concurrency
Which statement about StateFlow is true?Related
Answer: It always holds a current value
StateFlow requires an initial value and always has one.
From Flow and StateFlow
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Android career path” before its exam: Interview questions: Android and Compose, Portfolio and next steps.
- Module 12 exam: Android career path · 10 questions · 20 min, without help. 4 topics of this module still to finish first.
- 50 topics left in the course before the final exam.