Bird
0
0

Given val data: List?, which Kotlin expression safely returns the length of the first string or null if data or the first element is null?

hard📝 Application Q8 of 15
Kotlin - Null Safety
Given val data: List?, which Kotlin expression safely returns the length of the first string or null if data or the first element is null?
Adata.get(0)?.length
Bdata?.get(0)?.length
Cdata?.get(0).length
Ddata.get(0).length
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable types

    data is nullable list of nullable strings.
  2. Step 2: Access first element safely

    Use data?.get(0) to safely get first element or null if list is null.
  3. Step 3: Access length safely

    Use ?.length to safely get length if first element is not null.
  4. Final Answer:

    data?.get(0)?.length -> Option B
  5. Quick Check:

    Safe call needed on both list and element [OK]
Quick Trick: Use ?. twice to safely access nested nullable elements [OK]
Common Mistakes:
MISTAKES
  • Not using safe call on the list
  • Not using safe call on the element
  • Assuming non-null types without checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes