Bird
0
0

Which Kotlin code snippet safely calls printLength() on a nullable String input and provides a default message if null?

hard📝 Application Q8 of 15
Kotlin - Null Safety
Which Kotlin code snippet safely calls printLength() on a nullable String input and provides a default message if null?
Ainput?.let { printLength(it) } ?: println("No input")
BprintLength(input)
CprintLength(input!!)
Dif (input != null) printLength(input) else printLength("")
Step-by-Step Solution
Solution:
  1. Step 1: Understand safe call with let

    input?.let { printLength(it) } calls printLength only if input is not null.
  2. Step 2: Use Elvis operator for default

    The Elvis operator provides a fallback println("No input") if input is null.
  3. Final Answer:

    input?.let { printLength(it) } ?: println("No input") -> Option A
  4. Quick Check:

    Safe call with let and Elvis handles null safely [OK]
Quick Trick: Use ?.let { } ?: for safe calls with default [OK]
Common Mistakes:
MISTAKES
  • Calling function directly on nullable without safe call
  • Using !! which can throw exceptions
  • Providing empty string instead of default message

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes