Skip to content
tezvyn:

Top 30 Kotlin Interview Questions and Answers

30 multiple-choice questions on Kotlin, drawn from 30 bites out of the 225 tagged Kotlin on Tezvyn. Answer them here or read straight down. Every question carries the correct option, why it is correct, and a link to the bite it came from.

30 questions. Pick an answer, or open “Show the answer” to read it.

Answers are graded in your browser. Nothing is saved, and no XP or streak is earned here. The app keeps score.

  1. Question 1 of 30

    In Kotlin, what does declaring a variable with val guarantee about the referenced object?

    Show the answer

    Answer: c · The reference cannot be reassigned, though the object's contents may still mutate

    val only prevents reassigning the reference itself, so a val holding a MutableList can still be modified. The most tempting distractor confuses reference immutability with deep object immutability, which val does not enforce.

    Read the full bite: Explain val vs var in Kotlin and null safety risks

  2. Question 2 of 30

    A developer needs to store a user's current score in a game, which will change throughout gameplay. Which Kotlin keyword should be used?

    Show the answer

    Answer: c · var

    The card states that 'var' should be used for values that need to be reassigned, such as a user's score, while 'val' is for values that remain constant after initial assignment. Choosing 'val' here would lead to a compilation error when trying to update the score.

    Read the full bite: Kotlin Variables: `val` for Constants, `var` for Variables

  3. Question 3 of 30

    When adding a new implicit dependency such as a Logger to a deep call chain, what is the main benefit of Kotlin 2.4.0 stable context parameters?

    Show the answer

    Answer: a · They let the compiler pass the dependency implicitly through the call stack, avoiding changes to every intermediate function signature.

    Stable context parameters allow the compiler to implicitly pass dependencies through call chains, eliminating the need to refactor every intermediate signature when adding a new implicit dependency. Option C describes explicit backing fields, a separate Kotlin 2.4.0 feature for property storage shapes, not dependency threading.

    Read the full bite: Kotlin 2.4.0 Ships Stable Context Parameters and UUID API

  4. Question 4 of 30

    What is the main advantage of Kotlin's approach to null safety with default non-nullable types?

    Show the answer

    Answer: d · It shifts the detection of potential NullPointerExceptions from runtime to compile time.

    Kotlin's type system is explicitly designed to prevent NullPointerExceptions (NPEs) at compile time, a major benefit over languages that only catch them at runtime. Option B is a common misconception; val declares a read-only reference, not necessarily a compile-time constant.

    Read the full bite: Explain val, var, and null safety in Kotlin

  5. Question 5 of 30

    Given the Kotlin declaration `val userNames = mutableListOf("Alice")`, which statement accurately describes the `userNames` variable?

    Show the answer

    Answer: d · The variable `userNames` cannot be reassigned, but the list it points to can be modified.

    The `val` keyword creates a read-only reference, meaning the `userNames` variable cannot be reassigned. However, the `mutableListOf` object it points to is still mutable, allowing its contents to be changed. A common misconception is that `val` makes the object itself immutable.

    Read the full bite: Explain val vs. var and null safety in Kotlin

  6. Question 6 of 30

    When migrating to Koog 1.0, how should teams handle the stable and beta module split to align with JetBrains' support guarantees?

    Show the answer

    Answer: c · Move core dependencies to stable modules while isolating beta features to specific use cases

    Koog 1.0 guarantees one year of API stability only for stable core modules, so teams should migrate core dependencies there while isolating beta features to specific use cases. Option B is tempting but wrong because the framework explicitly supports adding beta features as needed rather than avoiding them entirely.

    Read the full bite: Koog 1.0 Stabilizes Kotlin AI Agent Framework

  7. Question 7 of 30

    How do the Kotlin Language Server Alpha and the Kotlin Toolchain differ in purpose?

    Show the answer

    Answer: a · The Language Server brings IntelliJ-engine-backed completions to LSP editors, while the Toolchain unifies build, test, and agent workflows

    The Language Server delivers IntelliJ-engine-backed diagnostics and completions to LSP-compatible editors, whereas the Toolchain is a unified CLI for builds, tests, and agents. Option C reverses these roles, which is tempting because both tools were announced together and relate to developer workflow.

    Read the full bite: Kotlin 2.4.0 Preview, Unified Toolchain, and LSP Alpha Land

  8. Question 8 of 30

    What is the main benefit of Swift package support in Kotlin/Native for multiplatform teams?

    Show the answer

    Answer: c · It lets teams consume Apple ecosystem dependencies directly instead of maintaining bridging wrappers or CocoaPods workarounds.

    The card states that Swift package support allows pulling Apple dependencies directly, eliminating the need for bridging wrappers or CocoaPods workarounds. Option B is tempting because it mentions CocoaPods, but the feature provides an alternative rather than forcing a migration.

    Read the full bite: Kotlin 2.4.0 ships Swift package support and Java 26

  9. Question 9 of 30

    If user is null, what happens when evaluating user?.name ?: "Guest"?

    Show the answer

    Answer: b · It returns Guest without throwing an exception

    The safe call operator ?. returns null when user is null, and the Elvis operator ?: then substitutes Guest as the fallback. Option C is tempting because ?. does preserve nullability, but it ignores that ?: immediately provides the default value.

    Read the full bite: What do the safe call and Elvis operators do in Kotlin?

  10. Question 10 of 30

    Given a nullable `user: User?`, how do you safely get its name's length, returning 0 if either `user` or its `name` property is null?

    Show the answer

    Answer: d · user?.name?.length ?: 0

    The safe call `?.` accesses properties only if the object is not null, and the Elvis operator `?:` provides the default value `0` if the preceding expression is null. The `if/else` block is verbose and not idiomatic Kotlin.

    Read the full bite: Purpose of Kotlin's safe call (`?.`) and Elvis (`?:`) operators?

  11. Question 11 of 30

    Given `data class User(val name: String?)` and a nullable `val activeUser: User?`, which Kotlin expression correctly retrieves the user's name, providing "Guest" if `activeUser` is null or its `name` property is null?

    Show the answer

    Answer: d · activeUser?.name ?: "Guest"

    Option D correctly uses the safe call operator (?. ) to access 'name' only if 'activeUser' is not null, and then the Elvis operator (?: ) to provide "Guest" if the result is null. Option C, while functionally correct, is considered non-idiomatic Kotlin as it uses a verbose if/else block instead of the more concise and expressive ?. and ?: operators.

    Read the full bite: Purpose of safe call (`?.`) and Elvis (`?:`) operators

  12. Question 12 of 30

    What is the main consequence of using the not-null assertion operator (!!) in Kotlin?

    Show the answer

    Answer: b · It tells the compiler to trust that a value is not null, potentially leading to a runtime NullPointerException if it is.

    The not-null assertion operator (!!) bypasses Kotlin's compile-time null safety, forcing the compiler to assume a value is non-null. If the value turns out to be null at runtime, it will result in a NullPointerException, defeating the purpose of Kotlin's null safety. Option A is incorrect because it introduces risk, and options C and D describe the Elvis operator (?:) and safe call operator (?.), respectively.

    Read the full bite: Kotlin Null Safety: Catch Nulls at Compile Time

  13. Question 13 of 30

    What is a mandatory requirement when using an `if` expression to assign a value to a variable in Kotlin?

    Show the answer

    Answer: b · An `else` branch must always be present to cover all possible outcomes.

    When `if` is used as an expression, it must always return a value. To guarantee this, the compiler requires an `else` branch to cover all possible cases. The last line of a branch implicitly becomes its return value, without needing an explicit `return` keyword.

    Read the full bite: Kotlin's Control Flow Expressions: `if` and `when`

  14. Question 14 of 30

    If a class has a member function and an extension function is defined with the same signature, which one will be invoked when called on an instance of that class?

    Show the answer

    Answer: b · The member function, because member functions always take precedence.

    When a member function and an extension function have the same signature, the member function will always be chosen. This is because extension functions are resolved statically and do not override class members, making the member function the more specific choice.

    Read the full bite: What is a Kotlin extension function? Write one for String.

  15. Question 15 of 30

    Which statement accurately describes the underlying mechanism of Kotlin extension functions?

    Show the answer

    Answer: c · They are compiled into static utility methods where the receiver object is passed as the first parameter.

    The card explicitly states that extension functions are "syntactic sugar for static utility methods" and are "compiled down to static methods that take the receiver object as the first argument." They do not modify the original class or its bytecode, which is a common misconception.

    Read the full bite: What is a Kotlin extension function? Write one for String.

  16. Question 16 of 30

    Which statement accurately describes how Kotlin resolves an extension function call on a receiver?

    Show the answer

    Answer: c · It uses the compile-time declared type of the receiver, compiling down to a static method call

    Kotlin extension functions are resolved at compile time based on the declared receiver type and compile to static methods, not real class members. Option D is tempting because it mirrors true polymorphic dispatch, but extensions are statically dispatched and cannot be overridden.

    Read the full bite: What is an extension function? Write a hasWhitespace extension for String.

  17. Question 17 of 30

    What is the main purpose of using functions in Kotlin?

    Show the answer

    Answer: d · To package reusable logic, preventing code duplication and enhancing structure.

    The card states functions exist "to solve the problem of code repetition and disorganization" by packaging logic into "a single, named block that can be called from anywhere." Option C is incorrect because while good code structure can aid optimization, functions' primary role isn't automatic speed enhancement.

    Read the full bite: Kotlin Functions: Named, Reusable Code Blocks

  18. Question 18 of 30

    When working with immutable state in Kotlin, which generated function of a data class is specifically designed to create a new object instance with modified properties without altering the original?

    Show the answer

    Answer: b · copy()

    The copy() function is specifically designed to create a new instance of the data class, allowing certain properties to be modified while leaving the original object unchanged, which is crucial for immutable state management. The other generated functions serve different purposes like comparison, string representation, or hash generation, not for creating modified copies.

    Read the full bite: What are the advantages of a Kotlin data class and its functions?

  19. Question 19 of 30

    When managing immutable state with a data class, what is the primary role of the generated `copy()` function?

    Show the answer

    Answer: b · To create a new, modified instance of the object, leaving the original unchanged.

    The `copy()` function is central to working with immutable objects; it creates a new instance with specified changes, leaving the original untouched. Option A is a critical misunderstanding, as `copy()` never mutates the original object, which is key to predictable state management.

    Read the full bite: What are the advantages of a Kotlin data class?

  20. Question 20 of 30

    When using a Kotlin data class as a key in a HashMap, which statement about its compiler-generated behavior is true?

    Show the answer

    Answer: b · Only properties defined in the primary constructor marked as val or var are used in generated equals and hashCode, so the key remains consistent in the map.

    The compiler only incorporates primary constructor properties marked val or var into generated equals and hashCode, guaranteeing the contractual consistency required for safe use as a HashMap key. Option C is a tempting distractor because developers often assume body properties participate in structural equality, but the compiler intentionally excludes them.

    Read the full bite: What are the primary advantages of using a data class?

  21. Question 21 of 30

    What is the primary reason Kotlin classes are final by default?

    Show the answer

    Answer: a · To encourage explicit design for inheritance and promote composition over inheritance.

    The card states that Kotlin's design makes inheritance an intentional act to encourage more robust code and promotes composition over inheritance. Option A directly captures these core reasons. Option C is incorrect as 'final by default' relates to inheritance, not the mutability of properties, which is controlled by 'val' and 'var'.

    Read the full bite: Kotlin Inheritance: Open for Extension, Closed by Default

  22. Question 22 of 30

    An Android Fragment needs a property for a complex, computationally expensive object that is only used in certain user flows. The object should be created only once. Which declaration is most appropriate?

    Show the answer

    Answer: b · private val myObject: MyObject by lazy { createExpensiveObject() }

    `val by lazy` is ideal for expensive, immutable properties because it defers creation until first access and caches the result. `lateinit var` is incorrect because the property is immutable and is meant for when an external framework provides the value, not for deferred computation.

    Read the full bite: lateinit var vs. val by lazy in Android

  23. Question 23 of 30

    Which statement accurately describes a fundamental difference between lateinit var and val by lazy in Kotlin?

    Show the answer

    Answer: b · val by lazy can initialize properties of any type, including primitives, unlike lateinit var.

    The card states that lateinit var cannot be used for primitive types like Int or Boolean, while val by lazy has no such restriction. Option A is incorrect because it describes the behavior of val by lazy, not lateinit var, which is initialized manually.

    Read the full bite: lateinit var vs. val by lazy in Android

  24. Question 24 of 30

    In an Activity, a property holds a ViewModel obtained via ViewModelProvider in onCreate. Why is lateinit var more suitable than val by lazy?

    Show the answer

    Answer: c · lateinit var allows imperative assignment and can be reassigned if the Activity is recreated

    lateinit var is designed for mutable properties assigned imperatively in lifecycle callbacks like onCreate and can be reassigned when the Activity is recreated, whereas val by lazy is immutable and self-initializing. Option D is tempting but wrong because it describes lazy's first-access caching behavior, not lateinit's imperative assignment model.

    Read the full bite: Explain lateinit var versus val by lazy in Android

  25. Question 25 of 30

    Which statement accurately describes the behavior of the copy() method generated for a Kotlin data class?

    Show the answer

    Answer: d · It performs a shallow copy, meaning references to mutable objects within the data class are shared between the original and the new instance.

    The card explicitly states that the copy() function creates a shallow copy. This means if a data class contains a mutable object, both the original and the copied instance will refer to the same mutable object. Option A is a common misconception, as many expect a copy function to perform a deep copy for safety.

    Read the full bite: Kotlin Data Classes: Automatic Boilerplate for Data Holders

  26. Question 26 of 30

    What core characteristic of Kotlin's function handling makes higher-order functions possible?

    Show the answer

    Answer: b · The treatment of functions as first-class citizens, allowing them to be passed as arguments or returned.

    The card explicitly states that higher-order functions are possible "because Kotlin treats functions as first-class citizens, meaning they can be stored in variables or passed as arguments." This fundamental concept allows functions to be used as parameters or return values. While type inference is a useful Kotlin feature, it does not enable the core mechanism of functions being treated as first-class entities.

    Read the full bite: Explain and implement a Kotlin higher-order function

  27. Question 27 of 30

    In Kotlin, what is the defining characteristic that makes a function a 'higher-order function'?

    Show the answer

    Answer: b · It accepts a function as an argument or returns a function as its result.

    The correct answer is right because the definition of a higher-order function is one that treats functions as first-class citizens by taking them as parameters or returning them. While many higher-order functions operate on collections (a tempting distractor), this is a common use case, not the defining property.

    Read the full bite: Explain and implement a higher-order function in Kotlin

  28. Question 28 of 30

    What distinguishes filterAndTransform as a higher-order function rather than merely a generic function in Kotlin?

    Show the answer

    Answer: d · It takes other functions as arguments, such as predicate and transform

    A higher-order function must accept or return a function, so taking predicate and transform as arguments makes it one. Option C describes generics, which alone do not make a function higher-order.

    Read the full bite: Explain higher-order functions and implement filterAndTransform

  29. Question 29 of 30

    To model a state machine where each state might carry unique, type-specific data (e.g., a success state holding a user object, an error state holding a message), which Kotlin construct is most appropriate?

    Show the answer

    Answer: b · A sealed class

    Sealed classes are specifically designed for representing restricted hierarchies where subclasses can be distinct types (like data classes or objects) and hold different associated data. Enum classes cannot hold different types of data for each constant, making them unsuitable for states with unique payloads.

    Read the full bite: When to use a sealed class instead of an enum?

  30. Question 30 of 30

    To model a network request's state—`Loading`, `Success` (with data), or `Error` (with a message)—which Kotlin construct is most suitable?

    Show the answer

    Answer: d · A sealed class, because each of its subclasses can carry different data types specific to that state.

    A sealed class is ideal because its subclasses can represent states with different associated data (e.g., data for Success, a message for Error) in a type-safe way. An enum is a set of constants of the same type and cannot elegantly model states with different data requirements.

    Read the full bite: When would you use a sealed class instead of an enum?

Could you explain these out loud?

That is what an interview actually tests. Tezvyn gives you questions like these with what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon