0
0
Kotlinprogramming~10 mins

Mutable vs immutable interfaces in Kotlin - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an immutable list in Kotlin.

Kotlin
val numbers: List<Int> = [1](1, 2, 3)
Drag options to blanks, or click blank then click option'
AlistOf
BmutableListOf
CarrayListOf
DsetOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableListOf instead of listOf, which creates a mutable list.
2fill in blank
medium

Complete the code to add an element to a mutable list in Kotlin.

Kotlin
val numbers = mutableListOf(1, 2, 3)
numbers.[1](4)
Drag options to blanks, or click blank then click option'
Aset
Bremove
Cclear
Dadd
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'set' which replaces an element instead of adding.
Using 'remove' which deletes an element.
3fill in blank
hard

Fix the error in the code by choosing the correct interface to declare a mutable list.

Kotlin
val numbers: [1]<Int> = mutableListOf(1, 2, 3)
Drag options to blanks, or click blank then click option'
AMutableList
BSet
CList
DCollection
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'List' which is immutable and causes errors when modifying.
4fill in blank
hard

Fill both blanks to create an immutable map from a mutable map in Kotlin.

Kotlin
val mutableMap = mutableMapOf("a" to 1, "b" to 2)
val immutableMap: [1]<String, Int> = mutableMap
val readOnlyMap: [2]<String, Int> = immutableMap
Drag options to blanks, or click blank then click option'
AMap
BMutableMap
CHashMap
DLinkedHashMap
Attempts:
3 left
💡 Hint
Common Mistakes
Using MutableMap for immutableMap causes mutability.
5fill in blank
hard

Fill all three blanks to filter a mutable list and create an immutable list in Kotlin.

Kotlin
val mutableList = mutableListOf(1, 2, 3, 4, 5)
val filteredList: [1]<Int> = mutableList.[2] { it [3] 3 }
Drag options to blanks, or click blank then click option'
AList
Bfilter
C>
DMutableList
Attempts:
3 left
💡 Hint
Common Mistakes
Using MutableList as the type causes confusion about mutability.
Using '<' instead of '>' changes the filter condition.