Bird
0
0

Which of the following Kotlin variable declarations allows the variable to hold either a String value or null?

easy📝 Conceptual Q2 of 15
Kotlin - Null Safety
Which of the following Kotlin variable declarations allows the variable to hold either a String value or null?
Avar name: String?
Bvar name: String
Cvar name: Nullable<String>
Dvar name: String!!
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin nullability

    In Kotlin, appending ? to a type makes it nullable, allowing it to hold null.
  2. Step 2: Analyze options

    • var name: String? allows null.
    • var name: String does not allow null.
    • Nullable is not valid Kotlin syntax.
    • String!! is invalid syntax.
  3. Final Answer:

    var name: String? -> Option A
  4. Quick Check:

    Nullable types use ? suffix [OK]
Quick Trick: Use ? suffix to allow null values [OK]
Common Mistakes:
MISTAKES
  • Assuming all types are nullable by default
  • Using invalid syntax like Nullable
  • Confusing !! operator with type declaration

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes