Bird
0
0

Which of the following is the correct syntax to declare a mutable list that can hold null strings in Kotlin?

easy📝 Syntax Q3 of 15
Kotlin - Null Safety
Which of the following is the correct syntax to declare a mutable list that can hold null strings in Kotlin?
Aval list = mutableListOf<String>()?
Bval list = mutableListOf<String?>()
Cval list = mutableListOf<?String>()
Dval list = mutableListOf<String?>?()
Step-by-Step Solution
Solution:
  1. Step 1: Understand Kotlin syntax for nullable types

    To allow null elements, the element type must be nullable, written as String?.
  2. Step 2: Check correct mutable list declaration

    mutableListOf() correctly declares a mutable list that can hold null strings.
  3. Final Answer:

    val list = mutableListOf() -> Option B
  4. Quick Check:

    Nullable element type inside mutableListOf = correct syntax [OK]
Quick Trick: Put ? after element type, not after mutableListOf [OK]
Common Mistakes:
MISTAKES
  • Placing ? after mutableListOf instead of element type
  • Using invalid syntax like
  • Adding ? after the whole expression incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes