Bird
0
0

You want to create a list that you can modify but keep the reference fixed. Which declaration is correct?

hard📝 Application Q8 of 15
Kotlin - Variables and Type System
You want to create a list that you can modify but keep the reference fixed. Which declaration is correct?
Avar list = mutableListOf(1, 2, 3)
Bvar list = listOf(1, 2, 3)
Cval list = listOf(1, 2, 3)
Dval list = mutableListOf(1, 2, 3)
Step-by-Step Solution
Solution:
  1. Step 1: Fix reference with val

    Use val to keep the reference immutable.
  2. Step 2: Use mutable list for modifiable content

    mutableListOf creates a list that can be changed.
  3. Final Answer:

    val list = mutableListOf(1, 2, 3) -> Option D
  4. Quick Check:

    val + mutableListOf = fixed reference, mutable content [OK]
Quick Trick: val fixes reference, mutableListOf allows changes [OK]
Common Mistakes:
MISTAKES
  • Using var instead of val
  • Using immutable list for modification
  • Confusing list mutability and reference mutability

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes