0
0
Kotlinprogramming~20 mins

Timeout with withTimeout in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Timeout Mastery
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 code using withTimeout?
Consider the following Kotlin code snippet using withTimeout. What will be printed when this code runs?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    try {
        withTimeout(1000L) {
            repeat(5) { i ->
                println("Processing $i")
                delay(400L)
            }
        }
    } catch (e: TimeoutCancellationException) {
        println("Timeout occurred")
    }
}
A
Processing 0
Processing 1
Processing 2
Timeout occurred
B
Processing 0
Processing 1
Processing 2
Processing 3
Processing 4
CTimeout occurred
D
Processing 0
Timeout occurred
Attempts:
2 left
💡 Hint
Think about how long each delay takes and the total timeout limit.
Predict Output
intermediate
2:00remaining
What happens if delay exceeds withTimeout duration?
What will be the output of this Kotlin code snippet?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    try {
        withTimeout(500L) {
            delay(1000L)
            println("Finished")
        }
    } catch (e: TimeoutCancellationException) {
        println("Timeout!")
    }
}
ATimeout!
BFinished
CNo output
DRuntimeException
Attempts:
2 left
💡 Hint
The delay is longer than the timeout duration.
🔧 Debug
advanced
2:00remaining
Identify the error in this withTimeout usage
This Kotlin code snippet is intended to timeout after 1 second, but it does not behave as expected. What is the error?
Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    withTimeout(1000L) {
        repeat(3) { i ->
            println("Step $i")
            Thread.sleep(500L)
        }
    }
    println("Done")
}
AwithTimeout requires delay, not Thread.sleep, otherwise it throws an exception.
BUsing Thread.sleep blocks the thread and prevents cancellation, so timeout does not work.
CThe timeout value is too short to complete the loop, causing an immediate cancellation.
DrunBlocking cannot be used with withTimeout.
Attempts:
2 left
💡 Hint
Consider how blocking calls affect coroutine cancellation.
📝 Syntax
advanced
2:00remaining
Which option correctly uses withTimeout to return a value?
Select the Kotlin code snippet that correctly uses withTimeout to return a value from a suspending block.
A
val result = withTimeout(1000L) {
    delay(500L)
    println("Success")
}
println(result)
B
val result = withTimeout(1000L) {
    delay(500L)
    return "Success"
}
println(result)
C
val result = withTimeout(1000L) {
    delay(500L)
    "Success"
}
println(result)
D
val result = withTimeout(1000L) {
    delay(500L)
    "Success"
}
return result
println(result)
Attempts:
2 left
💡 Hint
Remember how to return values from lambda blocks in Kotlin.
🚀 Application
expert
3:00remaining
How to handle multiple withTimeout calls with different durations?
You want to run two suspending tasks sequentially with different timeouts: first task with 1 second timeout, second with 2 seconds timeout. Which Kotlin code snippet correctly implements this?
A
runBlocking {
    val first = withTimeout(1000L) {
        delay(900L)
        "First"
    }
    val second = withTimeout(2000L) {
        delay(1500L)
        "Second"
    }
    println("$first and $second")
}
B
runBlocking {
    withTimeout(3000L) {
        val first = delay(900L)
        val second = delay(1500L)
        println("First and Second")
    }
}
C
runBlocking {
    val first = withTimeout(1000L) {
        delay(1100L)
        "First"
    }
    val second = withTimeout(2000L) {
        delay(1500L)
        "Second"
    }
    println("$first and $second")
}
D
runBlocking {
    val first = withTimeoutOrNull(1000L) {
        delay(1100L)
        "First"
    } ?: "Timeout"
    val second = withTimeoutOrNull(2000L) {
        delay(1500L)
        "Second"
    } ?: "Timeout"
    println("$first and $second")
}
Attempts:
2 left
💡 Hint
Consider how to handle tasks that might timeout without throwing exceptions.