0
0
Kotlinprogramming~10 mins

Why immutable collections are default in Kotlin - Test Your Understanding

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

Complete the code to create an immutable list in Kotlin.

Kotlin
val numbers = listOf[1](1, 2, 3, 4, 5)
Drag options to blanks, or click blank then click option'
A<Any>
B<Int>
C(nothing)
D<Number>
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableListOf instead of listOf
Adding parentheses incorrectly
2fill in blank
medium

Complete the code to try adding an element to an immutable list and see the error.

Kotlin
val fruits = listOf("Apple", "Banana")
fruits.[1]("Cherry")
Drag options to blanks, or click blank then click option'
Aadd
Bappend
Cpush
Dinsert
Attempts:
3 left
💡 Hint
Common Mistakes
Using methods like append or push which don't exist in Kotlin lists
Trying to modify an immutable list directly
3fill in blank
hard

Fix the error by choosing the correct mutable collection type to allow adding elements.

Kotlin
val fruits = [1]("Apple", "Banana")
fruits.add("Cherry")
Drag options to blanks, or click blank then click option'
AmutableListOf
BlistOf
CsetOf
DmapOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using listOf which is immutable
Using setOf or mapOf which are different collection types
4fill in blank
hard

Fill both blanks to create an immutable map and access a value safely.

Kotlin
val capitals = [1]("France" to "Paris", "Japan" to "Tokyo")
val capital = capitals.[2]("France")
Drag options to blanks, or click blank then click option'
AmapOf
Bget
CmutableMapOf
Dput
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableMapOf which creates a mutable map
Using put which adds or updates entries, not retrieves
5fill in blank
hard

Fill all three blanks to filter an immutable list and create a new list with only even numbers.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evens = numbers.[1] { it [2] 2 [3] 0 }
Drag options to blanks, or click blank then click option'
Afilter
B%
C==
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of filter which transforms elements
Using incorrect comparison operators