0
0
Kotlinprogramming~20 mins

Suspend functions concept in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Suspend Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple suspend function call
What is the output of this Kotlin code using a suspend function?
Kotlin
import kotlinx.coroutines.*

suspend fun greet(): String {
    delay(100)
    return "Hello, Kotlin!"
}

fun main() = runBlocking {
    val message = greet()
    println(message)
}
ACompilation error: suspend function called from non-suspend context
BHello, Kotlin!
CHello, Kotlin!\nHello, Kotlin!
DRuntime exception: Unresolved reference: delay
Attempts:
2 left
💡 Hint
Remember that suspend functions must be called from a coroutine or another suspend function.
🧠 Conceptual
intermediate
1:30remaining
Understanding suspend function behavior
Which statement best describes what happens when a suspend function is called in Kotlin?
AIt suspends the coroutine without blocking the thread, allowing other coroutines to run.
BIt runs the function in a new thread automatically.
CIt blocks the current thread until the function completes.
DIt converts the function into a callback-based asynchronous function.
Attempts:
2 left
💡 Hint
Think about how suspend functions help with concurrency without blocking threads.
🔧 Debug
advanced
2:00remaining
Identify the error in suspend function usage
What error will this Kotlin code produce when compiled and run?
Kotlin
suspend fun fetchData(): String {
    return "Data"
}

fun main() {
    val result = fetchData()
    println(result)
}
ACompilation error: suspend function fetchData() called from non-suspend function main()
BCompilation error: missing coroutine context
COutput: Data
DRuntime error: suspend function cannot be called directly
Attempts:
2 left
💡 Hint
Check where suspend functions can be called from.
📝 Syntax
advanced
2:00remaining
Correct syntax for calling a suspend function
Which option correctly calls the suspend function fetchUser() and prints its result?
Kotlin
suspend fun fetchUser(): String {
    delay(50)
    return "User123"
}
A
fun main() {
    val user = fetchUser()
    println(user)
}
B
suspend fun main() = coroutineScope {
    val user = fetchUser()
    println(user)
}
C
fun main() {
    GlobalScope.launch {
        val user = fetchUser()
        println(user)
    }
}
D
fun main() = runBlocking {
    val user = fetchUser()
    println(user)
}
Attempts:
2 left
💡 Hint
Remember that runBlocking is a coroutine builder that blocks the main thread until completion.
🚀 Application
expert
2:30remaining
Predict the output of nested suspend functions with delay
What will be printed when running this Kotlin program?
Kotlin
import kotlinx.coroutines.*

suspend fun first(): String {
    delay(100)
    return "First"
}

suspend fun second(): String {
    delay(50)
    return "Second"
}

fun main() = runBlocking {
    val result = "${first()} and ${second()}"
    println(result)
}
ACompilation error: suspend functions cannot be called inside string templates
BSecond and First
CFirst and Second
DRuntime error: IllegalStateException
Attempts:
2 left
💡 Hint
Suspend functions inside string templates are evaluated sequentially in runBlocking.