0
0
Kotlinprogramming~10 mins

RunBlocking for bridging 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 start a coroutine that blocks the current thread until it completes.

Kotlin
import kotlinx.coroutines.*

fun main() {
    [1] {
        println("Hello from coroutine!")
    }
}
Drag options to blanks, or click blank then click option'
Alaunch
Basync
CrunBlocking
Ddelay
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch which is non-blocking and returns immediately.
Using async without awaiting the result.
2fill in blank
medium

Complete the code to delay inside a runBlocking coroutine.

Kotlin
import kotlinx.coroutines.*

fun main() = [1] {
    println("Start")
    delay(500)
    println("End")
}
Drag options to blanks, or click blank then click option'
ArunBlocking
Basync
Claunch
DwithContext
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch which does not block the main thread.
Trying to call delay outside a coroutine scope.
3fill in blank
hard

Fix the error in the code by choosing the correct coroutine builder to bridge blocking and suspending code.

Kotlin
import kotlinx.coroutines.*

fun main() {
    [1] {
        val result = async {
            delay(100)
            "Done"
        }.await()
        println(result)
    }
}
Drag options to blanks, or click blank then click option'
Alaunch
BrunBlocking
CGlobalScope.launch
DcoroutineScope
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch which returns immediately and does not block.
Using GlobalScope.launch which is also non-blocking.
4fill in blank
hard

Fill both blanks to create a map of word lengths for words longer than 3 characters inside runBlocking.

Kotlin
import kotlinx.coroutines.*

fun main() = [1] {
    val words = listOf("apple", "bat", "carrot", "dog")
    val lengths = words.filter { word -> word.length > 3 }.associate { word -> word to [2] }
    println(lengths)
}
Drag options to blanks, or click blank then click option'
ArunBlocking
Bword.length
Cword.uppercase()
Dlaunch
Attempts:
3 left
💡 Hint
Common Mistakes
Using launch which does not block the main thread.
Using word.uppercase() instead of length.
5fill in blank
hard

Fill all three blanks to create a filtered map of uppercase words and their lengths inside runBlocking.

Kotlin
import kotlinx.coroutines.*

fun main() = [1] {
    val words = listOf("apple", "bat", "carrot", "dog")
    val filtered = words.filter { word -> word.length > 3 }.associate { word -> word.[2]() to word.[3] }
    println(filtered)
}
Drag options to blanks, or click blank then click option'
ArunBlocking
Buppercase
Clength
Dlowercase
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase.
Using launch instead of runBlocking.