📖 Scenario: Imagine you are building a simple contact list app. You want to keep a list of contacts where some parts of your program can only read the contacts, while other parts can add or remove contacts.
🎯 Goal: You will create a Kotlin program that shows the difference between mutable and immutable interfaces by using List and MutableList. You will start by creating a list of contacts, then add a helper variable, use the mutable interface to add a contact, and finally print the updated list.
📋 What You'll Learn
Create an immutable list of contacts using List
Create a mutable list variable from the immutable list
Add a new contact to the mutable list
Print the final list of contacts
💡 Why This Matters
🌍 Real World
Many apps need to protect data from accidental changes by using immutable interfaces, while allowing controlled changes with mutable interfaces.
💼 Career
Understanding mutable vs immutable collections is important for writing safe and clear Kotlin code in Android development and backend services.
Progress0 / 4 steps
1
Create an immutable list of contacts
Create a variable called contacts that holds an immutable list of strings with these exact values: "Alice", "Bob", "Charlie" using listOf().
Kotlin
Hint
Use val contacts: List = listOf("Alice", "Bob", "Charlie") to create an immutable list.
2
Create a mutable list from the immutable list
Create a variable called mutableContacts that holds a mutable list of strings by copying the contacts list using toMutableList().
Kotlin
Hint
Use toMutableList() on contacts to create a mutable copy.
3
Add a new contact to the mutable list
Add the string "Diana" to the mutableContacts list using the add() method.
Kotlin
Hint
Use mutableContacts.add("Diana") to add the new contact.
4
Print the updated list of contacts
Print the mutableContacts list using println() to show all contacts including the new one.
Kotlin
Hint
Use println(mutableContacts) to display the list.
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
Step 1: Understand interface roles
Immutable interfaces provide read-only access to data, preventing changes.
Step 2: Compare mutable interfaces
Mutable interfaces extend immutable ones and allow data to be changed.
Final Answer:
Immutable interfaces allow only reading data, while mutable interfaces allow data modification. -> Option D
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
Step 1: Identify mutable list declaration
MutableList is the interface for mutable lists, and mutableListOf() creates a mutable list instance.
Step 2: Check each option
val list: MutableList<Int> = mutableListOf(1, 2, 3) correctly declares a MutableList and initializes it with mutableListOf().
Final Answer:
val list: MutableList<Int> = mutableListOf(1, 2, 3) -> Option C
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
Step 1: Check immutable list size
immutableList has 3 elements, so size is 3.
Step 2: Add element to mutable list and print
mutableList adds 7, so it becomes [4, 5, 6, 7].
Final Answer:
3
[4, 5, 6, 7] -> Option A
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
Step 1: Understand List immutability
List interface in Kotlin is immutable; it does not support add() method.
Step 2: Analyze the error
Calling add(4) on immutable List causes a compilation error.
Final Answer:
Cannot add to a List because it is immutable -> Option A
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
Step 1: Understand parameter immutability
List<String> is immutable, so it cannot be modified directly.
Step 2: Evaluate safe modification methods
Casting to MutableList is unsafe and may cause runtime errors; changing parameter type affects API design.
Step 3: Use safe copy for modification
Creating a mutable copy inside the function allows safe modifications without affecting original list.
Final Answer:
Create a new mutable copy of the list inside the function and modify that. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.