A Flow is a type that can emit multiple values sequentially, like a stream of data that you can collect over time.
Click to reveal answer
beginner
How do you create a simple Flow using the flow builder?
You use the flow { } builder and emit values inside it using emit(value).
Click to reveal answer
beginner
What does the collect function do in Kotlin Flows?
collect gathers the emitted values from a Flow and lets you handle each value as it arrives.
Click to reveal answer
intermediate
Why is collect a suspending function?
Because collecting values from a Flow can take time, collect suspends the coroutine until all values are received or the flow completes.
Click to reveal answer
beginner
Show a simple example of a Flow that emits numbers 1 to 3 and collects them.
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun simpleFlow() = flow {
for (i in 1..3) {
emit(i)
}
}
fun main() = runBlocking {
simpleFlow().collect { value ->
println("Received $value")
}
}
Click to reveal answer
What keyword is used to create a Flow in Kotlin?
Aemit
Basync
Cflow
Dlaunch
✗ Incorrect
The flow builder is used to create a Flow in Kotlin.
Which function do you use to receive values from a Flow?
Acollect
Bemit
Claunch
DrunBlocking
✗ Incorrect
collect is used to receive and handle values emitted by a Flow.
Why must collect be called from a coroutine or suspending function?
ABecause it is a suspending function
BBecause it blocks the thread
CBecause it returns immediately
DBecause it is a constructor
✗ Incorrect
collect is suspending, so it must be called from a coroutine or another suspending function.
What does emit do inside a flow builder?
AStarts a coroutine
BStops the Flow
CCollects values
DSends a value to the Flow
✗ Incorrect
emit sends a value to the Flow to be collected later.
Which of these is true about Kotlin Flows?
AThey are synchronous streams
BThey can emit multiple values asynchronously
CThey emit only one value
DThey do not support coroutines
✗ Incorrect
Flows can emit multiple values asynchronously over time.
Explain how to create and collect values from a Kotlin Flow using the flow builder and collect function.
Think about how you start a flow, send values, and then receive them.
You got /5 concepts.
Describe why collecting from a Flow requires a coroutine or suspending function.
Consider what happens when data arrives over time.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of the flow {} builder in Kotlin?
easy
A. To create a stream of values that can be emitted over time
B. To immediately execute a block of code and return a single value
C. To collect values from a list synchronously
D. To define a suspend function without any emissions
Solution
Step 1: Understand the role of flow {}
The flow {} builder creates a stream that can emit multiple values asynchronously over time.
Step 2: Compare with other options
Options B, C, and D describe synchronous or non-stream behaviors, which do not match flow {} usage.
Final Answer:
To create a stream of values that can be emitted over time -> Option A
Quick Check:
Flow builder = stream of values [OK]
Hint: Remember: flow builds streams, not single immediate values [OK]
Common Mistakes:
Confusing flow with suspend functions returning single values
Thinking flow collects values instead of emitting them
Assuming flow is synchronous
2. Which of the following is the correct syntax to collect values from a flow named myFlow inside a suspend function?
easy
A. myFlow.collect(value -> println(value))
B. myFlow.collect { println(value) }
C. myFlow.collect { value -> println(value) }
D. myFlow.collect { value => println(value) }
Solution
Step 1: Identify correct lambda syntax in Kotlin
Kotlin uses curly braces with parameter and arrow: { param -> action }.
Step 2: Match syntax with options
myFlow.collect { value -> println(value) } correctly uses { value -> println(value) }. Options A lacks braces, B misses parameter declaration (value undefined), D uses wrong arrow symbol (=>).
Final Answer:
myFlow.collect { value -> println(value) } -> Option C
D. Missing suspend modifier on the function calling collect
Solution
Step 1: Check flow and collect usage
The flow builder and emit calls are correct.
Step 2: Understand collect context
Collect is a suspend function and must be called from a suspend function or coroutine scope. Calling it directly without suspend context causes error.
Final Answer:
Missing suspend modifier on the function calling collect -> Option D
Quick Check:
collect requires suspend context [OK]
Hint: Remember: collect is suspend, needs suspend function or coroutine [OK]
Common Mistakes:
Trying to call collect outside suspend or coroutine
Confusing emit usage inside flow
Thinking collect syntax is wrong
5. You want to create a flow that emits only even numbers from 1 to 5 and then collect and print them. Which code snippet correctly achieves this?
hard
A. val flow = flow {
for (i in 1..5) {
if (i % 2 == 0) emit(i)
}
}
suspend fun main() {
flow.collect { println(it) }
}
B. val flow = flowOf(1, 2, 3, 4, 5).filter { it % 2 == 0 }
fun main() {
flow.collect { println(it) }
}
C. val flow = flow {
emit(2)
emit(4)
}
flow.collect { println(it) }
D. val flow = flow {
for (i in 1..5) emit(i)
}
flow.collect { if (it % 2 == 0) println(it) }
Solution
Step 1: Check flow emission of even numbers
val flow = flow {
for (i in 1..5) {
if (i % 2 == 0) emit(i)
}
}
suspend fun main() {
flow.collect { println(it) }
} emits only even numbers inside the flow builder using a for loop and condition.
Step 2: Verify collection and printing
val flow = flow {
for (i in 1..5) {
if (i % 2 == 0) emit(i)
}
}
suspend fun main() {
flow.collect { println(it) }
} collects the flow inside a suspend function and prints each emitted value correctly.
Final Answer:
val flow = flow {
for (i in 1..5) {
if (i % 2 == 0) emit(i)
}
}
suspend fun main() {
flow.collect { println(it) }
} -> Option A
Quick Check:
Emit filtered values inside flow, collect in suspend [OK]
Hint: Filter inside flow builder to emit only needed values [OK]
Common Mistakes:
Calling collect outside suspend function
Filtering after emitting all values instead of inside flow
Not using suspend function for collect
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.