0
0
Kotlinprogramming~10 mins

Flow builder and collect 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 flow that emits numbers from 1 to 3.

Kotlin
val numberFlow = flow {
    emit([1])
    emit(2)
    emit(3)
}
Drag options to blanks, or click blank then click option'
A0
B5
C4
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of 1 as the first emitted value.
Using a number outside the range 1 to 3.
2fill in blank
medium

Complete the code to collect and print each value from the flow.

Kotlin
runBlocking {
    numberFlow.[1] { value ->
        println(value)
    }
}
Drag options to blanks, or click blank then click option'
Aemit
Blaunch
Ccollect
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'emit' instead of 'collect'.
Using 'launch' which starts a coroutine but does not collect flow values.
3fill in blank
hard

Fix the error in the flow builder by completing the code to emit squares of numbers from 1 to 3.

Kotlin
val squareFlow = flow {
    for (i in 1..3) {
        emit(i [1] i)
    }
}
Drag options to blanks, or click blank then click option'
A*
B+
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using division or subtraction which do not calculate squares.
4fill in blank
hard

Fill both blanks to create a flow that emits even numbers from 1 to 6.

Kotlin
val evenFlow = flow {
    for (num in 1..6) {
        if (num [1] 2 == 0) {
            emit(num[2]0)
        }
    }
}
Drag options to blanks, or click blank then click option'
A%
B*
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or subtraction in the condition instead of modulo.
Modifying the number when emitting instead of emitting it as is.
5fill in blank
hard

Fill all three blanks to create a flow that emits uppercase strings from a list and collects them.

Kotlin
val words = listOf("apple", "banana", "cherry")

val upperFlow = flow {
    for (word in words) {
        emit(word[1])
    }
}

runBlocking {
    upperFlow.[2] { value ->
        println(value[3])
    }
}
Drag options to blanks, or click blank then click option'
A.uppercase()
Bcollect
C.toUpperCase()
D.lowercase()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase conversion instead of uppercase.
Using 'emit' instead of 'collect' to gather values.