Interview prep · Stage 1: Kotlin and DSA basics
Kotlin object interview questions
One opener and four harder follow-ups. Each step builds on the last, the way a real interviewer digs.
Try each question out loud before you open the answer.
Opening question
What is an object in Kotlin, and how is it different from a class?
Show answer
object Session {
var email: String? = null
fun isLoggedIn(): Boolean = email != null
}
Session.email = "hello@imayankkumar.com"Junior answer
An object is an instance of a class.
Senior answer
In Kotlin, the object keyword declares a class and its only instance in one step. A class can have many instances, one per constructor call. An object has exactly one, has no constructor, and you reach it by its name. Kotlin creates it the first time you use it.
What they're really checking: Saying "an instance of a class". The question is about the object keyword.
Follow-up 1
What does the compiler turn object Session into?
Show answer
public final class Session {
public static final Session INSTANCE = new Session();
private Session() { }
static { /* properties and init, in order */ }
}Junior answer
A class with static methods.
Senior answer
A final class with a private constructor and a public static final INSTANCE field. The instance is created in the class's static block, which also runs your property initializers and init block in the order you wrote them. Session.isLoggedIn() compiles to Session.INSTANCE.isLoggedIn(), so the functions are instance methods, not static ones. Add @JvmStatic if Java callers need static methods.
What they're really checking: The functions are not static. That's why an object can implement an interface and be passed as a value.
Follow-up 2
Is an object thread-safe?
Show answer
Junior answer
Yes, objects are thread-safe.
Senior answer
Only its creation. The JVM runs a class's static block once, and other threads wait for it, so you never get two instances. But a var inside the object is a normal shared variable. Two threads changing Session.email at the same time can still race. Use a StateFlow, an atomic type or a lock.
What they're really checking: Mixing up thread-safe creation with thread-safe state.
Follow-up 3
This code adds a listener and later removes it. Why does the listener keep running?
Show answer
loginButton.addLoginListener(object : LoginListener { /* ... */ })
// later
loginButton.removeLoginListener(object : LoginListener { /* ... */ })Answer
Each object expression creates a new instance of an anonymous class. The object passed to removeLoginListener is a different object from the one that was added, so nothing matches and nothing is removed. Keep one reference in a val and pass the same instance to both calls.
What they're really checking: Thinking object : is a singleton because it uses the object keyword.
Follow-up 4
Why use data object LoggedOut instead of object LoggedOut in a sealed interface?
Show answer
sealed interface AuthState {
data object LoggedOut : AuthState
data class LoggedIn(val email: String) : AuthState
}Junior answer
So it gets copy() and componentN() like a data class.
Senior answer
A plain object prints its class name and a hash, like AuthState$LoggedOut@3fee733d. A data object prints LoggedOut, which matches the data classes next to it in logs. It also gets an equals that checks the type and a fixed hashCode, so == works even if reflection or serialization creates a second instance. It has no copy() or componentN(), and you can't override equals or hashCode. Data objects are stable since Kotlin 1.9.0.
What they're really checking: Data objects don't get copy() or componentN(). There's only one instance and no data to copy.
Say it in 30 seconds
Show the answer
An object declaration is a class with exactly one instance. It has no constructor, and Kotlin creates it the first time you use it. On the JVM it becomes a final class with a private constructor and a static INSTANCE field, created in a static block, so creation is thread-safe. Its mutable state is not. On Android it lives as long as the process, so it is reset after process death and can leak an Activity it holds. An object expression, object : Listener { }, is an anonymous class that creates a new instance each time it runs. A data object is an object whose toString prints its name, which is why it fits sealed state like LoggedOut.