Kotlin quiz

Null safety, data classes, lambdas, collections and coroutines.

64 questionsInstant feedbackFree

Set up your quiz

Difficulty
Number of questions

Tip: press 1–4 to answer and Enter for the next question.

All 64 questions with answers
  1. Which company created Kotlin?

    • Google
    • Oracle
    • Microsoft
    • JetBrains (correct)
  2. Which keyword declares a read-only variable in Kotlin?

    • var
    • val (correct)
    • const only
    • let
  3. Which keyword declares a variable that can be reassigned in Kotlin?

    • val
    • let
    • var (correct)
    • mut
  4. Which keyword declares a function in Kotlin?

    • fun (correct)
    • function
    • func
    • def
  5. What is the entry point of a Kotlin program?

    • start()
    • static void main()
    • fun main() (correct)
    • def main()
  6. Which function prints a line to the console in Kotlin?

    • System.print()
    • println() (correct)
    • echo()
    • console.log()
  7. What file extension do Kotlin source files use?

    • .kt (correct)
    • .kts only
    • .kotlin
    • .ko
  8. Is a semicolon required at the end of each Kotlin statement?

    • Only inside functions
    • Yes, always
    • No, it is optional (correct)
    • Only after return
  9. Which syntax inserts a variable into a Kotlin string?

    • "Hello %name"
    • "Hello {name}"
    • "Hello $name" (correct)
    • "Hello #name"
  10. Which type holds whole numbers by default in Kotlin?

    • Integer
    • Long only
    • Number
    • Int (correct)
  11. Which type is used for true/false values in Kotlin?

    • Bit
    • Bool
    • Logic
    • Boolean (correct)
  12. Which keyword replaces the switch statement in Kotlin?

    • match
    • select
    • case
    • when (correct)
  13. What does 1..5 create in Kotlin?

    • A range from 1 to 5 inclusive (correct)
    • A range from 1 to 4
    • A list of two numbers
    • A decimal number
  14. Which function creates a read-only list in Kotlin?

    • listOf() (correct)
    • arrayListOf only
    • newList()
    • List()
  15. Which function creates a mutable list in Kotlin?

    • mutableListOf() (correct)
    • fixedListOf()
    • readOnlyList()
    • listOf()
  16. Kotlin is the officially preferred language for which platform?

    • iOS
    • PlayStation
    • Android (correct)
    • Windows desktop
  17. Which keyword is used for a class whose main job is holding data?

    • struct
    • data class (correct)
    • record
    • bean
  18. How do you write a single-line comment in Kotlin?

    • -- comment
    • # comment
    • ; comment
    • // comment (correct)
  19. What does if return in Kotlin when used as an expression?

    • Always a Boolean
    • A value (correct)
    • Nothing, it is only a statement
    • Always Unit
  20. Which keyword is used for a for loop over a range in Kotlin?

    • for (i in 1..3) (correct)
    • for (i = 1; i <= 3; i++)
    • foreach i in 1..3
    • loop i to 3
  21. What does the ? in String? mean?

    • The variable is optional to declare
    • The variable can hold null (correct)
    • The type is unknown
    • The string is empty
  22. What does the safe-call operator ?. do?

    • Calls the member only if the value is not null, otherwise returns null (correct)
    • Throws if the value is null
    • Converts null to an empty string
    • Checks the type
  23. What does the Elvis operator ?: do?

    • Casts a value
    • Compares two values
    • Creates a range
    • Returns the right side when the left side is null (correct)
  24. What does the !! operator do?

    • Asserts the value is not null and throws if it is (correct)
    • Negates a Boolean twice
    • Makes a value nullable
    • Checks equality
  25. Which methods does a data class generate automatically?

    • main()
    • Only getters
    • Only toString()
    • equals(), hashCode(), toString() and copy() (correct)
  26. Are classes in Kotlin open for inheritance by default?

    • No, they are final unless marked open (correct)
    • Only abstract classes
    • Yes, always
    • Only data classes
  27. Which keyword lets a class be inherited from?

    • open (correct)
    • virtual
    • extends
    • public
  28. What does the object keyword declare?

    • An enum
    • A singleton (correct)
    • A data class
    • An abstract class
  29. What is a companion object used for?

    • Members tied to the class rather than an instance, like static members (correct)
    • Holding private constructors
    • Creating copies of an object
    • Declaring interfaces
  30. What does a lambda like { x: Int -> x * 2 } represent?

    • A function value (correct)
    • A range
    • A class body
    • A map entry
  31. In a single-parameter lambda, what is the implicit name of the parameter?

    • this
    • self
    • arg
    • it (correct)
  32. What does list.filter { it > 2 } return?

    • The count of elements greater than 2
    • A new list with elements greater than 2 (correct)
    • True or false
    • The first element greater than 2
  33. What does list.map { it * 2 } return?

    • A map of keys to values
    • A new list with each element doubled (correct)
    • The sum of all elements times 2
    • The original list doubled in size
  34. What is an extension function?

    • A function that takes unlimited arguments
    • A function inside another function
    • A function added to an existing type without inheriting from it (correct)
    • A function marked open
  35. What does mapOf("a" to 1) create?

    • A list of pairs only
    • A read-only map with key "a" and value 1 (correct)
    • A mutable map
    • A range
  36. What does the lateinit modifier allow?

    • Declaring a constant
    • Skipping the constructor
    • Initializing a non-null var later, after declaration (correct)
    • Lazy computing a val
  37. What does by lazy do for a val?

    • Computes the value on first access and caches it (correct)
    • Makes the value mutable
    • Computes the value at compile time
    • Delays the program start
  38. What is a sealed class used for?

    • A thread-safe class
    • A restricted class hierarchy whose subclasses are known at compile time (correct)
    • A class that cannot have methods
    • A class with private fields only
  39. What does the until keyword in for (i in 0 until 5) produce?

    • 1 to 5
    • 0 to 4 (correct)
    • 0 to 5
    • 1 to 4
  40. What does step 2 do in for (i in 1..10 step 2)?

    • Visits every second list
    • Visits 2, 4, 6, 8, 10
    • Visits 1, 3, 5, 7, 9 (correct)
    • Repeats each number twice
  41. Which function returns the first element of a list or null if the list is empty?

    • first()
    • firstOrNull() (correct)
    • head()
    • getFirst()
  42. Which type does a Kotlin function return when it has no useful value?

    • Nothing
    • Unit (correct)
    • Void
    • Null
  43. Which keyword declares a compile-time constant in Kotlin?

    • static val
    • final val
    • let
    • const val (correct)
  44. What is the difference between == and === in Kotlin?

    • == checks references; === checks content
    • === is only for numbers
    • == checks structural equality; === checks if two references are the same object (correct)
    • They are identical
  45. What is a coroutine in Kotlin?

    • A database connection
    • A type of class
    • A lightweight unit of work that can suspend without blocking a thread (correct)
    • A new operating system thread
  46. What does the suspend modifier on a function mean?

    • It runs on a separate process
    • It can pause and resume and must be called from a coroutine or another suspend function (correct)
    • It runs only once
    • It cannot return a value
  47. Which builder starts a coroutine and returns a Deferred result?

    • launch
    • thread
    • runBlocking only
    • async (correct)
  48. What does launch return?

    • A Job (correct)
    • A Deferred
    • The result value
    • Unit
  49. Which dispatcher is meant for blocking IO work such as file or network calls?

    • Dispatchers.Main
    • Dispatchers.Default
    • Dispatchers.IO (correct)
    • Dispatchers.Unconfined
  50. What is the Nothing type used for?

    • Functions that return null
    • Optional parameters
    • Functions that never return normally, such as one that always throws (correct)
    • Empty lists
  51. What does the inline modifier do to a higher-order function?

    • Runs it on the main thread
    • Makes it private
    • Copies the function body and its lambda into the call site (correct)
    • Makes it recursive
  52. Inside a when expression on a sealed class, what lets you skip the else branch?

    • Using a data class
    • Adding @Suppress
    • Marking the function inline
    • Covering every subclass (correct)
  53. What does the scope function apply return?

    • The result of the lambda
    • The object it was called on (correct)
    • null
    • Unit
  54. What does the scope function let return?

    • Unit
    • Always null
    • The object it was called on
    • The result of the lambda (correct)
  55. What does a smart cast mean in Kotlin?

    • Casting with the as keyword
    • Converting numbers silently
    • Casting at runtime only
    • The compiler automatically casts a value after a type or null check (correct)
  56. What is a Flow in Kotlin coroutines?

    • A cold asynchronous stream of values (correct)
    • A blocking list
    • A thread pool
    • A UI layout
  57. What is structured concurrency?

    • Running coroutines in alphabetical order
    • Using only one thread
    • Coroutines are tied to a scope so they are cancelled and awaited together (correct)
    • Starting coroutines with GlobalScope
  58. What is the effect of the reified keyword on an inline function's type parameter?

    • The type is erased
    • The function becomes generic over Any only
    • The type becomes nullable
    • The type is available at runtime inside the function (correct)
  59. Which interface do Kotlin's destructuring declarations on data classes rely on?

    • Serializable
    • componentN() functions (correct)
    • Iterable
    • Comparable
  60. What does the copy() function of a data class do?

    • Copies the class definition
    • Creates a new instance, optionally changing some properties (correct)
    • Deep-copies every nested object
    • Clones the object in place
  61. What does the out keyword in List<out T> mean?

    • The type is invariant
    • The list is read-write
    • The type is covariant: it is only produced, not consumed (correct)
    • The type is contravariant
  62. Which function blocks the current thread until its coroutines complete and is mainly used in main or tests?

    • runBlocking (correct)
    • delay
    • launch
    • async
  63. What does delay() do inside a coroutine?

    • Cancels the coroutine
    • Blocks the thread with Thread.sleep
    • Suspends the coroutine without blocking the thread (correct)
    • Stops the program
  64. What is the difference between a List and a MutableList in Kotlin?

    • They are the same type
    • List has no methods to change it; MutableList adds add and remove (correct)
    • List is always empty
    • MutableList cannot hold nulls