Skip to content

Kotlin Null Safety Practice (Interactive)

Intuition

Learning through practice: Practice problems are like training drills — they help you apply knowledge and identify areas that need more study.

Why it matters: Regular practice builds confidence and reveals patterns in how concepts are tested. Each problem reinforces key programming concepts.

The key insight: Making mistakes during practice is valuable — each error points to a concept that needs clarification.

Kotlin — Null Safety Practice

13 auto-graded practice problems covering Kotlin null safety, scope functions, sealed classes, and coroutines. Select an answer, submit, and review the explanation.


Null Safety


Scope Functions


Sealed Classes


Coroutines


Safe Calls and Let


Also and Require


Check and Assertion


Scope Functions


Coroutines and Flows

Cross-References

Common Mistakes

Confusing require with check: require validates function arguments (throws IllegalArgumentException); check validates internal state (throws IllegalStateException). Using the wrong one misleads callers about whether a failure is bad input or a bug.

Using !! (not-null assertion) unnecessarily: !! converts any nullable type to non-nullable and throws NullPointerException if null. Prefer safe calls ?., elvis ?:, or let for safe handling.

Forgetting that lazy is thread-safe by default: by lazy uses synchronized double-checked locking. For single-threaded contexts, pass LazyThreadSafetyMode.NONE to avoid unnecessary synchronization overhead.