0
0
Kotlinprogramming~20 mins

Flow context preservation in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flow Context Master
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 with context preservation?

Consider this Kotlin code using flow and withContext. What will it print?

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

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        withContext(Dispatchers.IO) {
            emit(2)
        }
        emit(3)
    }
    flow.collect { value ->
        println(value)
    }
}
A
2
3
B
1
3
C
1
Exception thrown
D
1
2
3
Attempts:
2 left
💡 Hint

Think about how withContext affects the flow builder and emission.

Predict Output
intermediate
2:00remaining
What happens if you emit inside a different coroutine context without preserving flow context?

What will this Kotlin code print?

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

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        launch(Dispatchers.IO) {
            emit(2)
        }.join()
        emit(3)
    }
    flow.catch { e -> println("Error: $e") }
        .collect { value -> println(value) }
}
A
1
3
B
1
Error: java.lang.IllegalStateException: Flow invariant is violated
C
1
2
3
D
1
Exception thrown at emit(2)
Attempts:
2 left
💡 Hint

Consider what happens when emit is called from a different coroutine launched inside the flow builder.

🔧 Debug
advanced
2:30remaining
Why does this Kotlin Flow code cause a runtime error?

Identify the cause of the runtime error in this Kotlin Flow snippet:

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

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        withContext(Dispatchers.IO) {
            launch {
                emit(2)
            }
        }
        emit(3)
    }
    flow.collect { println(it) }
}
ABecause emit(2) is called inside a new coroutine launched inside withContext, breaking flow context preservation
BBecause emit(3) is called after withContext block
CBecause withContext cannot be used inside flow builder
DBecause runBlocking cannot collect flows
Attempts:
2 left
💡 Hint

Look at where emit(2) is called and how coroutines are nested.

📝 Syntax
advanced
2:30remaining
Which option correctly preserves flow context when switching dispatcher?

Choose the Kotlin code snippet that correctly emits values on different dispatchers without breaking flow context preservation.

A
flow {
  emit(1)
  withContext(Dispatchers.IO) {
    launch {
      emit(2)
    }
  }
  emit(3)
}
B
flow {
  emit(1)
  launch(Dispatchers.IO) {
    emit(2)
  }
  emit(3)
}
C
flow {
  emit(1)
  withContext(Dispatchers.IO) {
    emit(2)
  }
  emit(3)
}
D
flow {
  emit(1)
  GlobalScope.launch(Dispatchers.IO) {
    emit(2)
  }
  emit(3)
}
Attempts:
2 left
💡 Hint

Remember that emit must be called from the flow's coroutine context.

🚀 Application
expert
3:00remaining
How many items are emitted by this Kotlin Flow with nested context switches?

Analyze the following Kotlin Flow code and determine how many items will be emitted and collected.

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

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        withContext(Dispatchers.IO) {
            emit(2)
            withContext(Dispatchers.Default) {
                emit(3)
            }
        }
        emit(4)
    }
    val collected = mutableListOf<Int>()
    flow.collect { collected.add(it) }
    println(collected.size)
}
A4
B3
C2
DException thrown
Attempts:
2 left
💡 Hint

Consider how nested withContext calls affect flow emissions.