0
0
Kotlinprogramming~10 mins

Map creation (mapOf, mutableMapOf) in Kotlin - Interactive Code Practice

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

Complete the code to create an immutable map with two entries.

Kotlin
val colors = [1]("red" to "#FF0000", "green" to "#00FF00")
Drag options to blanks, or click blank then click option'
AmutableMapOf
BmapOf
ClistOf
DsetOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableMapOf creates a mutable map, not immutable.
Using listOf or setOf creates collections, not maps.
2fill in blank
medium

Complete the code to create a mutable map and add a new entry.

Kotlin
val fruits = [1]("apple" to 1, "banana" to 2)
fruits["orange"] = 3
Drag options to blanks, or click blank then click option'
AmutableMapOf
BmapOf
ClistOf
DsetOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mapOf creates an immutable map, so adding entries causes an error.
3fill in blank
hard

Fix the error in the code by choosing the correct function to create a mutable map.

Kotlin
val scores = [1]("Alice" to 10, "Bob" to 8)
scores["Charlie"] = 7
Drag options to blanks, or click blank then click option'
AmapOf
BsetOf
CmutableMapOf
DlistOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mapOf causes an error when adding new entries.
4fill in blank
hard

Fill both blanks to create a map of squares for numbers greater than 3.

Kotlin
val squares = (1..5).filter { it [2] 3 }.associate { it to it [1] it }
Drag options to blanks, or click blank then click option'
A**
B>
C<
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using ** for power in Kotlin is incorrect.
Using < instead of > filters the wrong numbers.
5fill in blank
hard

Fill all three blanks to create a mutable map with uppercase keys and values greater than 0.

Kotlin
val result = data.entries.filter { (_, v) -> v [3] 0 }.associate { (k, v) -> [1] to [2] }.toMutableMap()
Drag options to blanks, or click blank then click option'
Ak.uppercase()
Bv
C>
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > filters wrong entries.
Not transforming keys to uppercase.