Kotlin Language
1. What is the difference between val, var and const val?
var can be reassigned, val cannot, and const val is a compile-time constant. A val can be set from a function call at runtime.
A const val must be a primitive or String known at compile time, declared at the top level or in an object, and the compiler inlines its value.
A val is read-only, not immutable. val items = mutableListOf(1) can still have items added. And a val with a custom getter can return a new value on every read.
2. How does null safety work, and where does it break down?
Types are non-null by default. String? allows null, and the compiler forces you to handle it before use.
The tools are the safe call ?., the Elvis operator ?: for a fallback, smart casts after a null check, and !!, which throws if the value is null.
val length = user?.email?.length ?: 0
The gap is Java. A value from Java code without nullability annotations has a "platform type" such as String!, and Kotlin trusts you with it. Assigning it to a non-null String can throw at runtime.
Good code declares the expected type at the Java boundary. Frequent !! in a code review is a warning sign.
3. What does a data class generate, and what are its limits?
A data class gets equals, hashCode, toString, componentN functions for destructuring, and copy, all from the properties in the primary constructor.
Properties declared in the class body are left out of all of them, which surprises people when two objects compare equal. copy is shallow: a copied object shares the same list instance as the original.
Data classes also make poor JPA entities, because generated equals and hashCode clash with lazy loading and generated IDs.
4. What are sealed classes and interfaces for?
A sealed type has a fixed set of direct subtypes, known at compile time, so a when over it needs no else branch. Adding a new subtype then turns every incomplete when into a compile error, which is the point.
sealed interface PaymentResult {
data class Paid(val id: String) : PaymentResult
data class Declined(val reason: String) : PaymentResult
data object Pending : PaymentResult
}
fun message(r: PaymentResult) = when (r) {
is PaymentResult.Paid -> "Paid ${r.id}"
is PaymentResult.Declined -> "Declined: ${r.reason}"
PaymentResult.Pending -> "Pending"
}
A common outdated answer says subclasses must be in the same file. Per the sealed classes docs, direct subclasses must be in the same package and module.
5. What do inline, reified, noinline and crossinline do?
inline copies a function's body, and the lambdas passed to it, into each call site. That removes the lambda object allocation and allows a return inside the lambda to return from the calling function.
reified keeps a type parameter available at runtime in an inline function, so T::class and is T work.
noinline marks one lambda parameter that must stay an object, for example to store it.
crossinline forbids a non-local return in a lambda that runs in another context, such as inside a Runnable.
inline fun <reified T> String.parse(): T = Json.decodeFromString<T>(this)
6. How do you choose between let, run, with, apply and also?
By two questions: is the object this or it inside the block, and does the block return the object or the lambda's result?
apply (this, returns the object): configure an object.
also (it, returns the object): side effects such as logging.
let (it, returns the result): run code on a non-null value with ?.let.
run and with (this, return the result): compute a value from an object.
A strong candidate also says when not to use them: nested scope functions make it hard to tell what it and this refer to.
7. What is a value class?
A value class wraps a single value in its own type, and the compiler usually removes the wrapper at runtime. It gives type safety without an extra object.
@JvmInline
value class UserId(val raw: Long)
fun load(id: UserId) { /* cannot be passed an OrderId by mistake */ }
The wrapper is kept (boxed) when the value is used as a nullable type, a generic type or an interface. Function names that take value classes are mangled in bytecode, so Java callers need @JvmName or a plain overload.
8. What do out, in and star projection mean in generics?
out T means a type only produces T, so List<String> can be used as List<Any>. in T means it only consumes T, so a Comparator<Any> can be used as a Comparator<String>.
Kotlin declares variance on the class (interface List<out E>), where Java uses wildcards at every use. Star projection, List<*>, means "a list of some type I don't know". You can read items as Any? but cannot add to it.