Bird
0
0

Which of the following Kotlin declarations correctly handles a Java platform type that might be null?

easy📝 Syntax Q12 of 15
Kotlin - Null Safety
Which of the following Kotlin declarations correctly handles a Java platform type that might be null?
Aval name: String!! = javaObject.getName()
Bval name: String = javaObject.getName()
Cval name: String? = javaObject.getName()
Dval name: String = javaObject.getName()!!
Step-by-Step Solution
Solution:
  1. Step 1: Recognize platform type usage

    Java method returns a platform type; Kotlin doesn't know if it's nullable.
  2. Step 2: Choose safe Kotlin type

    Declaring as nullable String? safely handles possible null values.
  3. Final Answer:

    val name: String? = javaObject.getName() -> Option C
  4. Quick Check:

    Use nullable type for platform types to avoid crashes [OK]
Quick Trick: Use nullable type (String?) for uncertain Java values [OK]
Common Mistakes:
MISTAKES
  • Using non-nullable type without null check
  • Using double-bang (!!) unnecessarily
  • Declaring invalid Kotlin types like String!!

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes