Bird
0
0

Identify the error in this Kotlin code:

medium📝 Debug Q6 of 15
Kotlin - Variables and Type System
Identify the error in this Kotlin code:
val list = listOf(1, 2, 3)
list.add(4)
ACannot add to an immutable list
BVal variable cannot be reassigned
CMissing semicolon
Dlist is not initialized
Step-by-Step Solution
Solution:
  1. Step 1: Understand listOf returns immutable list

    listOf creates an immutable list that does not support add.
  2. Step 2: Calling add on immutable list

    Trying to call add(4) causes a compilation error.
  3. Final Answer:

    Cannot add to an immutable list -> Option A
  4. Quick Check:

    Immutable list disallows add() [OK]
Quick Trick: listOf creates immutable list, no add() allowed [OK]
Common Mistakes:
MISTAKES
  • Confusing val with mutability
  • Expecting add() to work
  • Ignoring immutable collection types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes