0
0
Kotlinprogramming~10 mins

Combining flows (zip, combine) 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 zip two flows and collect their combined values.

Kotlin
val flow1 = flowOf(1, 2, 3)
val flow2 = flowOf("A", "B", "C")

flow1.[1](flow2) { a, b -> "$a$b" }
    .collect { println(it) }
Drag options to blanks, or click blank then click option'
Azip
Bcombine
Cmap
DflatMapConcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'combine' instead of 'zip' which merges latest values instead of pairing.
Using 'map' which transforms elements of a single flow.
2fill in blank
medium

Complete the code to combine two flows and collect their latest values.

Kotlin
val flow1 = flowOf(1, 2, 3)
val flow2 = flowOf("X", "Y", "Z")

flow1.[1](flow2) { a, b -> "$a$b" }
    .collect { println(it) }
Drag options to blanks, or click blank then click option'
Azip
Bcombine
Cmap
DflatMapLatest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'zip' which pairs elements one by one instead of combining latest values.
Using 'map' which only transforms a single flow.
3fill in blank
hard

Fix the error in the code to correctly zip two flows and print combined results.

Kotlin
val numbers = flowOf(1, 2, 3)
val letters = flowOf("a", "b", "c")

numbers.[1](letters) { num, letter -> "$num$letter" }
    .collect { println(it) }
Drag options to blanks, or click blank then click option'
Azip
Bcombine
Cmap
DflatMapConcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'combine' which merges latest values instead of pairing.
Using 'map' which only works on a single flow.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 characters.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")

val lengths = words.associateWith { [1] }
    .filter { it.value [2] 3 }
Drag options to blanks, or click blank then click option'
Ait.length
Bit.size
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'it.size' which is not a property of String.
Using '<' instead of '>' causing wrong filtering.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths for words longer than 4 characters.

Kotlin
val words = listOf("pear", "plum", "banana", "kiwi")

val result = words.filter { it.length [1] 4 }
    .associateBy(
        keySelector = { it.[2]() },
        valueTransform = { it.[3] }
    )
Drag options to blanks, or click blank then click option'
A>
Blength
Cuppercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in filter.
Using 'uppercase' property instead of function call.
Using 'size' instead of 'length' for string length.