Kotlin - Control Flow as ExpressionsIn Kotlin, which condition allows smart casting inside a when expression?AWhen the variable is declared inside a function only.BWhen the variable is declared as <code>var</code> and changed inside <code>when</code>.CWhen the variable is a primitive type only.DWhen the variable is immutable or not modified after the check.Check Answer
Step-by-Step SolutionSolution:Step 1: Identify smart cast requirementsKotlin smart casts work only if the variable is immutable or not changed after the type check.Step 2: Understand mutability impactIf the variable is mutable and changed, Kotlin cannot guarantee type safety, so smart cast is not applied.Final Answer:When the variable is immutable or not modified after the check. -> Option DQuick Check:Smart casts require immutability or no modification [OK]Quick Trick: Smart casts need stable (unchanged) variables [OK]Common Mistakes:MISTAKESAssuming smart casts work with mutable variablesBelieving smart casts depend on variable type onlyThinking smart casts require variable declaration location
Master "Control Flow as Expressions" in Kotlin9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Kotlin Quizzes Collections Fundamentals - Set creation (setOf, mutableSetOf) - Quiz 8hard Collections Fundamentals - Map creation (mapOf, mutableMapOf) - Quiz 15hard Control Flow as Expressions - When as expression returning value - Quiz 2easy Control Flow as Expressions - When with multiple conditions - Quiz 15hard Data Types - Char type and Unicode behavior - Quiz 7medium Data Types - Any type as universal base - Quiz 9hard Kotlin Basics and JVM Runtime - Print and println output - Quiz 3easy Null Safety - Nullable types with ? suffix - Quiz 11easy Null Safety - Safe casts with as? - Quiz 6medium Variables and Type System - Type inference by the compiler - Quiz 11easy