A Flow is a type that can emit multiple values sequentially, asynchronously. It is like a stream of data that you can collect over time.
Click to reveal answer
beginner
How do you collect values from a Flow?
You use the collect terminal operator inside a coroutine to receive emitted values from the Flow.
Click to reveal answer
intermediate
What is the difference between Flow and suspend functions?
suspend functions return a single value asynchronously, while Flow can emit multiple values over time.
Click to reveal answer
beginner
What does the flow { } builder do?
It creates a Flow by defining a block where you can emit values using emit().
Click to reveal answer
beginner
Why should you collect a Flow inside a coroutine?
Because Flow is asynchronous and suspending, collecting it requires a coroutine context to handle the asynchronous stream properly.
Click to reveal answer
Which Kotlin type represents a stream of multiple asynchronous values?
AFlow
Bsuspend function
CList
DDeferred
✗ Incorrect
Flow is designed to emit multiple values asynchronously over time.
How do you receive values emitted by a Flow?
AUsing collect inside a coroutine
BCalling the Flow directly
CUsing a for loop
DUsing suspend functions only
✗ Incorrect
You must call collect inside a coroutine to receive Flow emissions.
What keyword is used to create a Flow builder?
Aemit
Basync
Cflow
Dlaunch
✗ Incorrect
The flow builder is created with the keyword flow { }.
Which operator is used inside a flow builder to send values?
Areturn
Bsend
Ccollect
Demit
✗ Incorrect
emit() is used inside flow { } to emit values.
What is the main difference between a suspend function and a Flow?
ASuspend functions are faster than Flow
BSuspend functions return one value; Flow emits many
CSuspend functions are synchronous; Flow is asynchronous
DFlow cannot be collected
✗ Incorrect
Suspend functions return a single asynchronous value, while Flow can emit multiple values over time.
Explain what a Flow is and how it differs from a suspend function in Kotlin.
Think about streams of data versus single results.
You got /4 concepts.
Describe how to create and collect values from a Flow in Kotlin.
Focus on the flow builder and collection process.
You got /4 concepts.
Practice
(1/5)
1. What is the primary purpose of a Flow in Kotlin?
easy
A. To represent a stream of data that can emit multiple values over time
B. To store a single immutable value
C. To create a user interface layout
D. To handle exceptions synchronously
Solution
Step 1: Understand what Flow represents
A Flow is designed to emit multiple values asynchronously over time, like a stream of data.
Step 2: Compare options with Flow's purpose
Only To represent a stream of data that can emit multiple values over time correctly describes Flow as a stream of data emitting multiple values over time.
Final Answer:
To represent a stream of data that can emit multiple values over time -> Option A
Quick Check:
Flow = stream of data [OK]
Hint: Flow emits multiple values over time, not just one [OK]
Common Mistakes:
Confusing Flow with a single value container like LiveData
Thinking Flow is for UI layout
Assuming Flow handles exceptions synchronously
2. Which of the following is the correct way to create a Flow that emits numbers 1 to 3?
easy
A. val flow = flow { emit(1); emit(2); emit(3) }
B. val flow = flowOf(1, 2, 3)
C. All of the above
D. val flow = listOf(1, 2, 3).asFlow()
Solution
Step 1: Review Flow creation methods
Kotlin provides multiple ways to create flows: using flow builder with emit, flowOf, and converting collections with asFlow().
Step 2: Check each option
val flow = flow { emit(1); emit(2); emit(3) } uses flow builder with emit calls, B uses flowOf, and C converts a list to flow. All are valid ways.
Final Answer:
All of the above -> Option C
Quick Check:
Multiple ways to create Flow = D [OK]
Hint: Remember flow, flowOf, and asFlow() all create flows [OK]
Common Mistakes:
Thinking only flow builder with emit works
Confusing flowOf with a list
Not knowing asFlow() converts collections
3. What will be printed by this code?
val flow = flow {
emit(10)
emit(20)
}
runBlocking {
flow.collect { value -> println(value) }
}
medium
A. No output
B. 20 10
C. Only 10
D. 10 20
Solution
Step 1: Understand flow emission order
The flow emits 10 first, then 20 in order.
Step 2: Collect prints emitted values in order
Collect receives values in the same order they are emitted, so prints 10 then 20.
Final Answer:
10 20 -> Option D
Quick Check:
Flow emits in order = 10 20 [OK]
Hint: Flow emits values in order unless otherwise specified [OK]
5. You want to create a Flow that emits numbers 1 to 5 but only collect even numbers. Which code correctly achieves this?
hard
A. val flow = flow {
for (i in 1..5) emit(i)
}
runBlocking {
flow.collect { if (it % 2 == 0) println(it) }
}
B. All of the above
C. val flow = flow {
for (i in 1..5) if (i % 2 == 0) emit(i)
}
runBlocking {
flow.collect { println(it) }
}
D. val flow = flowOf(1, 2, 3, 4, 5)
runBlocking {
flow.filter { it % 2 == 0 }.collect { println(it) }
}
Solution
Step 1: Analyze each code snippet
val flow = flow {
for (i in 1..5) emit(i)
}
runBlocking {
flow.collect { if (it % 2 == 0) println(it) }
} emits all numbers and filters during collect; C emits only even numbers directly; D uses filter operator before collect.
Step 2: Confirm all produce only even numbers on collect
All options print only even numbers 2 and 4 when collected.
Final Answer:
All of the above -> Option B
Quick Check:
Multiple ways to filter Flow = B [OK]
Hint: Filter before or during collect both work [OK]
Common Mistakes:
Thinking filter must be before collect only
Not realizing emit can be conditional
Forgetting to run collect inside coroutine
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.