Test yourself under real exam conditions: 50 timed questions, 60 on the clock, pass mark 70%%. Instant score with a full review of everything you got wrong. Free — no account needed.
Exam details
Null safety is a Kotlin feature that eliminates NullPointerException at compile time.
By default, variables cannot hold null. You must explicitly declare a type as nullable using ?, e.g. var name: String? = null.
Append a ? to the type declaration:var name: String? = null
Without the ?, the compiler will reject any attempt to assign null.
The safe call operator ?. allows you to access a property or call a method on a nullable object without risking a NullPointerException:val length = name?.length
If name is null, the expression returns null instead of throwing.
The Elvis operator ?: provides a default value when an expression is null:val len = name?.length ?: 0
If the left side is null, the right side is used.
The !! operator converts a nullable type to a non-null type and throws a KotlinNullPointerException if the value is null:val len = name!!.length
Use it only when you are absolutely certain the value is not null.