Kotlin Professional Certificate flashcards
Kotlin Professional Certificate · Build recall with a focused review session.
31 cards. Read the term and the definition here. The deck at the top of this page is optional.
- val versus var
- val binds a reference that cannot be reassigned after initialization; var allows reassignment. Neither keyword makes the referenced object immutable, since a val may point to a mutable collection.
- Elvis operator behavior
- The ?: operator returns the left operand if it is non-null, otherwise the right operand. It short-circuits, so the right side is evaluated only when the left is null.
- Smart cast conditions
- After an is-check or null-check, the compiler treats the value as the narrower type when it can prove the reference cannot change. Local vals qualify; mutable or captured vars usually do not.
- Structural versus referential equality
- == delegates to equals() and compares content; === compares object identity. Data classes generate content-based equals and hashCode from primary constructor properties.
- Exhaustive when expression
- A when used as an expression must cover every possibility. With sealed, enum, or Boolean subjects the compiler checks exhaustiveness and no else branch is required.
- Raw string trimming
- Triple-quoted strings keep literal newlines and indentation. trimIndent() removes common leading whitespace and the first/last blank lines; trimMargin() strips a prefix such as a pipe character.
- Range and progression operators
- a..b is inclusive, a until b excludes the upper bound, downTo counts downward, and step changes the increment. These produce Progressions usable with in and for loops.
- Primary constructor property promotion
- Prefixing a constructor parameter with val or var turns it into a stored property with generated accessors. Without it, the parameter is only available during initialization and in init blocks.
- open, final, and override
- Classes and members are final by default. open permits subclassing or overriding; override is required on the overriding member, and overridden members remain open unless marked final.
- Sealed class exhaustiveness
- A sealed type restricts direct subclasses to the same package and module. Because the compiler knows every subtype, when expressions over it can be exhaustive without an else branch.
- Data class generated members
- A data class generates equals, hashCode, toString, copy, and componentN functions based on primary constructor properties. It cannot be abstract, open, sealed, or inner.
- Companion object semantics
- An object declared inside a class with the companion modifier is a single instance accessed through the class name. Members behave like statics only with @JvmStatic on the JVM.
- Inline and reified types
- Marking a function inline copies its body into call sites, enabling non-local returns and avoiding lambda allocation. reified type parameters require inline and allow runtime is-checks and ::class access.
- Scope function selection
- let and also receive the object as it; run, with, and apply receive it as this. let, run, and with return the lambda result; apply and also return the receiver.
- Function type syntax
- A function type such as (Int, String) -> Boolean describes parameters and return type. A trailing lambda passes the final function argument outside the parentheses for readability.
- Read-only collection interfaces
- List, Set, and Map expose no mutation methods, but they are views over possibly mutable data. mutableListOf and similar factories return MutableList, which adds mutators.
- Sequence laziness
- asSequence builds a lazy pipeline where intermediate operators such as map and filter do nothing until a terminal operator like toList or first runs, avoiding intermediate collections.
- fold versus reduce
- fold takes an explicit initial accumulator and works on empty collections. reduce uses the first element as the initial value and throws on an empty collection.
- groupBy and associateBy
- groupBy returns a Map of key to List of matching elements, preserving duplicates. associateBy returns a Map of key to single element, keeping the last value for duplicate keys.
- launch versus async
- launch starts a coroutine and returns a Job, discarding results and reporting failures through the parent. async returns a Deferred whose value is retrieved with await.
- Structured concurrency guarantees
- A coroutine scope does not complete until all children finish. Cancelling the parent cancels children, and a child failure cancels siblings unless a SupervisorJob isolates it.
- Coroutine dispatcher choices
- Dispatchers.Default suits CPU-bound work, IO suits blocking calls, and Main suits UI updates. withContext switches dispatcher for a block and returns its result to the caller.
- Cooperative cancellation
- Coroutines cancel cooperatively at suspension points. delay and other suspend calls observe cancellation; tight CPU loops must call ensureActive or yield to become cancellable.
- suspend function restrictions
- A suspend function may call other suspend functions and suspend without blocking a thread. It can only be invoked from a coroutine or another suspend function.
- Gradle Kotlin DSL files
- Kotlin projects use build.gradle.kts with the Kotlin Gradle plugin applied. The jvmToolchain setting pins the JDK used for compiling and running, keeping builds reproducible.
- Test source set dependencies
- Tests live in src/test/kotlin and declare libraries with testImplementation, which keeps them off the main compile classpath and out of the production artifact.
- kotlin.test assertions
- kotlin.test offers assertEquals, assertTrue, and assertFailsWith, which delegates to the underlying framework. JUnit 5 runs require useJUnitPlatform in the Gradle test task.
- runTest virtual time
- kotlinx-coroutines-test runTest skips delay calls using a virtual clock, so suspend code completes instantly and deterministically without real waiting.
- Turbine flow testing
- Turbine's test block collects a Flow and asserts emissions in order with awaitItem and awaitComplete, failing on timeout or unexpected termination, which verifies cold flows.
- MockK suspend stubbing
- Suspend functions must be stubbed and verified with coEvery and coVerify. Plain every and verify target non-suspend functions and fail or misbehave on suspend calls.
- Backtick test names
- On the JVM, test function names may be wrapped in backticks to contain spaces and punctuation, making failure reports read as plain sentences describing the expected behavior.