0
0
Kotlinprogramming~10 mins

Coroutines vs threads mental model in Kotlin - Interactive 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
GlobalScope.[1] {
    println("Hello")
}
Drag options to blanks, or click blank then click option'
ArunBlocking
Basync
Claunch
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using runBlocking inside GlobalScope.launch which blocks the thread.
Using delay as a coroutine builder which is incorrect.
2fill in blank
medium

Complete the code to block the main thread until the coroutine finishes.

Kotlin
runBlocking [1] {
    launch {
        println("Running in coroutine")
    }
}
Drag options to blanks, or click blank then click option'
A{
B() -> Unit
C{ }
Dasync
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for the lambda block.
Trying to use async as a parameter to runBlocking.
3fill in blank
hard

Fix the error in the code to correctly start a thread that prints "Thread running".

Kotlin
val thread = Thread [1] {
    println("Thread running")
}
thread.start()
Drag options to blanks, or click blank then click option'
A{
B() -> Unit
C()
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of curly braces for the lambda.
Passing a function type instead of a lambda block.
4fill in blank
hard

Fill both blanks to create a coroutine that delays for 1000ms and then prints "Done".

Kotlin
GlobalScope.[1] {
    delay([2])
    println("Done")
}
Drag options to blanks, or click blank then click option'
Alaunch
B1000L
C500
DrunBlocking
Attempts:
3 left
💡 Hint
Common Mistakes
Using runBlocking instead of launch.
Using 500 instead of 1000L for delay.
5fill in blank
hard

Fill all three blanks to create a map of coroutine names to their status checking if they are active.

Kotlin
val coroutinesStatus = mapOf(
    "coroutine1" to [1],
    "coroutine2" to [2],
    "coroutine3" to [3]
)
Drag options to blanks, or click blank then click option'
Ajob1.isActive
Bjob2.isActive
Cjob3.isActive
Djob4.isActive
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same job variable multiple times.
Using a job variable that is not declared.