Top Kotlin Interview Questions and Answers

Here is a comprehensive list of 30 Kotlin interview questions with answers for experienced developers (5-8 years), covering Core Kotlin, Coroutines, Android Development, Functional Programming, and Advanced Topics.


1. Kotlin Basics

1. What are the key differences between val and var in Kotlin?

  • val (immutable): Read-only, cannot be reassigned.
  • var (mutable): Can be reassigned.
val name = "Kotlin"  // Immutable
var age = 30         // Mutable

2. How does Kotlin handle null safety?

  • ? denotes nullable types: val name: String? = null
  • Safe calls (?.), Elvis operator (?:), and !! (force unwrap).
val length = name?.length ?: 0  // Avoids NullPointerException

3. What are the differences between == and === in Kotlin?

  • == checks structural equality (content comparison).
  • === checks referential equality (same object).

2. Object-Oriented Concepts

4. What is the difference between open and final in Kotlin?

  • open: Allows inheritance.
  • final: Prevents further extension.

5. How do you create a Singleton in Kotlin?

  • Use the object keyword.
object Singleton {
    fun show() = println("Singleton instance")
}

6. What is the difference between object and companion object?

  • object: Standalone singleton.
  • companion object: Singleton inside a class (like static in Java).
class Example {
    companion object {
        fun show() = println("Companion Object")
    }
}

7. What is a data class in Kotlin?

  • Automatically generates equals(), hashCode(), copy(), toString().
data class User(val name: String, val age: Int)

3. Functional Programming

8. What are extension functions in Kotlin?

  • Allows adding methods to existing classes.
fun String.removeSpaces() = this.replace(" ", "")

9. What is the difference between let, apply, run, with, and also?

FunctionReturnsUse Case
letLambda resultOperate on nullable object
applyObjectInitialize object
runLambda resultExecute lambda with object
withLambda resultOperate on object
alsoObjectSide effects like logging

10. What are higher-order functions in Kotlin?

  • Functions that take other functions as parameters.
fun operation(x: Int, y: Int, op: (Int, Int) -> Int) = op(x, y)

4. Coroutines & Concurrency

11. What are coroutines in Kotlin?

  • Lightweight threads for async programming using suspend.

12. Difference between launch and async?

  • launch: Does not return a value.
  • async: Returns a value (Deferred<T>).

13. What is suspend in Kotlin?

  • Marks a function as suspendable, allowing non-blocking execution.
suspend fun fetchData() { /*...*/ }

14. What are Dispatchers.IO, Dispatchers.Main, Dispatchers.Default?

  • IO: Background tasks.
  • Main: UI thread.
  • Default: CPU-intensive work.

5. Collections & Generics

15. Difference between List, MutableList, Set, and Map in Kotlin?

  • List: Immutable.
  • MutableList: Can be modified.
  • Set: Unique elements.
  • Map: Key-value pairs.

16. What is reified in Kotlin?

  • Allows access to generic type information at runtime.
inline fun <reified T> getType() = T::class.java

17. Explain covariance and contravariance in Kotlin?

  • out: Covariant (only return values).
  • in: Contravariant (only accept values).

6. Android Development with Kotlin

18. How is LiveData different from StateFlow?

FeatureLiveDataStateFlow
ThreadingMain threadSupports coroutines
BehaviorHot streamHot stream
Replays data?NoYes

19. How do you use Kotlin Coroutines with ViewModel?

class MyViewModel : ViewModel() {
    private val viewModelScope = CoroutineScope(Dispatchers.Main)
}

20. How does Kotlin improve Android development?

  • Null safety, concise syntax, coroutine support, extension functions.

7. Advanced Topics

21. What are sealed classes in Kotlin?

  • Used for restricted class hierarchies.
sealed class Result {
    class Success(val data: String) : Result()
    class Error(val error: String) : Result()
}

22. What is Kotlin’s delegation pattern?

interface Printer {
    fun printMessage()
}

class PrinterImpl : Printer {
    override fun printMessage() = println("Printing...")
}

class PrinterDelegate(printer: Printer) : Printer by printer

23. How do inline functions improve performance?

  • Avoids creating function objects and lambda expressions.

8. Serialization & Networking

24. How do you use Kotlin with Retrofit?

interface ApiService {
    @GET("users")
    suspend fun getUsers(): List<User>
}

25. What are Kotlin Serialization and Gson?

  • Kotlin Serialization: Built-in JSON handling.
  • Gson: Java-based JSON library.

9. Testing & Performance Optimization

26. How do you write unit tests in Kotlin?

  • Use JUnit and MockK for testing.

27. How does lazy initialization work in Kotlin?

  • Delays initialization until the first access.
val data: String by lazy { "Initialized on first use" }

10. Miscellaneous

28. What is typealias in Kotlin?

  • Creates an alias for a type.
typealias UserMap = Map<String, List<String>>

29. How do you optimize Kotlin code for performance?

  • Use inline, avoid reflection, use sealed classes, prefer lazy.

30. How does Kotlin handle immutability?

  • Use val, data class, copy(), and immutable collections.

 

Leave a Reply

Your email address will not be published. Required fields are marked *