Bird
0
0

Why does Swift require constants declared with let to be initialized immediately or before use?

hard📝 Conceptual Q10 of 15
Swift - Variables and Constants
Why does Swift require constants declared with let to be initialized immediately or before use?
ATo ensure the constant has a fixed value and prevent runtime errors.
BBecause Swift does not support variables.
CTo allow constants to be changed later if needed.
DTo make the code run faster by skipping initialization.
Step-by-Step Solution
Solution:
  1. Step 1: Understand constant initialization rules

    Swift requires constants to have a value before use to guarantee immutability and safety.
  2. Step 2: Reason about the purpose

    This prevents runtime errors from uninitialized constants and ensures their value never changes.
  3. Final Answer:

    To ensure the constant has a fixed value and prevent runtime errors. -> Option A
  4. Quick Check:

    Immediate initialization = safety and immutability [OK]
Quick Trick: Initialize constants immediately to avoid errors [OK]
Common Mistakes:
  • Thinking constants can be changed later
  • Believing Swift lacks variables
  • Assuming initialization affects speed only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes