Kotlin
225 bites tagged Kotlin — interview questions with model answers, and 60-second explainers.
Explain Kotlin's scope functions: let, run, with, apply, also
This tests your grasp of idiomatic Kotlin and the two key differentiators: context object (`this`/`it`) and return value (object/lambda result). A great answer defines these axes, categorizes each function, and provides a clear example.
What is a `suspend` function in Kotlin?
Tests your grasp of Kotlin's core concurrency primitive. A `suspend` function can pause and resume. It must be called from another suspend function or a coroutine builder.
Difference between launch and async in Kotlin Coroutines
This tests your grasp of coroutine result handling and exception propagation. Explain that `launch` is fire-and-forget (returns `Job`), while `async` is for results (returns `Deferred`). Mention `async` defers exceptions until `await()`.
Explain `inline` and `reified` in Kotlin
Tests your grasp of JVM performance costs and type erasure. A great answer explains how `inline` avoids object allocation for lambdas, and how this enables `reified` to bypass type erasure for runtime checks. A red flag is just saying 'it's for performance'.
Compare and contrast Kotlin's `apply` and `let` scope functions
This tests your grasp of idiomatic Kotlin scope functions. `apply` uses `this` and returns the object, ideal for configuration. `let` uses `it` and returns the lambda result, useful for null-safe chaining.
When would you use a sealed class instead of an enum?
This tests your understanding of state representation. Explain that sealed classes define a hierarchy of types that can hold different data, while enums are a set of singleton constants.
Explain and implement a higher-order function in Kotlin
Tests your grasp of functions as first-class citizens. Define a higher-order function, explain it takes/returns functions, then implement `filterAndTransform` using the specified lambdas.
lateinit var vs. val by lazy in Android
Tests your grasp of Kotlin's non-nullable property initialization. Define `lateinit` (mutable, framework-init) vs. `lazy` (immutable, deferred-init). Use `lateinit` for Views in `onCreate` and `lazy` for expensive objects. Red flag: confusing their mutability.
What are the advantages of a Kotlin data class?
Tests knowledge of Kotlin's boilerplate reduction for data holders. A good answer defines the advantage (conciseness), names generated functions like `equals`, `hashCode`, and `copy`, and explains `copy` for immutable state updates.
What is a Kotlin extension function? Write one for String.
Tests your grasp of adding functionality to existing classes without inheritance. A great answer defines it as syntactic sugar over static methods, explains its use for third-party code, then implements the requested `String.hasWhitespace` function.
Purpose of Kotlin's safe call (`?.`) and Elvis (`?:`) operators?
This tests your grasp of Kotlin's null safety philosophy. A good answer defines the safe call (`?.`) for conditional access and the Elvis operator (`?:`) for default values, then combines them. A red flag is using verbose `if/else` checks for simple cases.
Explain val vs. var and null safety in Kotlin
Tests your grasp of Kotlin's core principles: immutability and compile-time null safety. Define `val` (read-only) vs. `var` (mutable), declare nullables with `?`, and identify the risk as runtime `NullPointerException`s.
Coroutine Exception Handling: launch vs. async
Coroutine builders handle exceptions differently. `launch` propagates them immediately, crashing if unhandled. `async` silently catches them, waiting for you to call `await`. Use a `CoroutineExceptionHandler` on root scopes for global logging.
Why is `LiveData<Boolean>` bad for one-time ViewModel events?
This tests your grasp of state vs. events. A `LiveData<Boolean>` is state; it re-triggers on rotation. Instead, expose events via a `StateFlow` and have the UI call an `eventConsumed()` function on the ViewModel to maintain unidirectional data flow.
How to pass data between Fragments with Jetpack Navigation?
This tests your knowledge of type-safe argument passing in Jetpack Navigation. A good answer defines the argument in XML, uses the Safe Args plugin to generate code, passes data via the Directions class, and retrieves it with the Args class.
What problem does Jetpack ViewModel solve during configuration changes?
This tests your understanding of state loss during configuration changes. A good answer explains that Activities are destroyed on rotation, and ViewModel survives this event, preserving UI state.
How do you diagnose and fix excessive recomposition in Jetpack Compose?
This tests your understanding of Compose's stability system for performance. Use the Layout Inspector and Compiler Metrics to find unstable composables, then fix them with immutable data types and stable lambdas.
Which side-effect handler for a one-time coroutine action in Compose?
This tests your understanding of Compose lifecycles and side-effect handlers. A good answer names `LaunchedEffect(Unit)`, explaining the constant key ensures the effect runs only once. A red flag is suggesting launching coroutines on every recomposition.
Explain Recomposition in Jetpack Compose
Tests your grasp of Compose's declarative model. A good answer defines recomposition, explains state-read triggers, and details how stability and positional memoization enable skipping. A red flag is assuming any state change redraws the entire UI.
What is the purpose of the `remember` function in Compose?
This tests your grasp of recomposition. Explain that `remember` stores an object in the composition tree, preventing it from being re-initialized on every render. A red flag is confusing it with `rememberSaveable`, which survives configuration changes.
Handle Android Process Death vs. Configuration Changes
Tests your grasp of Android's lifecycle for state restoration. A good answer explains that `onCreate(savedInstanceState)` is called in both cases, but process death recreates everything. Use `ViewModel` with `SavedStateHandle`.
When and why use a Foreground Service on modern Android?
Tests understanding of background work restrictions. A great answer defines the use case for user-visible tasks, explains the mandatory notification, and details the `startForegroundService()` flow.
How does a ViewModel survive configuration changes?
Tests understanding of ViewModel's scope, separate from the Activity lifecycle. A ViewModel is retained by a ViewModelStore, which persists across configuration changes. The new Activity instance then reconnects to the same ViewModel.
Describe the Android Activity lifecycle when navigating away and back
Tests understanding of resource management via lifecycle callbacks. A good answer traces onPause -> onStop -> onRestart -> onStart -> onResume, explaining what work happens in each.
Get Kotlin bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.