What does it mean when a collection is immutable in Kotlin?
An immutable collection cannot be changed after it is created. You cannot add, remove, or update its elements.
Click to reveal answer
beginner
Why are immutable collections the default in Kotlin?
Immutable collections help avoid bugs by preventing accidental changes. They make programs safer and easier to understand.
Click to reveal answer
intermediate
How do immutable collections improve thread safety?
Since immutable collections cannot change, multiple threads can read them without conflicts or the need for locks.
Click to reveal answer
beginner
What is a real-life example to understand immutable collections?
Think of a printed book. Once printed, the pages don’t change. This is like an immutable collection where data stays the same.
Click to reveal answer
beginner
How can you create a mutable collection if needed in Kotlin?
You can use functions like mutableListOf() or mutableMapOf() to create collections that can be changed.
Click to reveal answer
What happens if you try to add an element to an immutable list in Kotlin?
AYou get a compile-time error.
BThe element is added successfully.
CThe program crashes at runtime.
DThe list automatically becomes mutable.
✗ Incorrect
Immutable lists do not support adding elements, so Kotlin prevents this at compile time.
Why does Kotlin prefer immutable collections by default?
ATo save memory space.
BBecause mutable collections are slower.
CTo improve safety and reduce bugs.
DBecause mutable collections are deprecated.
✗ Incorrect
Immutable collections help avoid accidental changes, making code safer and easier to maintain.
Which of these is a mutable collection in Kotlin?
AlistOf()
BmutableListOf()
CsetOf()
DmapOf()
✗ Incorrect
mutableListOf() creates a list that can be changed after creation.
How do immutable collections help with multi-threading?
AThey automatically synchronize threads.
BThey slow down thread execution.
CThey prevent any thread from reading data.
DThey allow safe concurrent reads without locks.
✗ Incorrect
Immutable collections cannot change, so multiple threads can read them safely at the same time.
Which analogy best describes an immutable collection?
AA printed book that cannot be changed.
BA whiteboard you can erase and rewrite.
CA chalkboard that can be wiped clean.
DA notebook where you can add pages.
✗ Incorrect
A printed book is fixed and cannot be changed, just like an immutable collection.
Explain why Kotlin uses immutable collections as the default choice.
Think about how unchanging data helps avoid mistakes.
You got /4 concepts.
Describe a real-life example that helps you understand immutable collections.
Consider something you cannot change once made.
You got /3 concepts.
Practice
(1/5)
1. Why does Kotlin use immutable collections as the default choice?
easy
A. Because they prevent accidental changes and make code safer
B. Because they are faster to create than mutable collections
C. Because they allow adding elements after creation
D. Because they use less memory than mutable collections
Solution
Step 1: Understand immutability concept
Immutable collections cannot be changed after creation, preventing accidental modifications.
Step 2: Recognize safety benefits
This immutability makes programs safer and easier to understand by avoiding unexpected side effects.
Final Answer:
Because they prevent accidental changes and make code safer -> Option A
Quick Check:
Immutable collections = safer code [OK]
Hint: Immutable means no changes allowed after creation [OK]
Common Mistakes:
Thinking immutable collections allow adding elements
Assuming immutable collections are always faster
Confusing immutability with memory usage
2. Which of the following is the correct way to create an immutable list in Kotlin?
easy
A. val list = ArrayList(1, 2, 3)
B. val list = mutableListOf(1, 2, 3)
C. val list = arrayListOf(1, 2, 3)
D. val list = listOf(1, 2, 3)
Solution
Step 1: Identify immutable list creation function
In Kotlin, listOf() creates an immutable list.
Step 2: Differentiate from mutable list functions
mutableListOf(), arrayListOf(), and ArrayList() create mutable lists.
Final Answer:
val list = listOf(1, 2, 3) -> Option D
Quick Check:
Immutable list = listOf() [OK]
Hint: Use listOf() for immutable lists in Kotlin [OK]
Common Mistakes:
Using mutableListOf() when immutable is needed
Confusing arrayListOf() with immutable listOf()
Trying to use ArrayList constructor with elements directly
3. What will be the output of this Kotlin code?
val numbers = listOf(1, 2, 3)
numbers.add(4)
medium
A. List will contain [1, 2, 3, 4]
B. Compilation error: Unresolved reference 'add'
C. Runtime error: UnsupportedOperationException
D. List remains [1, 2, 3] without error
Solution
Step 1: Recognize listOf creates immutable list
The listOf() function returns an immutable list that does not support modification methods like add().
Step 2: Understand behavior of calling add() on immutable list
Calling add() on a List<T> causes a compilation error because List<T> has no add() method: Unresolved reference 'add'.
Final Answer:
Compilation error: Unresolved reference 'add' -> Option B
Quick Check:
Immutable list + add() = compile error [OK]
Hint: Immutable lists have no add() method: compile error [OK]
Common Mistakes:
Expecting runtime error instead of compile-time error
Assuming add() works on immutable lists
Thinking list remains unchanged silently
4. Identify the error in this Kotlin code snippet:
val set = setOf(1, 2, 3)
set.add(4)
medium
A. Runtime error: UnsupportedOperationException when calling add()
B. Syntax error: setOf cannot be used without mutable keyword
C. Compile-time error: add() is not a function of Set
D. No error, set will contain 4 elements
Solution
Step 1: Understand setOf creates immutable set
The setOf() function returns an immutable set that does not support modification.
Step 2: Analyze calling add() on immutable set
Calling add() on a Set<T> causes a compile-time error because Set<T> has no add() method.
Final Answer:
Compile-time error: add() is not a function of Set -> Option C
Quick Check:
Immutable set + add() = compile error [OK]
Hint: Immutable sets have no add() method: compile error [OK]
Common Mistakes:
Expecting runtime error instead of compile-time error
Assuming setOf creates mutable sets
Thinking add() silently fails without error
5. You want to create a map in Kotlin that cannot be changed after creation. Which approach correctly ensures immutability and prevents accidental modification?
hard
A. Use mapOf() to create an immutable map
B. Use mutableMapOf() and avoid calling modification methods
C. Create a mutable map and then assign it to a val variable
D. Use HashMap() and do not expose it outside the function
Solution
Step 1: Identify immutable map creation method
mapOf() creates an immutable map that cannot be changed after creation.
Step 2: Understand why other options fail
Using mutableMapOf() or HashMap() creates mutable maps; assigning to val only prevents reassignment, not modification.
Final Answer:
Use mapOf() to create an immutable map -> Option A
Quick Check:
Immutable map = mapOf() [OK]
Hint: Use mapOf() for truly immutable maps in Kotlin [OK]
Common Mistakes:
Thinking val prevents map modification
Using mutableMapOf() expecting immutability
Relying on scope to protect mutable maps
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.