Recall & Review
beginner
What is an immutable interface in Kotlin?
An immutable interface provides read-only access to data. It does not allow changes to the data it exposes.
Click to reveal answer
beginner
What does a mutable interface allow you to do?
A mutable interface allows you to change or update the data it exposes, such as adding, removing, or modifying elements.
Click to reveal answer
beginner
Give an example of a Kotlin immutable interface.
List<T> is an immutable interface in Kotlin. It allows reading elements but not changing the list.
Click to reveal answer
beginner
Give an example of a Kotlin mutable interface.
MutableList<T> is a mutable interface in Kotlin. It allows adding, removing, and updating elements in the list.
Click to reveal answer
intermediate
Why use immutable interfaces when mutable ones exist?
Immutable interfaces help prevent accidental changes, making code safer and easier to understand. They are good for sharing data without risk of modification.
Click to reveal answer
Which Kotlin interface allows modifying the collection?
✗ Incorrect
MutableList allows adding, removing, and updating elements, while List is read-only.
What happens if you try to add an element to a List in Kotlin?
✗ Incorrect
List is immutable and does not have methods to add elements, so trying to add causes a compilation error.
Why prefer immutable interfaces in Kotlin?
✗ Incorrect
Immutable interfaces prevent accidental data changes, making code safer and easier to maintain.
Which interface would you use to change elements in a Kotlin list?
✗ Incorrect
MutableList supports modification methods like add, remove, and set.
What is a key difference between MutableList and List in Kotlin?
✗ Incorrect
MutableList allows changes; List only allows reading.
Explain the difference between mutable and immutable interfaces in Kotlin.
Think about whether you can change the data or only read it.
You got /3 concepts.
Why might you choose to use an immutable interface over a mutable one?
Consider safety and clarity in your code.
You got /3 concepts.