Explain val vs var in Kotlin and null safety risks

Kotlin mutability and compile-time null safety.
val is read-only, var is mutable; nullables use ? like String?; risk is NullPointerException if checks are missed.
Calling val deep immutability or defaulting to !!.
What's really being asked
This question checks whether you understand two foundational Kotlin type-system concepts: reference mutability and null safety. The interviewer wants to see that you know val controls reassignment of the reference, not the underlying object state, and that you treat nullability as an explicit type-level contract rather than an afterthought.
A GOOD ANSWER COVERS four things in order. First, define val as a read-only reference that cannot be reassigned once initialized, while var is a mutable reference that can be changed. Second, clarify that val does not guarantee deep immutability; if the object is a mutable list or a class with var properties, its contents can still change. Third, explain that a nullable variable is declared by appending a question mark to the type, such as String?, and that the compiler then forces you to handle the null case before accessing members. Fourth, state that the main risk of nullable types is NullPointerException if you bypass safety with the not-null assertion operator or fail to check for null, and that nullability tends to propagate through call chains, increasing cognitive overhead and defensive code.
COMMON WRONG ANSWERS include saying val makes an object deeply immutable, which is false because a val holding a MutableList can still have items added. Another red flag is claiming Kotlin eliminates all NullPointerExceptions; in reality NPEs can still occur through explicit throws, the !! operator, leaking this during initialization, or Java interop with platform types. A third mistake is recommending the !! operator as a normal fix; senior candidates should prefer safe calls, Elvis operators, or early returns.
LIKELY FOLLOW-UPS include asking when to choose val over var, which should be answered with prefer val by default for readability and thread-safety of references. The interviewer may also ask how to handle nullable collections, or how lateinit and nullable types differ, or how Kotlin null safety maps to Java code via platform types and annotations.
A concrete example
Declare a non-nullable user name with val name: String = Ada, which cannot be reassigned. Declare a nullable nickname with var nickname: String? = null. Accessing nickname.length fails to compile, so you must use nickname?.length ?: 0 to safely get a default length. If you instead write nickname!!.length, you risk a NullPointerException at runtime when nickname is null.
Interview question
In Kotlin, what does declaring a variable with val guarantee about the referenced object?
- a.All properties of the object are automatically converted to read-only
- b.The object becomes deeply immutable and thread-safe by default
- c.The reference cannot be reassigned, though the object's contents may still mutateCorrect
- d.The compiler prevents any modification to the object's internal state
Why? this is the answer
val only prevents reassigning the reference itself, so a val holding a MutableList can still be modified. The most tempting distractor confuses reference immutability with deep object immutability, which val does not enforce.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
- #kotlin
- #null-safety
- #val-var
- #android
- #interview
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles