Kotlin interview questions
Kotlin questions that are common in Android and JVM interviews, with answers that show how to reason about the language.
Types are non-null by default, and a nullable type must be marked with ?, so the compiler knows exactly where null is possible. It then makes you handle null with a safe call ?., the Elvis operator ?:, a smart cast after a null check, or !! if you accept the risk of an exception. For example, user?.address?.city ?: "Unknown" reads a nested value safely with a fallback.
val is a read-only reference that cannot be reassigned, and var can be reassigned. const val is a compile-time constant, limited to primitive types and String, and declared at top level or inside an object or companion object. A plain val can still be computed at runtime, for example val start = System.currentTimeMillis().
From the properties in the primary constructor, the compiler generates equals, hashCode, toString, copy and componentN functions. That makes data classes a good fit for models such as API responses or UI state, where two objects with the same values should count as equal. copy is handy for immutable updates, such as state.copy(isLoading = false).
An extension function adds a function to an existing type without inheriting from it or changing its source, and it is resolved statically from the declared type. I use them for small helpers that read naturally, such as String.isValidEmail() or a View.hide() function in Android code. I avoid them for logic that really belongs inside the class itself.
A coroutine is a lightweight unit of work that can suspend and resume without blocking the thread it runs on. Many coroutines can share a small pool of threads, so starting thousands of them is cheap compared with starting threads. Structured concurrency ties each coroutine to a scope, so cancelling the scope cancels all of its children.
I launch in viewModelScope so the work is cancelled when the ViewModel is cleared. The repository exposes a suspend function that uses withContext(Dispatchers.IO) for the network call, and the result updates a state holder such as StateFlow or LiveData, which the UI observes on the main thread. I wrap the call in try/catch so failures become an error state the screen can show.
Practise out loud. Say each answer once as written, then again in your own words with your own number or story.
Have a number ready. "My Kotlin score is at benchmark on a timed test" is a stronger answer than "I am good at it". Take the test and quote the result.
More in Programming