0
0
Kotlinprogramming~10 mins

Why collection operations replace loops 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 filter a list of numbers to keep only even numbers.

Kotlin
val evens = numbers.[1] { it % 2 == 0 }
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Creduce
DforEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of filter returns transformed elements, not filtered ones.
2fill in blank
medium

Complete the code to transform a list of strings to their lengths.

Kotlin
val lengths = words.[1] { it.length }
Drag options to blanks, or click blank then click option'
Afilter
BforEach
Cfold
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter returns a subset, not transformed elements.
3fill in blank
hard

Fix the error in the code to sum all numbers in the list.

Kotlin
val total = numbers.[1] { acc, num -> acc + num }
Drag options to blanks, or click blank then click option'
AforEach
Bmap
Creduce
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using map returns a list, not a single combined value.
4fill in blank
hard

Fill both blanks to create a map of words to their uppercase forms, filtering only words longer than 3 letters.

Kotlin
val result = words.[1] { it.length > 3 }.[2] { it to it.uppercase() }
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Creduce
DforEach
Attempts:
3 left
💡 Hint
Common Mistakes
Using map before filter changes all elements before filtering.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, filtering words starting with 'a'.

Kotlin
val result = words.[1] { it.startsWith("a") }.[2] { it.uppercase() }.[3] { it to it.length }
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Cassociate
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using reduce instead of associate does not create a map.