0
0
Kotlinprogramming~10 mins

Flow operators (map, filter, transform) 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 transform each number by doubling it using the map operator.

Kotlin
val numbers = flowOf(1, 2, 3)
val doubled = numbers.[1] { it * 2 }
Drag options to blanks, or click blank then click option'
Afilter
Bcollect
Ctransform
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter instead of map will only select elements, not transform them.
Using collect is for consuming the flow, not transforming it.
2fill in blank
medium

Complete the code to keep only even numbers using the filter operator.

Kotlin
val numbers = flowOf(1, 2, 3, 4)
val evens = numbers.[1] { it % 2 == 0 }
Drag options to blanks, or click blank then click option'
Amap
Bfilter
Ctransform
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using map will transform elements but not filter them.
Using collect is for consuming the flow, not filtering.
3fill in blank
hard

Fix the error in the code to emit both original and doubled values using transform.

Kotlin
val numbers = flowOf(1, 2)
val result = numbers.[1] { emit(it); emit(it * 2) }
Drag options to blanks, or click blank then click option'
Atransform
Bfilter
Ccollect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map only emits one value per input.
Using filter does not emit new values, only filters existing ones.
4fill in blank
hard

Fill both blanks to create a flow of squares of odd numbers.

Kotlin
val numbers = flowOf(1, 2, 3, 4, 5)
val squaresOfOdds = numbers.[1] { it % 2 != 0 }.[2] { it * it }
Drag options to blanks, or click blank then click option'
Afilter
Bmap
Ctransform
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using map before filter will square all numbers before filtering.
Using transform instead of map for simple transformation is more complex.
5fill in blank
hard

Fill all three blanks to emit strings for numbers greater than 2, doubled and converted to string.

Kotlin
val numbers = flowOf(1, 2, 3, 4)
val result = numbers.[1] { it [2] 2 }.[3] { (it * 2).toString() }
Drag options to blanks, or click blank then click option'
Afilter
B>
Cmap
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' will select wrong numbers.
Using transform instead of map for simple transformation is unnecessary.