Bird
0
0

Why does Kotlin require explicit nullable types in collections instead of allowing nulls by default?

hard📝 Conceptual Q10 of 15
Kotlin - Null Safety
Why does Kotlin require explicit nullable types in collections instead of allowing nulls by default?
ATo make collections immutable by default.
BBecause Kotlin does not support null values anywhere in the language.
CTo reduce memory usage by disallowing nulls.
DTo prevent unexpected null pointer exceptions by enforcing null safety at compile time.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's null safety philosophy

    Kotlin enforces null safety to avoid runtime crashes caused by null pointer exceptions.
  2. Step 2: Explain explicit nullable types in collections

    By requiring explicit nullable types, Kotlin makes developers consciously handle nulls, catching issues at compile time.
  3. Final Answer:

    To prevent unexpected null pointer exceptions by enforcing null safety at compile time. -> Option D
  4. Quick Check:

    Explicit nullability = safer code, fewer runtime errors [OK]
Quick Trick: Explicit nullability avoids hidden null pointer errors [OK]
Common Mistakes:
MISTAKES
  • Thinking Kotlin disallows nulls completely
  • Confusing null safety with immutability
  • Assuming nulls are disallowed to save memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes