Kotlin language fundamentals and coding tasks
Domain 1 tests the rules you apply in short Kotlin coding tasks: declarations, nullability, control flow, equality, and numeric conversion. Questions often ask which snippet compiles or what a function returns, so read types carefully.
Core rules
valfixes the reference, not the object: avallist can still be mutated if its runtime type is mutable.varallows reassignment.- Kotlin does not widen numbers implicitly.
Intis not aLong; calltoLong(),toInt(), and so on. Integer division truncates. ==uses structural equality (equals), while===checks reference identity. For arrays,==is identity; usecontentEquals.- String templates use
$nameor${expression}. Use braces before a following identifier character. - A
whenused as an expression must be exhaustive. Awhenstatement does not, unless the compiler requires it for a sealed, enum, or Boolean subject.
Nullability
String? and String are different types. Safe call ?. short-circuits, Elvis ?: supplies a fallback, and !! throws NullPointerException if the value is null. Smart casts refine a nullable value after a null check when the compiler can prove the value is stable; they generally fail for a var property with a custom getter or an open property.
Worked example
fun describe(n: Int?): String = when {
n == null -> "missing"
n < 0 -> "negative"
n == 0 -> "zero"
else -> "positive: $n"
}
Reasoning: after the first branch is false, the compiler smart-casts n to Int, so n < 0 and the template compile. The subjectless when is exhaustive because of else, and every branch yields String, so the expression is valid.
Traps
Using !! to silence a warning can move the failure to runtime. Confusing == with === costs marks on arrays and wrapper types. Also check precedence in expressions such as a ?: b + c, which groups as a ?: (b + c).