Recall & Review
beginner
What is null safety in Kotlin?
Null safety is a feature in Kotlin that helps prevent errors caused by null references by distinguishing nullable and non-nullable types at compile time.
Click to reveal answer
beginner
How does Kotlin differentiate between nullable and non-nullable types?
Kotlin uses a question mark (?) after a type to mark it as nullable (e.g., String?), while types without it (e.g., String) cannot hold null values.
Click to reveal answer
intermediate
Why is null safety considered Kotlin's defining feature?
Because it eliminates the common source of runtime errors called NullPointerExceptions by catching potential null issues during compilation, making apps safer and more reliable.
Click to reveal answer
beginner
What happens if you try to assign null to a non-nullable type in Kotlin?
The Kotlin compiler will show an error and prevent the code from compiling, forcing you to handle nulls explicitly.
Click to reveal answer
intermediate
Name two Kotlin operators that help safely work with nullable types.
The safe call operator (?.) allows accessing properties or methods only if the object is not null, and the Elvis operator (?:) provides a default value if the expression is null.
Click to reveal answer
What symbol does Kotlin use to mark a type as nullable?
✗ Incorrect
Kotlin uses the question mark (?) after a type to indicate it can hold null values.
What error does Kotlin's null safety help prevent?
✗ Incorrect
Null safety helps prevent NullPointerExceptions by catching null issues at compile time.
What will happen if you assign null to a non-nullable variable in Kotlin?
✗ Incorrect
Kotlin's compiler prevents assigning null to non-nullable types, causing a compilation error.
Which operator safely accesses a property or method of a nullable object?
✗ Incorrect
The safe call operator (?.) accesses members only if the object is not null.
What does the Elvis operator (?:) do in Kotlin?
✗ Incorrect
The Elvis operator returns the left expression if not null, otherwise the right default value.
Explain why null safety is important in Kotlin and how it improves code reliability.
Think about how null errors cause crashes and how Kotlin stops them early.
You got /4 concepts.
Describe how Kotlin's type system enforces null safety with examples.
Consider how Kotlin marks types and operators to handle null values safely.
You got /4 concepts.