Bird
0
0

What does Kotlin's null safety feature ensure when working with collections like List or Set?

easy📝 Conceptual Q11 of 15
Kotlin - Null Safety
What does Kotlin's null safety feature ensure when working with collections like List or Set?
AIt prevents storing <code>null</code> values in collections declared without nullable types.
BIt automatically converts all <code>null</code> values to empty strings in collections.
CIt allows <code>null</code> values in any collection without restrictions.
DIt disables the use of collections if they contain <code>null</code> values.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's null safety in collections

    Kotlin distinguishes between nullable and non-nullable types. Collections declared with non-nullable types cannot hold null values.
  2. Step 2: Apply this to collection elements

    If a collection is declared as List<String>, it cannot contain null. To allow null, it must be declared as List<String?>.
  3. Final Answer:

    It prevents storing null values in collections declared without nullable types. -> Option A
  4. Quick Check:

    Null safety = no null in non-nullable collections [OK]
Quick Trick: Non-nullable collections disallow null elements [OK]
Common Mistakes:
MISTAKES
  • Assuming all collections allow null by default
  • Confusing null safety with automatic null replacement
  • Thinking null safety disables collections with nulls

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes