Bird
0
0

Why does Swift use optionals instead of allowing null or nil values directly in variables?

hard📝 Conceptual Q10 of 15
Swift - Basics and Runtime
Why does Swift use optionals instead of allowing null or nil values directly in variables?
ATo allow variables to change type dynamically
BTo force developers to handle absence of values explicitly, reducing runtime errors
CTo make code shorter by ignoring null checks
DBecause Swift does not support nil values at all
Step-by-Step Solution
Solution:
  1. Step 1: Understand optionals in Swift

    Optionals wrap a value that may be present or absent, requiring explicit handling.
  2. Step 2: Reason why this design reduces errors

    This prevents unexpected nil crashes by making absence visible in code.
  3. Final Answer:

    To force developers to handle absence of values explicitly, reducing runtime errors -> Option B
  4. Quick Check:

    Optionals improve safety = D [OK]
Quick Trick: Optionals make nil explicit and safe to handle [OK]
Common Mistakes:
  • Thinking Swift disallows nil completely
  • Assuming optionals skip null checks
  • Believing optionals allow dynamic typing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes