Kotlin standard library, collections, and coroutines
Domain 3 covers standard collection operations, scope functions, and coroutine behavior. The exam tests whether you can choose the right transformation and reason about structured concurrency rather than memorize signatures.
Collections and sequences
List, Set, and Map are read-only interfaces: they expose no mutators, but the underlying object may still be mutable. map, filter, flatMap, groupBy, and fold return new values. firstOrNull avoids exceptions; getOrElse supplies a default. A Sequence is lazy: intermediate operations such as map run only when a terminal operation such as toList or count executes. Sequences help long chains or large inputs but add overhead for small collections.
Scope functions differ by receiver and return value: let and also use it, run and apply use this; let, run, and with return the lambda result, while apply and also return the receiver.
Coroutines
A suspend function may suspend without blocking a thread and can be called only from a coroutine or another suspend function. launch returns a Job; async returns a Deferred<T> whose value is obtained with await. Structured concurrency makes a parent scope wait for its children and cancel them if a child fails. Cancellation is cooperative, so blocking calls must be replaced or checked.
Worked example
suspend fun loadPair(): Pair<String, String> = coroutineScope {
val first = async { fetchA() }
val second = async { fetchB() }
first.await() to second.await()
}
Reasoning: coroutineScope creates a child scope and waits for both async children. The calls can run concurrently. If fetchA throws, the scope cancels fetchB, and the exception propagates; loadPair never returns a partial pair.
Traps
GlobalScope and runBlocking in production code break structured concurrency or block threads. In a regular coroutineScope, a failing async cancels siblings even before await; in a supervisorScope, the failure is isolated and surfaces at await. A read-only collection reference is not a guarantee of immutability.