Bird
0
0

Why does Kotlin require var for mutable references instead of allowing all variables to be mutable by default?

hard📝 Conceptual Q10 of 15
Kotlin - Variables and Type System
Why does Kotlin require var for mutable references instead of allowing all variables to be mutable by default?
ABecause val variables can be reassigned later
BBecause var is faster than val
CTo make code shorter and simpler
DTo encourage safer code by default immutability
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's design philosophy

    Kotlin encourages immutability by default to reduce bugs and improve safety.
  2. Step 2: Explain role of var

    var explicitly marks variables as mutable, making changes intentional and clear.
  3. Final Answer:

    To encourage safer code by default immutability -> Option D
  4. Quick Check:

    Immutability for safety = D [OK]
Quick Trick: Immutable by default means safer code [OK]
Common Mistakes:
MISTAKES
  • Thinking var is faster than val
  • Believing val can be reassigned
  • Assuming var makes code shorter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes