0
0
Kotlinprogramming~20 mins

Coroutines vs threads mental model in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Coroutine Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of coroutine vs thread switching

What is the output of this Kotlin code that uses a coroutine and a thread?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("Start coroutine")
        delay(100)
        println("End coroutine")
    }
    Thread {
        println("Start thread")
        Thread.sleep(50)
        println("End thread")
    }.start()
    delay(200)
}
A
Start coroutine
Start thread
End thread
End coroutine
B
Start thread
Start coroutine
End coroutine
End thread
C
Start coroutine
End coroutine
Start thread
End thread
D
Start thread
End thread
Start coroutine
End coroutine
Attempts:
2 left
💡 Hint

Remember coroutines are lightweight and can suspend without blocking threads.

🧠 Conceptual
intermediate
1:30remaining
Difference in resource usage between coroutines and threads

Which statement best describes the difference in resource usage between Kotlin coroutines and threads?

ACoroutines use less memory and are more lightweight than threads because they share threads and suspend without blocking.
BThreads use less memory than coroutines because threads are managed by the OS.
CCoroutines and threads use the same amount of memory because both run concurrently.
DThreads are more lightweight because they can suspend without blocking, unlike coroutines.
Attempts:
2 left
💡 Hint

Think about how coroutines suspend without blocking threads.

🔧 Debug
advanced
2:00remaining
Identify the error in coroutine and thread usage

What error will this Kotlin code produce?

Kotlin
import kotlinx.coroutines.*

fun main() {
    GlobalScope.launch {
        println("Coroutine running")
        delay(100)
        println("Coroutine finished")
    }
    Thread.sleep(50)
    println("Main finished")
}
AThe program throws a runtime exception because delay cannot be called outside runBlocking.
BThe program prints "Coroutine running" then "Main finished" and exits before "Coroutine finished" prints.
CThe program prints all three lines in order without error.
DThe program deadlocks and never finishes.
Attempts:
2 left
💡 Hint

Consider the lifecycle of GlobalScope coroutines and main thread.

Predict Output
advanced
2:30remaining
Output order with multiple coroutines and threads

What is the output of this Kotlin program?

Kotlin
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        println("Coroutine 1 start")
        delay(100)
        println("Coroutine 1 end")
    }
    launch {
        println("Coroutine 2 start")
        delay(50)
        println("Coroutine 2 end")
    }
    Thread {
        println("Thread start")
        Thread.sleep(75)
        println("Thread end")
    }.start()
}
A
Thread start
Coroutine 1 start
Coroutine 2 start
Thread end
Coroutine 2 end
Coroutine 1 end
B
Coroutine 2 start
Coroutine 1 start
Thread start
Coroutine 2 end
Coroutine 1 end
Thread end
C
Coroutine 1 start
Thread start
Coroutine 2 start
Coroutine 2 end
Thread end
Coroutine 1 end
D
Coroutine 1 start
Coroutine 2 start
Thread start
Coroutine 2 end
Thread end
Coroutine 1 end
Attempts:
2 left
💡 Hint

Coroutines start immediately but suspend at delay; thread runs independently with sleep.

🧠 Conceptual
expert
1:30remaining
Why coroutines are preferred over threads for concurrency in Kotlin

Which reason best explains why Kotlin coroutines are often preferred over threads for concurrency?

ACoroutines require more memory but provide better debugging tools than threads.
BThreads are faster than coroutines because they run on multiple CPU cores automatically.
CCoroutines allow writing asynchronous code sequentially and use fewer resources by suspending without blocking threads.
DThreads are easier to cancel and manage than coroutines in Kotlin.
Attempts:
2 left
💡 Hint

Think about how coroutines simplify asynchronous programming and resource use.