0
0
Kotlinprogramming~20 mins

Flow builder and collect in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flow Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin Flow code?

Consider the following Kotlin code using flow builder and collect. What will be printed?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        emit(2)
        emit(3)
    }
    flow.collect { value ->
        println(value * 2)
    }
}
A
1
2
3
BCompilation error
C
2
4
6
D
3
6
9
Attempts:
2 left
💡 Hint

Remember that collect receives each emitted value and you can transform it inside the lambda.

Predict Output
intermediate
2:00remaining
What does this Flow builder code print?

Analyze this Kotlin code snippet using flow and collect. What is the output?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow = flow {
        for (i in 1..3) {
            emit(i)
        }
    }
    flow.collect { value ->
        if (value == 2) return@collect
        println(value)
    }
}
ACompilation error
B
1
2
3
C
2
D
1
3
Attempts:
2 left
💡 Hint

Look at the return@collect statement inside the collect lambda.

Predict Output
advanced
2:00remaining
What is the output of this Flow with delay and collect?

Consider this Kotlin code using flow with delays. What will be printed?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.delay

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        delay(100)
        emit(2)
        delay(100)
        emit(3)
    }
    flow.collect { value ->
        println(value)
    }
}
A
1
2
3
B
3
2
1
C
1
1
1
DCompilation error
Attempts:
2 left
💡 Hint

The delay suspends the flow emission but does not change the order.

Predict Output
advanced
2:00remaining
What error does this Flow code produce?

What error will this Kotlin code produce when run?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        throw IllegalStateException("Error in flow")
    }
    flow.collect { value ->
        println(value)
    }
}
ANo output, program runs normally
BIllegalStateException: Error in flow
CNullPointerException
DCompilation error
Attempts:
2 left
💡 Hint

Exceptions thrown inside flow builder propagate to the collector.

🧠 Conceptual
expert
2:00remaining
How many items are collected from this Flow?

Given this Kotlin Flow code, how many items will be collected and printed?

Kotlin
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking

fun main() = runBlocking {
    val flow = flow {
        for (i in 1..5) {
            emit(i)
            if (i == 3) break
        }
    }
    var count = 0
    flow.collect {
        println(it)
        count++
    }
    println("Total collected: $count")
}
A3 items
B5 items
C0 items
DCompilation error
Attempts:
2 left
💡 Hint

The break statement terminates the loop after emitting 3.