Bird
0
0

Which Kotlin collection type allows null elements by default?

easy📝 Conceptual Q2 of 15
Kotlin - Null Safety
Which Kotlin collection type allows null elements by default?
AList<String>
BMutableList<String>
CList<String?>
DSet<String>
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the type declaration

    List and MutableList do not allow nulls because the type String is non-nullable.
  2. Step 2: Identify nullable type usage

    List explicitly allows null elements because the element type is nullable (String?).
  3. Final Answer:

    List allows null elements by default. -> Option C
  4. Quick Check:

    Nullable element type = allows nulls [OK]
Quick Trick: Use ? after type to allow nulls in collections [OK]
Common Mistakes:
MISTAKES
  • Confusing MutableList with nullability
  • Assuming Set allows nulls by default
  • Ignoring the ? in the element type

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes