yoklainterview sim

Mobile Kt Kotlin Type System Null Safety Interview Questions

75 verified Mobile Kt Kotlin Type System Null Safety interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Kt Kotlin Type System Null SafetyDifficulty 1
What does declaring a class as data class automatically generate?
  • aA private constructor that prevents instantiation outside the file.
  • bA default no-argument constructor only.
  • cequals(), hashCode(), toString(), and copy().
  • dAutomatic Parcelable serialization with no annotation needed.
Explanation:data class generates structural equals/hashCode/toString/copy (and componentN functions) from the primary constructor's properties, saving boilerplate.
Kt Kotlin Type System Null SafetyDifficulty 1
data class Point(val x: Int, val y: Int)
val p1 = Point(1, 2)
val p2 = Point(1, 2)
println(p1 == p2)

What is printed?
  • afalse, because == checks reference identity for classes.
  • bIt depends on whether Point is also marked open.
  • cA compile error, data class cannot override ==.
  • dtrue
Explanation:data class generates a structural equals(), so == compares property values, not references; two instances with equal x/y are equal.
Kt Kotlin Type System Null SafetyDifficulty 2
A data class Account(val id: String, var balance: Double) is put into a HashSet. After insertion, balance is mutated on the object already inside the set. What risk does this create?
  • aNo risk, HashSet recomputes hash codes automatically on every mutation.
  • bThe object's hash code can change, breaking lookups in the set.
  • cbalance mutation triggers copy() internally, creating a new element.
  • dThe mutation is rejected at runtime since HashSet elements are immutable.
Explanation:Mutable properties used in hashCode() are dangerous inside hash-based collections: mutating the object after insertion can move where its correct bucket would be, while the set still stores it in the old bucket, silently breaking contains/lookup.
Kt Kotlin Type System Null SafetyDifficulty 1
What does calling .copy(balance = 500.0) on a data class Account(val id: String, val balance: Double) instance do?
  • aMutates balance on the original instance in place.
  • bThrows at runtime since balance was declared val.
  • cDeletes the original instance and replaces every reference to it.
  • dReturns a new instance with balance replaced.
Explanation:copy() (generated for data class) returns a new instance, letting you override selected constructor properties while reusing the rest — the original instance is untouched.
Kt Kotlin Type System Null SafetyDifficulty 2
Why does the compiler allow an exhaustive when expression over a sealed class hierarchy without an else branch?
  • aThe subclass set is closed, so the compiler can enumerate every case.
  • bBecause sealed class forces every subclass to be an object, so there are always exactly two cases.
  • cBecause when always requires an else branch regardless of the type.
  • dBecause sealed classes cannot be subclassed at all, so there is only one case.
Explanation:A sealed class's subclasses are restricted to a known, closed set the compiler can fully enumerate, so a when that covers every subclass is provably exhaustive and doesn't need a catch-all else.
Kt Kotlin Type System Null SafetyDifficulty 2
sealed class Result
data class Ok(val v: Int) : Result()
data class Err(val msg: String) : Result()

fun describe(r: Result): String = when (r) {
    is Ok -> "ok: ${r.v}"
    is Err -> "err: ${r.msg}"
}

A new subclass object Pending : Result() is added later. What happens to describe?
  • aNothing, describe keeps compiling and silently returns null for Pending.
  • bThe new subclass is rejected at compile time because Result already has two subclasses.
  • cThe compiler now reports the when as non-exhaustive.
  • ddescribe throws a runtime exception the first time it is called with any argument.
Explanation:Because Result is sealed, the compiler tracks every subclass; adding Pending makes the existing when (used as an expression, requiring exhaustiveness) incomplete, and the compiler flags the missing branch instead of letting it compile silently.

Test yourself against the 2400-question Mobile bank.

Start interview