0
0
Kotlinprogramming~10 mins

Why Flow matters for async sequences 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 create a simple Flow that emits numbers from 1 to 3.

Kotlin
val numbersFlow = flow {
    for (i in 1..[1]) {
        emit(i)
    }
}
Drag options to blanks, or click blank then click option'
A10
B3
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a range that is too large or too small.
Forgetting to use the range operator '..'.
2fill in blank
medium

Complete the code to collect values from the Flow and print them.

Kotlin
runBlocking {
    numbersFlow.[1] { value ->
        println(value)
    }
}
Drag options to blanks, or click blank then click option'
AflowOn
Blaunch
Cemit
Dcollect
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'launch' instead of 'collect' to receive values.
Trying to use 'emit' outside the flow builder.
3fill in blank
hard

Fix the error in the code to make the Flow emit values on the IO dispatcher.

Kotlin
val ioFlow = numbersFlow.[1](Dispatchers.IO)
Drag options to blanks, or click blank then click option'
AflowOn
Bemit
Ccollect
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'collect' or 'launch' instead of 'flowOn'.
Trying to use 'emit' outside the flow builder.
4fill in blank
hard

Fill both blanks to create a Flow that emits squares of numbers greater than 2.

Kotlin
val squaresFlow = flow {
    for (num in 1..5) {
        if (num [1] 2) {
            emit(num [2] num)
        }
    }
}
Drag options to blanks, or click blank then click option'
A>
B*
C<
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using '+' instead of '*' for squaring.
5fill in blank
hard

Fill all three blanks to create a Flow that emits uppercase strings longer than 3 characters.

Kotlin
val words = listOf("cat", "lion", "tiger", "dog")
val filteredFlow = flow {
    for (word in words) {
        if (word.length [1] 3) {
            emit(word.[2]().[3]())
        }
    }
}
Drag options to blanks, or click blank then click option'
A>
BtoUpperCase
Cuppercase
DtoLowerCase
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for length comparison.
Using 'toLowerCase' instead of uppercase functions.