Bird
0
0

Which of the following Kotlin variable declarations is correct to allow null values?

easy📝 Syntax Q12 of 15
Kotlin - Null Safety
Which of the following Kotlin variable declarations is correct to allow null values?
Aval name: String? = null
Bval name: String?
Cval name: String = null
Dval name: String = "null"
Step-by-Step Solution
Solution:
  1. Step 1: Identify nullable type syntax

    Adding ? after the type allows the variable to hold null.
  2. Step 2: Check initialization

    Nullable variables must be initialized or declared with null explicitly, so val name: String? = null is correct.
  3. Final Answer:

    val name: String? = null -> Option A
  4. Quick Check:

    Nullable type with ? and initialized to null [OK]
Quick Trick: Use ? and initialize nullable variables [OK]
Common Mistakes:
MISTAKES
  • Declaring nullable without ?
  • Not initializing nullable variables
  • Assigning null to non-nullable types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes