Bird
0
0

Why does Swift require var to declare mutable variables instead of allowing all variables to be changed by default?

hard📝 Conceptual Q10 of 15
Swift - Variables and Constants
Why does Swift require var to declare mutable variables instead of allowing all variables to be changed by default?
ABecause <code>var</code> is faster than <code>let</code>
BTo make all variables global by default
CTo improve code safety by distinguishing changeable from constant values
DBecause Swift does not support constants
Step-by-Step Solution
Solution:
  1. Step 1: Understand Swift's design philosophy

    Swift encourages immutability by default to prevent accidental changes and bugs.
  2. Step 2: Role of var keyword

    var explicitly marks variables as mutable, improving code clarity and safety.
  3. Final Answer:

    To improve code safety by distinguishing changeable from constant values -> Option C
  4. Quick Check:

    var vs let improves safety and clarity [OK]
Quick Trick: Use var only when you need to change values [OK]
Common Mistakes:
  • Thinking var is faster
  • Believing all variables are global
  • Ignoring constants in Swift
  • Confusing var with let purpose

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes