Bird
0
0

Identify the error in this Kotlin code snippet:

medium📝 Debug Q14 of 15
Kotlin - Null Safety
Identify the error in this Kotlin code snippet:
val text: String? = null
text.let { println(it.length) }
AIt prints 0 because null is treated as empty string.
BIt compiles but does not print anything.
CIt throws NullPointerException because safe call is missing.
DIt prints 'null' as a string.
Step-by-Step Solution
Solution:
  1. Step 1: Check safe call usage

    The code uses text.let without ?., so it runs even if text is null.
  2. Step 2: Understand null dereference

    Inside the lambda, it.length is called on null, causing a NullPointerException.
  3. Final Answer:

    It throws NullPointerException because safe call is missing. -> Option C
  4. Quick Check:

    Missing ?. causes null error [OK]
Quick Trick: Always use ?.let to avoid null errors [OK]
Common Mistakes:
MISTAKES
  • Assuming let skips null without safe call
  • Thinking null acts like empty string
  • Ignoring possibility of NullPointerException

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes