Kotlin Exercises

Practice with interactive exercises. Check your answers instantly.

BeginnerVariables · multiple-choice

Which keyword declares a read-only variable?

BeginnerVariables · predict-output

What does this print? val x = 5 var y = x + 3 println(y)

BeginnerNull Safety · multiple-choice

What does name?.length return when name is null?

BeginnerFunctions · predict-output

What does this print? fun double(x: Int) = x * 2 println(double(7))

BeginnerCollections · multiple-choice

Which creates a mutable list?

BeginnerControl Flow · predict-output

What does this print? val n = 4 if (n % 2 == 0) println("Even") else println("Odd")

BeginnerNull Safety · predict-output

What does this print? val x: Int? = null println(x ?: -1)

IntermediateOOP · multiple-choice

What does a data class NOT auto-generate?

BeginnerStrings · predict-output

What does this print? val name = "Kotlin" println("Hello, $name")

BeginnerOperators · predict-output

What does this print? println(10 / 3)

BeginnerControl Flow · multiple-choice

Which loop iterates over a range 1 to 5 inclusive?

BeginnerWhen · predict-output

What does this print? val x = 2 val r = when (x) { 1 -> "A"; 2 -> "B"; else -> "C" } println(r)

BeginnerCollections · predict-output

What does this print? val nums = listOf(1, 2, 3) println(nums.size)

BeginnerLambdas · predict-output

What does this print? println(listOf(1,2,3,4).filter { it % 2 == 0 })

BeginnerOOP · multiple-choice

Which keyword allows a class to be extended?

IntermediateNull Safety · multiple-choice

What does !! do on a nullable value?

BeginnerFunctions · multiple-choice

What is the default value used here? greet(name: String = "World")

IntermediateGenerics · multiple-choice

What does List<String> mean?

BeginnerExceptions · predict-output

What does this print? try { println(10/0) } catch (e: ArithmeticException) { println("Error") }

IntermediateScope Functions · multiple-choice

Which scope function returns the lambda result (not the receiver)?

IntermediateCoroutines · multiple-choice

What does runBlocking do?

IntermediateFlow · multiple-choice

When does a cold Flow start emitting?

BeginnerData Types · multiple-choice

Which is the correct Kotlin integer type?

BeginnerRanges · predict-output

What does this print? for (i in 1 until 4) print("$i ")

BeginnerMaps · predict-output

What does this print? val m = mapOf("a" to 1) println(m["a"])

BeginnerVariables · predict-output

What does this print? val a = 2 val b = 3 println(a + b)

BeginnerStrings · multiple-choice

Which creates a read-only string in Kotlin?

BeginnerOperators · predict-output

What does this print? println(17 % 5)

BeginnerIf/Else · predict-output

What does this print? val x = -3 println(if (x > 0) "pos" else "neg")

BeginnerWhen · multiple-choice

What is when in Kotlin?

BeginnerFor Loop · predict-output

What does this print? for (i in 1..3) print(i)

BeginnerWhile · predict-output

What does this print? var n = 3 while (n > 0) { print(n); n-- }

BeginnerFunctions · multiple-choice

Which keyword declares a function in Kotlin?

BeginnerDefault Args · predict-output

What does this print? fun greet(name: String = "World") = println("Hi $name") greet()

BeginnerLists · multiple-choice

What does listOf() return?

BeginnerSets · predict-output

What does this print? println(setOf(1, 2, 2, 3).size)

BeginnerOOP · multiple-choice

What does data class auto-generate?

IntermediateInheritance · multiple-choice

Which keyword allows overriding?

IntermediateInterfaces · multiple-choice

How many interfaces can a class implement?

IntermediateSealed Classes · multiple-choice

Why use sealed class?

BeginnerNull Safety · predict-output

What does this print? val s: String? = "Kotlin" println(s?.uppercase())

BeginnerLambdas · predict-output

What does this print? println(listOf(1,2,3).count { it > 1 })

IntermediateExtension Functions · predict-output

What does this print? fun Int.double() = this * 2 println(4.double())

IntermediateGenerics · multiple-choice

What does List<String> mean?

IntermediateSuspend · multiple-choice

Where can you call suspend fun?

IntermediateDispatchers · multiple-choice

Best dispatcher for file I/O?

IntermediateFlow · multiple-choice

What does emit() do in flow { }?

BeginnerEnums · predict-output

What does this print? enum class Level { LOW, HIGH } println(Level.HIGH)

BeginnerCompanion Object · multiple-choice

How do you call a companion function?

BeginnerObject · multiple-choice

object Logger creates…