0
0
Kotlinprogramming~10 mins

Suspend functions concept in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a suspend function named fetchData.

Kotlin
suspend fun [1]() {
    // simulate fetching data
}
Drag options to blanks, or click blank then click option'
AfetchData
BretrieveData
CloadData
DgetData
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than requested.
2fill in blank
medium

Complete the code to call the suspend function fetchData inside a coroutine scope.

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    [1]()
}
Drag options to blanks, or click blank then click option'
Alaunch
Basync
CfetchData
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to launch or async instead of calling the function directly.
3fill in blank
hard

Fix the error in the suspend function declaration by filling the blank.

Kotlin
fun [1] fetchData() {
    // code
}
Drag options to blanks, or click blank then click option'
Asuspend
Basync
Claunch
Dcoroutine
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'async' or 'launch' as function modifiers instead of 'suspend'.
4fill in blank
hard

Fill both blanks to create a suspend function that delays for 1000 milliseconds and then prints a message.

Kotlin
suspend fun waitAndPrint() {
    [1](1000L)
    println([2])
}
Drag options to blanks, or click blank then click option'
Adelay
B"Done waiting!"
C"Waiting finished"
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'print' instead of 'println', or wrong string message.
5fill in blank
hard

Fill all three blanks to create a suspend function that fetches data, delays, and returns a string result.

Kotlin
suspend fun fetchData(): String {
    val data = "Data"
    [1](500L)
    return [2] + [3]
}
Drag options to blanks, or click blank then click option'
Adelay
Bdata
C" fetched"
D"done"
Attempts:
3 left
💡 Hint
Common Mistakes
Returning wrong strings or forgetting to delay.