Kotlin - Null SafetyHow can you safely iterate over a Java List platform type that might be null in Kotlin?AjavaList!!.forEach { println(it) }BjavaList.forEach { println(it) }CjavaList?.forEach { println(it) } ?: println("Empty list")Dfor (item in javaList) println(item)Check Answer
Step-by-Step SolutionSolution:Step 1: Recognize javaList may be nullPlatform types from Java can be null, so safe call is needed.Step 2: Use safe call with fallbackUsing ?.forEach runs loop if not null; ?: prints fallback message if null.Final Answer:javaList?.forEach { println(it) } ?: println("Empty list") -> Option CQuick Check:Safe call with fallback handles nullable platform collections [OK]Quick Trick: Use ?. and ?: to safely handle nullable Java collections [OK]Common Mistakes:MISTAKESCalling forEach without null checkUsing !! risking exceptionsIgnoring null fallback
Master "Null Safety" in Kotlin9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Kotlin Quizzes Collections Fundamentals - Array creation and usage - Quiz 2easy Collections Fundamentals - Collection size and emptiness checks - Quiz 14medium Data Types - Number literal formats (underscore, hex, binary) - Quiz 5medium Functions - Infix functions for readable calls - Quiz 8hard Kotlin Basics and JVM Runtime - Print and println output - Quiz 2easy Loops and Ranges - Why ranges simplify iteration - Quiz 1easy Loops and Ranges - Labeled break and continue - Quiz 9hard Null Safety - Safe call operator (?.) - Quiz 8hard Operators and Expressions - Operator precedence - Quiz 8hard Variables and Type System - Val for immutable references - Quiz 6medium