Bird
0
0

How would you explicitly declare a nullable string variable in Kotlin and assign it a null value?

hard📝 Application Q9 of 15
Kotlin - Variables and Type System
How would you explicitly declare a nullable string variable in Kotlin and assign it a null value?
Aval name: String = null
Bvar name: String? = null
Cvar name: String = ?null
Dval name: String? = "null"
Step-by-Step Solution
Solution:
  1. Step 1: Understand nullable type syntax

    In Kotlin, a nullable type is declared by adding a question mark after the type, e.g., String?.
  2. Step 2: Assign null to nullable variable

    var name: String? = null correctly declares a mutable nullable string variable and assigns it null.
  3. Final Answer:

    var name: String? = null -> Option B
  4. Quick Check:

    Nullable type uses '?' after type [OK]
Quick Trick: Add '?' after type to allow null values [OK]
Common Mistakes:
MISTAKES
  • Assigning null to non-nullable type
  • Using quotes around null
  • Incorrect nullable syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes