Interview prep · Stage 1: Kotlin and DSA basics

Kotlin val vs var interview questions

Junior · 5 questions · 3 min read

One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.

Try each question out loud before you open the answer.

Opening question

What is the difference between val and var?

Show answer
var query = "kotlin"
query = "compose"   // fine

val email = "hello@imayankkumar.com"
email = "hi@imayankkumar.com" // 'val' cannot be reassigned.

Junior answer

val is a constant and var is a variable.

Senior answer

A var can be assigned a new value later. A val is assigned exactly once, though not always on the same line: you can declare it and assign it later if every code path assigns it once. In a class, a val property compiles to a private final field with only a getter, a var to a field with a getter and a setter.

What they're really checking: Calling val a constant. That's const val, which is a different thing.

Follow-up 1

Is a val immutable?

Show answer
val recent = mutableListOf("kotlin")
recent.add("compose")      // fine: the list changes
recent = mutableListOf()   // error: the name can't change

Junior answer

Yes, a val can never change.

Senior answer

No, it's read-only. val locks the reference, not the object. A val that holds a MutableList can still add items. Even a read-only List can change if someone else holds it as a MutableList. For real immutability, use immutable types, make copies, or kotlinx.collections.immutable.

What they're really checking: Thinking a read-only List is immutable. It's a view with no add(), not a guarantee.

Follow-up 2

Can reading the same val property twice give two different values?

Show answer
class Session(private val expiresAtMillis: Long) {
    val isExpired: Boolean
        get() = System.currentTimeMillis() > expiresAtMillis
}

Answer

Yes. A val property can have a custom getter that runs on every read, like val isExpired get() = System.currentTimeMillis() > expiresAtMillis. And a subclass can override an open val with a var (not the other way round). So val on a property only promises that callers can't set it.

What they're really checking: Assuming val means "same value forever". It means "no setter".

Follow-up 3

What is const val, and how is it different from val?

Show answer
const val MAX_RECENT = 10                     // fine
val startedAt = System.currentTimeMillis()    // fine, runtime
const val builtAt = System.currentTimeMillis()
// Const 'val' initializer must be a constant value.

Junior answer

It's the same as val, just more constant.

Senior answer

A val gets its value when the code runs. A const val is known when the code compiles. It's only allowed at the top level, in an object or in a companion object, only for primitive types and String, and the initializer must be a constant. The compiler copies the value into every caller, so MAX_RECENT + 1 compiles to the number 11.

What they're really checking: Changing a const val in a library and expecting old callers to see the new value without recompiling.

Follow-up 4

Your StateFlow holds a data class with val recent: MutableList<String>. You call recent.add(query) and the screen doesn't update. Why, and how do you fix it?

Show answer
data class SearchState(val recent: List<String> = emptyList())

fun onSearch(query: String) {
    _state.update { it.copy(recent = it.recent + query) }
}

Answer

add() changes the object in place, so nothing new is set on the flow. Even setting the same object again doesn't help, because StateFlow compares values with equals and the object is equal to itself. Fix: make the field a read-only List, and on each change create a new state with copy(recent = recent + query) inside update {}. The flow sees a different value and emits, and no one can change the list behind your back.

What they're really checking: Fixing it with _state.value = _state.value. Same object, equals is true, no emission.

Say it in 30 seconds

Show the answer

var can be assigned again, val is assigned once. But val is read-only, not immutable: it locks the reference, not the object, so a val MutableList can still grow. For a property, val only means there is no setter: a custom getter can return a new value on every read, and a subclass can override an open val with a var. On the JVM a val property is a private final field with a getter, a var adds a setter. const val is different: it's a compile-time constant, only for primitives and String, at the top level or in an object or companion object, and the compiler copies its value into callers. On Android, mutating a list inside StateFlow state doesn't emit, because StateFlow compares with equals and the object is equal to itself. Use a read-only List and copy() instead.

How did you do?