Bird
0
0

Given a nullable list of nullable strings val items: List?, which expression safely gets the length of the first string or returns null if any part is null?

hard📝 Application Q15 of 15
Kotlin - Null Safety
Given a nullable list of nullable strings val items: List?, which expression safely gets the length of the first string or returns null if any part is null?
Aitems?.get(0)?.length
Bitems.get(0).length
Citems!![0]!!.length
Ditems?[0].length
Step-by-Step Solution
Solution:
  1. Step 1: Understand the nullability

    items can be null, and its elements can also be null. So both the list and the first element might be null.
  2. Step 2: Use safe calls to avoid errors

    To safely get the length of the first string, use items?.get(0)?.length. This returns null if items or the first element is null.
  3. Final Answer:

    items?.get(0)?.length -> Option A
  4. Quick Check:

    Chain safe calls for nested nullables [OK]
Quick Trick: Chain ?. for each nullable level [OK]
Common Mistakes:
MISTAKES
  • Using !! which throws exception if null
  • Missing safe call on list or element
  • Using invalid syntax like ?[0]

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes