Basic Interview Questions
What is Kotlin and why did Google make it the preferred language for Android?
Kotlin is a statically typed JVM language from JetBrains that compiles to Java bytecode (and also to JS and native). Google declared Android development "Kotlin-first" in 2019 because Kotlin is concise, has null safety built into the type system, supports coroutines for async work, and is 100% interoperable with existing Java code and libraries. Jetpack Compose is Kotlin-only.
What are the main differences between Kotlin and Java?
| Kotlin | Java |
|---|---|
Null safety in the type system (String vs String?) | Any reference can be null |
Classes are final by default | Classes are open by default |
| No checked exceptions | Checked exceptions must be caught or declared |
| Data classes, extension functions, default arguments, smart casts | Records (Java 16+), no extensions, overloading instead of defaults |
| Coroutines for async code | Threads, CompletableFuture, virtual threads |
if, when, try are expressions | Mostly statements (switch expressions since Java 14) |
What is the difference between val, var and const val?
var: mutable reference, can be reassigned.val: read-only reference, assigned once (like Javafinal). The object it points to can still be mutable, e.g.val list = mutableListOf(1).const val: a compile-time constant (primitive or String only), declared at top level or inside anobject/companion object. Its value is inlined at use sites.
What is the difference between == and === in Kotlin?
Example
== checks structural equality (calls equals(), null-safe). === checks referential equality (same object).
data class Point(val x: Int, val y: Int)
fun main() {
val a = Point(1, 2)
val b = Point(1, 2)
val c = a
println("a == b : ${a == b}")
println("a === b : ${a === b}")
println("a === c : ${a === c}")
}Output
a == b : true a === b : false a === c : true
How is when different from Java switch?
when is an expression that returns a value, needs no break, and its branches can match constants, several values (1, 2 ->), ranges (in 1..9), types (is String) or arbitrary conditions (when { x > 0 -> ... }). When used as an expression over an enum or sealed type it must be exhaustive, so the compiler catches a missing case.
What are Unit, Nothing and Any?
| Type | Meaning |
|---|---|
Any | Root of all non-null types (like Object); Any? is the root of everything. |
Unit | A function returns no useful value (like void), but Unit is a real object, so it works with generics. |
Nothing | A function never returns normally: it always throws or loops forever, e.g. TODO(), error(). It is a subtype of every type, so val x: String = s ?: throw ... compiles. |
What are default and named arguments? What does @JvmOverloads do?
Kotlin parameters can have default values, and callers can pass arguments by name in any order, which removes most telescoping overloads and builder classes. Java does not understand defaults, so @JvmOverloads makes the compiler generate the overloads for Java callers (commonly used on custom Android View constructors).
fun sendOtp(phone: String, length: Int = 6, viaSms: Boolean = true) { /* ... */ }
sendOtp("9876543210")
sendOtp("9876543210", viaSms = false)
class ProfileView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyle: Int = 0
) : View(context, attrs, defStyle)What are string templates and ranges?
Example
Templates insert values with $name or ${expr}. Ranges are created with .., until (or ..<), downTo and step.
fun main() {
val name = "Asha"
val items = listOf(120, 80)
println("Hi $name, your total is Rs ${items.sum()}")
println((1..5).toList())
println((0 until 5).toList())
println((10 downTo 0 step 3).toList())
println(7 in 1..10)
}Output
Hi Asha, your total is Rs 200 [1, 2, 3, 4, 5] [0, 1, 2, 3, 4] [10, 7, 4, 1] true