Bird
0
0

Which of the following is the correct syntax to declare an immutable list of integers in Kotlin?

easy📝 Syntax Q3 of 15
Kotlin - Collections Fundamentals
Which of the following is the correct syntax to declare an immutable list of integers in Kotlin?
Aval numbers: MutableList<Int> = listOf(1, 2, 3)
Bval numbers: List<Int> = listOf(1, 2, 3)
Cval numbers: List<Int> = mutableListOf(1, 2, 3)
Dval numbers: MutableList<Int> = mutableListOf(1, 2, 3)
Step-by-Step Solution
Solution:
  1. Step 1: Identify immutable list declaration

    listOf() returns an immutable List, so the type should be List.
  2. Step 2: Match type and function

    MutableList requires mutableListOf(), so val numbers: List = listOf(1, 2, 3) correctly pairs List with listOf().
  3. Final Answer:

    val numbers: List = listOf(1, 2, 3) -> Option B
  4. Quick Check:

    Immutable list syntax = val List = listOf() [OK]
Quick Trick: Use listOf() with List type for immutable lists [OK]
Common Mistakes:
MISTAKES
  • Using mutableListOf() with List type
  • Declaring MutableList with listOf()
  • Mixing mutable and immutable types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes