0
0
Kotlinprogramming~10 mins

Map transformation 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 a new map with values doubled.

Kotlin
val numbers = mapOf("a" to 1, "b" to 2, "c" to 3)
val doubled = numbers.mapValues { it.value [1] 2 }
println(doubled)
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' adds 2 instead of doubling.
Using '-' or '/' changes values incorrectly.
2fill in blank
medium

Complete the code to filter map entries with values greater than 2.

Kotlin
val numbers = mapOf("a" to 1, "b" to 3, "c" to 4)
val filtered = numbers.filter { it.value [1] 2 }
println(filtered)
Drag options to blanks, or click blank then click option'
A<
B<=
C==
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters smaller values.
Using '==' filters only values equal to 2.
3fill in blank
hard

Fix the error in the code to convert map keys to uppercase.

Kotlin
val original = mapOf("one" to 1, "two" to 2)
val upperKeys = original.mapKeys { it.key.[1]() }
println(upperKeys)
Drag options to blanks, or click blank then click option'
AtoLowerCase
Buppercase
CtoUpperCase
Dcapitalize
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'toUpperCase()' is deprecated in modern Kotlin.
Using 'toLowerCase()' changes to lowercase, not uppercase.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words longer than 3 letters.

Kotlin
val words = listOf("cat", "house", "tree", "a")
val lengths = words.filter { it.length [1] 3 }.associateWith { it.[2] }
println(lengths)
Drag options to blanks, or click blank then click option'
A>
Blength
C<
Dfirst
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters shorter words.
Using 'first' returns first character, not length.
5fill in blank
hard

Fill all three blanks to create a map with uppercase keys and values doubled for values > 1.

Kotlin
val data = mapOf("x" to 1, "y" to 2, "z" to 3)
val result = data.filter { it.value [1] 1 }.mapKeys { it.key.[2]() }.mapValues { it.value [3] 2 }
println(result)
Drag options to blanks, or click blank then click option'
A<
B>
C*
Duppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters smaller values.
Using 'toUpperCase()' instead of 'uppercase()' in modern Kotlin.