Swift - Control FlowWhy does Swift require switch statements to be exhaustive, either by covering all cases or including a default?ATo allow fallthrough behavior between cases.BTo make code shorter and easier to write.CBecause Swift does not support if-else statements.DTo prevent runtime errors by ensuring all values are handled.Check Answer
Step-by-Step SolutionSolution:Step 1: Understand exhaustiveness in Swift switchSwift enforces exhaustiveness to guarantee all possible values are handled safely.Step 2: Evaluate other optionsOptions B, C, and D are incorrect because they do not explain safety or exhaustiveness reasons.Final Answer:To prevent runtime errors by ensuring all values are handled. -> Option DQuick Check:Exhaustive switch = Safe code, no missing cases [OK]Quick Trick: Exhaustiveness avoids unexpected runtime failures [OK]Common Mistakes:Thinking exhaustiveness is for code brevityBelieving Swift lacks if-elseConfusing exhaustiveness with fallthrough
Master "Control Flow" in Swift9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Swift Quizzes Data Types - Numeric literal formats - Quiz 2easy Data Types - Typealias for custom naming - Quiz 4medium Data Types - Tuples for grouped values - Quiz 9hard Operators and Expressions - Nil coalescing operator deep usage - Quiz 5medium Optionals - Nil coalescing operator (??) - Quiz 3easy Optionals - Optional chaining with ?. - Quiz 11easy Optionals - Why optionals are Swift's core safety feature - Quiz 10hard Swift Basics and Runtime - Main entry point and @main attribute - Quiz 5medium Variables and Constants - Why let and var distinction matters - Quiz 6medium Variables and Constants - Semicolons are optional behavior - Quiz 15hard