Challenge - 5 Problems
Suspend Functions Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember that suspend functions must be called from a coroutine or another suspend function.
✗ Incorrect
The suspend function greet() is called inside runBlocking, which is a coroutine builder. The delay suspends the coroutine without blocking the thread, then returns the string. The println prints the returned string.
🧠 Conceptual
intermediate1:30remaining
Understanding suspend function behavior
Which statement best describes what happens when a suspend function is called in Kotlin?
Attempts:
2 left
💡 Hint
Think about how suspend functions help with concurrency without blocking threads.
✗ Incorrect
Suspend functions suspend the coroutine they run in, freeing the thread to do other work. They do not block the thread or create new threads automatically.
🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Check where suspend functions can be called from.
✗ Incorrect
Suspend functions can only be called from other suspend functions or coroutine builders. Calling fetchData() directly in main() causes a compilation error.
📝 Syntax
advanced2: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" }
Attempts:
2 left
💡 Hint
Remember that runBlocking is a coroutine builder that blocks the main thread until completion.
✗ Incorrect
Option D uses runBlocking to call the suspend function fetchUser() properly and prints the result synchronously. Option D calls suspend function directly causing compile error. Option D launches a coroutine but main returns immediately, so println may not run before program ends. Option D is valid syntax if main is suspend, but typically main is not suspend in Kotlin JVM apps; however, the original Option D was invalid syntax because coroutineScope cannot be called like that in main without suspend. Corrected Option D marks main as suspend.
🚀 Application
expert2: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) }
Attempts:
2 left
💡 Hint
Suspend functions inside string templates are evaluated sequentially in runBlocking.
✗ Incorrect
Inside runBlocking, the suspend functions first() and second() are called sequentially inside the string template. first() delays 100ms, then second() delays 50ms. The final string is 'First and Second'.