Bird
0
0

Why does Swift use the ? suffix for optionals instead of allowing nil for all types by default?

hard📝 Conceptual Q10 of 15
Swift - Optionals
Why does Swift use the ? suffix for optionals instead of allowing nil for all types by default?
ATo make code shorter and less clear
BBecause all variables in Swift are optional by default
CTo allow only constants to be nil
DTo explicitly indicate variables that can be nil, improving safety
Step-by-Step Solution
Solution:
  1. Step 1: Understand Swift's safety design

    Swift requires explicit optional declaration to avoid unexpected nil values and runtime crashes.
  2. Step 2: Analyze the purpose of ? suffix

    The ? suffix clearly marks variables that can be nil, making code safer and easier to understand.
  3. Final Answer:

    To explicitly indicate variables that can be nil, improving safety -> Option D
  4. Quick Check:

    Optional ? improves code safety by explicit nil allowance [OK]
Quick Trick: Use ? to mark variables that can be nil explicitly [OK]
Common Mistakes:
  • Assuming all variables can be nil by default
  • Thinking ? makes code less clear
  • Confusing optional with constant behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes