Bird
0
0

Which of the following Kotlin statements correctly creates a mutable list of strings containing "red", "green", and "blue"?

easy📝 Syntax Q3 of 15
Kotlin - Collections Fundamentals
Which of the following Kotlin statements correctly creates a mutable list of strings containing "red", "green", and "blue"?
Aval colors = arrayListOf("red", "green", "blue")
Bval colors = listOf("red", "green", "blue")
Cval colors = mutableListOf<String>("red", "green", "blue")
Dval colors = mutableListOf("red", "green", "blue")
Step-by-Step Solution
Solution:
  1. Step 1: Identify mutable list creation

    mutableListOf() creates a mutable list. val colors = mutableListOf("red", "green", "blue") uses this correctly with string literals.
  2. Step 2: Check other options

    val colors = listOf("red", "green", "blue") creates an immutable list. val colors = mutableListOf("red", "green", "blue") is syntactically valid but unnecessary to specify type explicitly here. val colors = arrayListOf("red", "green", "blue") creates an ArrayList, which is mutable but not the asked function.
  3. Final Answer:

    val colors = mutableListOf("red", "green", "blue") -> Option D
  4. Quick Check:

    mutableListOf() with values creates mutable list [OK]
Quick Trick: Use mutableListOf() to create mutable lists with initial values [OK]
Common Mistakes:
MISTAKES
  • Using listOf() which creates immutable list
  • Unnecessarily specifying generic type
  • Confusing arrayListOf() with mutableListOf()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes