Kotlin Professional Certificate study materials
Kotlin Professional Certificate study resources

Kotlin Professional Certificate: Kotlin language fundamentals and coding tasks

Kotlin Professional Certificate · Work through your exam, one topic at a time.

All guide sections

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

  • val fixes the reference, not the object: a val list can still be mutated if its runtime type is mutable. var allows reassignment.
  • Kotlin does not widen numbers implicitly. Int is not a Long; call toLong(), toInt(), and so on. Integer division truncates.
  • == uses structural equality (equals), while === checks reference identity. For arrays, == is identity; use contentEquals.
  • String templates use $name or ${expression}. Use braces before a following identifier character.
  • A when used as an expression must be exhaustive. A when statement 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).