What is the type of `length` after this safe-call chain?
val length = user?.address?.city?.lengthRead the full question →Kotlin is the default language for Android and a common Java replacement on the JVM. Coroutines are its defining feature.
Null safety and platform types, coroutine scopes and cancellation, suspend function semantics, inline and reified generics, and data class copy behaviour.
Where it shows up: Android apps, JVM back ends adopting Kotlin, and multiplatform mobile teams.
Twenty real Kotlin questions from the library, code snippet included. Nothing here is paraphrased for search engines. It is the same text a signed-in user sees.
val length = user?.address?.city?.lengthRead the full question →suspend fun loadData(): String = withContext(Dispatchers.IO) {
blockingDbCall()
}Read the full question →val name: String? = null
name?.let { println(it.uppercase()) }Read the full question →import kotlinx.coroutines.flow.*
import kotlinx.coroutines.runBlocking
fun main() = runBlocking {
val flow = (1..3).asFlow()
.map { println("map $it"); it * 2 }
println("flow built")
flow.collect { println("got $it") }
}Read the full question →tailrec fun isEven(n: Int): Boolean =
if (n == 0) true else isOdd(n - 1)
tailrec fun isOdd(n: Int): Boolean =
if (n == 0) false else isEven(n - 1)Read the full question →suspend fun run() = coroutineScope {
val childA = launch { delay(1000); println("A") }
val childB = launch { throw IllegalStateException("boom") }
}Read the full question →flow {
repeat(5) { emit(it); println("emitted $it") }
}.buffer(2).collect {
delay(100)
println("collected $it")
}Read the full question →import kotlinx.coroutines.*
val scope = CoroutineScope(Dispatchers.Default + CoroutineName("parent"))
fun demo() {
scope.launch(Dispatchers.IO) {
// what context applies here?
}
}Read the full question →launch {
var i = 0
while (i < 1_000_000_000) { i++ }
println("done")
}Read the full question →// Writer thread:
data = 42
flag = true
// Reader thread:
if (flag) println(data)Read the full question →tailrec fun factorial(n: Long, acc: Long = 1L): Long {
return if (n <= 1L) acc
else factorial(n - 1, acc * n)
}Read the full question →suspend fun work() = coroutineScope {
launch { delay(50); throw RuntimeException("boom") }
launch { delay(1000); println("sibling") }
}Read the full question →val text: String? = null
val result = text?.uppercase()Read the full question →data class Account(val id: Int) {
var balance: Int = 0
}Read the full question →class Box {
fun show() = "member"
}
fun Box.show() = "extension"
println(Box().show())Read the full question →val count = 5
count = 6Read the full question →data class Point(val x: Int, val y: Int)
val a = Point(1, 2)
val b = Point(1, 2)
println(a == b)Read the full question →fun kind(c: Char) = when (c) {
'a', 'e', 'i', 'o', 'u' -> "vowel"
else -> "consonant"
}
println(kind('e'))Read the full question →data class User(val id: Int, val name: String)Read the full question →data class User(val name: String, val age: Int)
val a = User("Ana", 30)
val b = a.copy(age = 31)Read the full question →We keep the correct answer, the explanation and the scoring behind sign-in so the platform stays honest, which is why the answer options and the worked explanation for every Kotlin question stay behind a free account. Signing in is free and takes a few seconds.
58 distinct Kotlin topics are tagged across the library. These are the real topic labels stored on the questions, not a hand-written list.
…