0
0
Kotlinprogramming~10 mins

Launch coroutine builder 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 launch a coroutine that prints "Hello".

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    [1] {
        println("Hello")
    }
}
Drag options to blanks, or click blank then click option'
Adelay
Basync
Crun
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using async instead of launch when no result is needed.
Trying to call delay directly without a coroutine builder.
2fill in blank
medium

Complete the code to launch a coroutine that delays for 1000ms before printing "Done".

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        [1](1000)
        println("Done")
    }
}
Drag options to blanks, or click blank then click option'
Await
Bdelay
Cpause
Dsleep
Attempts:
3 left
💡 Hint
Common Mistakes
Using Thread.sleep which blocks the thread instead of suspending the coroutine.
Using incorrect function names like wait or pause.
3fill in blank
hard

Fix the error in the code to correctly launch a coroutine inside runBlocking.

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    [1] {
        println("Inside coroutine")
    }
}
Drag options to blanks, or click blank then click option'
Alaunch
Brun
Casync
DcoroutineScope
Attempts:
3 left
💡 Hint
Common Mistakes
Using run which is not a coroutine builder.
Using coroutineScope without proper context.
4fill in blank
hard

Fill both blanks to launch a coroutine that prints the thread name.

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    [1] {
        println("Running on: " + Thread.[2]().name)
    }
}
Drag options to blanks, or click blank then click option'
Alaunch
BcurrentThread
CmainThread
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using mainThread which does not exist.
Using async when no result is needed.
5fill in blank
hard

Fill all three blanks to launch a coroutine that delays and then prints a message with the delay time.

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    val time = 500L
    [1] {
        [2](time)
        println("Waited for [3] ms")
    }
}
Drag options to blanks, or click blank then click option'
Alaunch
Bdelay
Ctime
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using async instead of launch when no result is needed.
Using a literal number instead of the variable time in the print statement.