Bird
0
0

How do you declare a variable of type String that can hold null values in Kotlin?

easy📝 Syntax Q3 of 15
Kotlin - Null Safety
How do you declare a variable of type String that can hold null values in Kotlin?
Aval name: String!
Bval name: String
Cval name: Nullable<String>
Dval name: String?
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin's nullable syntax

    Kotlin uses a question mark (?) after the type to indicate nullability.
  2. Step 2: Identify correct declaration

    Only val name: String? allows the variable to hold null values.
  3. Final Answer:

    val name: String? -> Option D
  4. Quick Check:

    Nullable types require '?' [OK]
Quick Trick: Use '?' after type for nullable variables [OK]
Common Mistakes:
MISTAKES
  • Using non-nullable type without '?'
  • Using 'Nullable' which is invalid
  • Using 'String!' which is not Kotlin syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes