5 Basic Kotlin Interview Questions With Answers
5 Basic Kotlin Interview Questions With Answers
1. What is Kotlin and what are its key advantages over Java?
Answer: Kotlin is a statically typed programming language developed by JetBrains that runs on the Java
Virtual Machine (JVM). It's 100% interoperable with Java and has been Google's preferred language for
Android development since 2019.
Key Advantages:
Conciseness:
kotlin
// Java
public class Person {
private String name;
private int age;
// Kotlin
data class Person(var name: String, var age: Int)
Null Safety:
kotlin
Other advantages:
Type Inference: Automatically determines variable types
2. Explain the difference between val and var in Kotlin. When would you use
each?
Answer: val and var are used to declare variables in Kotlin, but they have different mutability
characteristics.
kotlin
kotlin
Best Practice:
kotlin
3. What are nullable types in Kotlin and how do you handle null safely?
Answer: Kotlin's type system distinguishes between nullable and non-nullable types to eliminate null
pointer exceptions at compile time.
kotlin
2. Elvis Operator ( ?: ):
kotlin
kotlin
4. Not-null Assertion ( !! ):
kotlin
kotlin
kotlin
if (nickname != null) {
println(nickname.length) // Smart cast - treated as non-null
}
Best Practices:
kotlin
4. Explain Kotlin data classes and their benefits. How do they differ from
regular classes?
Answer: Data classes in Kotlin are classes designed to hold data. They automatically generate useful
methods and provide concise syntax for creating classes that primarily store state.
kotlin
Auto-generated Methods:
kotlin
Regular Class:
kotlin
class RegularUser(val id: Int, val name: String, val email: String) {
// Must manually implement if needed
override fun toString(): String {
return "RegularUser(id=$id, name=$name, email=$email)"
}
Benefits:
Advanced Usage:
kotlin
// Usage
val products = listOf(
Product("1", "Laptop", 999.99, "Electronics"),
Product("2", "Mouse", 29.99, "Electronics")
)
// Destructuring in loops
for ((id, name, price) in products) {
println("$name costs $price")
}
5. What are Kotlin extension functions and how do you create them? Provide
examples.
Answer: Extension functions allow you to add new functionality to existing classes without modifying
their source code or using inheritance. They appear to be part of the class but are actually resolved
statically.
// Usage
val text = "Hello world from Kotlin"
println(text.wordCount()) // Output: 4
kotlin
Examples:
kotlin
// Usage
println(42.isEven()) // true
println(listOf(1, 2, 3).secondOrNull()) // 2
println(listOf(1).secondOrNull()) // null
data class Person(val firstName: String, val lastName: String, val age: Int)
// Extension function
fun Person.fullName(): String = "$firstName $lastName"
// Extension property
val Person.isAdult: Boolean
get() = age >= 18
// Usage
val person = Person("John", "Doe", 25)
println(person.fullName()) // John Doe
println(person.isAdult) // true
kotlin
// Usage
val name: String? = null
println(name.isNotNull()) // false
4. Practical Examples:
kotlin
// Usage
val timestamp = System.currentTimeMillis()
println(timestamp.toDateString()) // 2024-01-15
Important Characteristics:
1. Static Resolution:
kotlin
class Example {
fun existing() = "Member function"
}
3. Extension Properties:
kotlin
// Usage
println("Hello".lastChar) // 'o'
Best Practices: