Bird
Raised Fist0
Kotlinprogramming~10 mins

Mutable vs immutable interfaces in Kotlin - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - Mutable vs immutable interfaces
Start with collection reference
↓
Is interface mutable?
No→Immutable: no change allowed
Yes↓
Mutable: can add, remove, update
↓
Use collection methods accordingly
↓
End
This flow shows how a collection reference can be either mutable or immutable, affecting what operations are allowed.
Execution Sample
Kotlin
val list: List<Int> = listOf(1, 2, 3)
val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)

// list.add(4) // Error
mutableList.add(4)
println(mutableList)
This code shows an immutable list that cannot be changed and a mutable list that can be updated.
Execution Table
StepCode LineActionResultOutput
1val list: List<Int> = listOf(1, 2, 3)Create immutable listlist = [1, 2, 3]
2val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)Create mutable listmutableList = [1, 2, 3]
3// list.add(4) // ErrorAttempt to add to immutable listCompile error: Unresolved reference: add
4mutableList.add(4)Add 4 to mutableListmutableList = [1, 2, 3, 4]
5println(mutableList)Print mutableList[1, 2, 3, 4]
💡 Execution stops after printing the updated mutable list; adding to immutable list is not allowed.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
listundefined[1, 2, 3][1, 2, 3][1, 2, 3]
mutableListundefined[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 2 Insights
Why can't we add an element to 'list'?
'list' is declared as List<Int>, which is immutable. The execution_table row 3 shows a compile error when trying to add.
What happens when we add an element to 'mutableList'?
Since 'mutableList' is MutableList<Int>, it allows adding elements. Row 4 shows the list updated with the new element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what happens when trying to add to 'list'?
ACompile error occurs
BThe element is added successfully
CNothing happens, code skips
DRuntime exception occurs
💡 Hint
Check execution_table row 3 under 'Result' for the error message.
At which step does 'mutableList' change its contents?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at variable_tracker for 'mutableList' changes after each step.
If 'list' was declared as MutableList<Int>, what would happen at step 3?
ACompile error still occurs
BRuntime exception occurs
CElement would be added successfully
DCode would skip adding
💡 Hint
Refer to the difference between List and MutableList in the concept_flow and execution_table.
Concept Snapshot
Mutable vs Immutable Interfaces in Kotlin:
- List<T> is immutable: no add/remove/update allowed.
- MutableList<T> is mutable: supports add, remove, update.
- Immutable interfaces prevent accidental changes.
- Use mutable interfaces when you need to change collections.
- Trying to modify immutable collections causes compile errors.
Full Transcript
This lesson shows the difference between mutable and immutable interfaces in Kotlin using lists. An immutable list (List<Int>) cannot be changed after creation, so trying to add elements causes a compile error. A mutable list (MutableList<Int>) allows adding, removing, and updating elements. The execution table traces each step: creating lists, attempting to add to immutable list (which fails), adding to mutable list (which succeeds), and printing the result. The variable tracker shows how the lists' contents change over time. Key moments clarify why immutable lists cannot be changed and how mutable lists work. The quiz tests understanding of these concepts by referencing the execution steps. Remember: use immutable interfaces for safety and mutable interfaces when changes are needed.

Practice

(1/5)
1. Which statement best describes the difference between mutable and immutable interfaces in Kotlin?
easy
A. Immutable interfaces are used only for primitive types.
B. Mutable interfaces only allow reading data, immutable interfaces allow modification.
C. Both mutable and immutable interfaces allow data modification.
D. Immutable interfaces allow only reading data, while mutable interfaces allow data modification.

Solution

  1. Step 1: Understand interface roles

    Immutable interfaces provide read-only access to data, preventing changes.
  2. Step 2: Compare mutable interfaces

    Mutable interfaces extend immutable ones and allow data to be changed.
  3. Final Answer:

    Immutable interfaces allow only reading data, while mutable interfaces allow data modification. -> Option D
  4. Quick Check:

    Immutable = read-only, Mutable = read-write [OK]
Hint: Immutable = read-only, mutable = can change data [OK]
Common Mistakes:
  • Confusing which interface allows modification
  • Thinking both interfaces allow changes
  • Assuming immutable interfaces are only for primitives
2. Which of the following is the correct way to declare a mutable list interface in Kotlin?
easy
A. val list: MutableList<Int> = listOf(1, 2, 3)
B. val list: List<Int> = mutableListOf(1, 2, 3)
C. val list: MutableList<Int> = mutableListOf(1, 2, 3)
D. val list: List<Int> = listOf(1, 2, 3)

Solution

  1. Step 1: Identify mutable list declaration

    MutableList is the interface for mutable lists, and mutableListOf() creates a mutable list instance.
  2. Step 2: Check each option

    val list: MutableList<Int> = mutableListOf(1, 2, 3) correctly declares a MutableList and initializes it with mutableListOf().
  3. Final Answer:

    val list: MutableList<Int> = mutableListOf(1, 2, 3) -> Option C
  4. Quick Check:

    MutableList + mutableListOf() = mutable list [OK]
Hint: Use MutableList with mutableListOf() for mutable lists [OK]
Common Mistakes:
  • Using listOf() with MutableList type
  • Declaring mutable list as List type
  • Mixing mutable and immutable functions
3. What will be the output of the following Kotlin code?
val immutableList: List<Int> = listOf(1, 2, 3)
val mutableList: MutableList<Int> = mutableListOf(4, 5, 6)

println(immutableList.size)
mutableList.add(7)
println(mutableList)
medium
A. 3 [4, 5, 6, 7]
B. 3 [4, 5, 6]
C. 4 [4, 5, 6, 7]
D. Compilation error

Solution

  1. Step 1: Check immutable list size

    immutableList has 3 elements, so size is 3.
  2. Step 2: Add element to mutable list and print

    mutableList adds 7, so it becomes [4, 5, 6, 7].
  3. Final Answer:

    3 [4, 5, 6, 7] -> Option A
  4. Quick Check:

    Immutable size 3, mutable list adds 7 [OK]
Hint: Immutable size fixed; mutable can add elements [OK]
Common Mistakes:
  • Trying to add to immutable list causing error
  • Expecting immutable list size to change
  • Confusing output format
4. Identify the error in this Kotlin code snippet:
val numbers: List<Int> = listOf(1, 2, 3)
numbers.add(4)
medium
A. Cannot add to a List because it is immutable
B. Syntax error in listOf() usage
C. No error, code runs fine
D. Variable numbers should be declared as var

Solution

  1. Step 1: Understand List immutability

    List interface in Kotlin is immutable; it does not support add() method.
  2. Step 2: Analyze the error

    Calling add(4) on immutable List causes a compilation error.
  3. Final Answer:

    Cannot add to a List because it is immutable -> Option A
  4. Quick Check:

    Immutable List blocks add() method [OK]
Hint: List is immutable; use MutableList to add elements [OK]
Common Mistakes:
  • Trying to add to immutable List
  • Assuming var allows modification of immutable List
  • Confusing syntax error with immutability error
5. You have a function that accepts a List<String> parameter but you want to modify the list inside the function. Which is the best approach to handle this in Kotlin?
hard
A. Change the parameter type to MutableList<String> and pass a mutable list.
B. Create a new mutable copy of the list inside the function and modify that.
C. Use val keyword inside the function to modify the list.
D. Keep the parameter as List<String> and cast it to MutableList inside the function.

Solution

  1. Step 1: Understand parameter immutability

    List<String> is immutable, so it cannot be modified directly.
  2. Step 2: Evaluate safe modification methods

    Casting to MutableList is unsafe and may cause runtime errors; changing parameter type affects API design.
  3. Step 3: Use safe copy for modification

    Creating a mutable copy inside the function allows safe modifications without affecting original list.
  4. Final Answer:

    Create a new mutable copy of the list inside the function and modify that. -> Option B
  5. Quick Check:

    Copy immutable list to mutable for safe changes [OK]
Hint: Copy immutable list to mutable before modifying [OK]
Common Mistakes:
  • Casting immutable list to mutable unsafely
  • Changing parameter type breaking API contracts
  • Assuming val allows modification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard