0% found this document useful (0 votes)
17 views36 pages

Kotlin Language Interview

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views36 pages

Kotlin Language Interview

g
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Kotlin Language

Interview Q&A –
2025 Edition
⚙️ Mastering Kotlin for Android
Interviews – 2025 Edition

🧱 Class Types • ⚙️ Functions &


Scoping • 🛡 Null Safety • 🪝 Interop

A complete Kotlin interview guide with 34 essential


questions – covering everything from data, sealed,
inline, delegation, DSLs, to real-world traps like ==
vs ===.
1/23
Q1. What is a data class in
Kotlin and when should you
use it?

A data class is used to store and represent plain data.


Kotlin auto-generates toString(), equals(), hashCode(),
and copy() functions.
It's ideal for modeling things like users, API
responses, or UI states.
2/23
Q2. What is a sealed class
and how is it different from
abstract or enum?

A sealed class restricts subclassing to a known set of


types in the same file. It allows exhaustive when
expressions — great for modeling states like success, error,
loading.
3/23
Q3. What is the object
keyword in Kotlin and where
is it used?

The object keyword defines a singleton — a class with only


one instance. Use it for utilities, global state, or anonymous
objects.
4/23

Q4. What is a companion


object and how is it
different from object?

A companion object is a singleton declared inside a class.


It acts like a static context and allows class-level
functions or constants.
5/23

Q5. What are enum classes


and how do they support
behavior?

An enum class represents a fixed set of constant values.


Each constant can also have functions or properties. Useful
for states, directions, or modes.
6/23

Q6. How does Kotlin handle


inheritance differently
from Java?

In Kotlin, classes are final by default — you must mark


them open to allow inheritance.
This avoids accidental subclassing and enforces safer
OOP.
7/23

Q7. What is the init block in


Kotlin and when does it
execute?

The init block runs during object creation, right after the
primary constructor. It’s used for input validation or side-
effects during initialization.
8/23

Q8. What are sealed


interfaces and how are they
different from sealed
classes?
Sealed interfaces (Kotlin 1.5+) work like sealed classes but
allow multiple inheritance. They’re useful when you want
polymorphism with shared behaviors.
9/23

Q9. How does constructor


overloading work in Kotlin?

Kotlin uses default parameter values to simplify


constructor overloading. You can also define secondary
constructors if needed.
10/23

Q10. What is an inline function


and when should you use it?

An inline function tells the compiler to insert the function


body at the call site.
It reduces the overhead of creating function objects and
is especially useful when passing lambdas.
Use when:
You're passing lambdas to functions
You want to avoid memory overhead
Performance matters in tight call paths
11/23

Q11. What’s the difference


between inline, noinline, and
crossinline?

inline: Inlines the entire function, including lambdas


noinline: Prevents inlining of specific lambda
parameters
crossinline: Restricts use of return from lambdas (no
non-local returns)
12/23

Q12. What are higher-order


functions and how does Kotlin
support them?

Higher-order functions take functions as parameters or


return them. Kotlin supports this using lambda
expressions or function references.
Use cases:
Reusable business logic
Custom filtering, mapping
Functional patterns
13/23

Q13. What is @JvmOverloads


and why is it useful for Java
interop?

Kotlin supports default arguments, but Java does not.


@JvmOverloads generates multiple overloaded
versions for Java compatibility.
Use it when:
Your function has default parameters
It's accessed from Java code (e.g., Android XML, Java
SDKs)
14/23

Q14. What is the operator


modifier and how do you
overload operators in Kotlin?

The operator modifier allows you to override symbols


like +, -, [], or ==.
It adds syntactic sugar to class operations.
15/23

Q15. What is SAM conversion


in Kotlin and where is it
useful?

SAM = Single Abstract Method.


Kotlin can convert lambdas to Java interfaces with a
single method — great for interop with Java APIs like
Runnable or Android listeners.
16/23

Q16. What’s the difference


between let, run, apply, with,
and also?

These are scope functions used to perform operations on


objects. They differ in:
Return type
this vs it context
When to use (side effects, object config, chaining)
Summary Use Cases:

let transform or chain with it

run execute logic and return result

apply configure object (this context), returns
object

with same as run but not extension

also side-effects like logging (it context), returns
object
17/23

Q17. What are extension


functions and how are they
resolved at runtime ?
Extension functions let you add new functions to existing
classes without modifying their source code or using
inheritance.
📌 Key Point:
They are statically resolved at compile time — not
polymorphic.
🛠 Use Cases:
Adding helper functions (e.g.,
[Link]())
Cleaner chaining (like .orEmpty())
Enhancing third-party or SDK classes
18/23

Q18. How do function


references work in Kotlin
(e.g., ::functionName)?
Function references in Kotlin allow you to refer to a
function by its name using the :: operator, without
invoking it.
🛠 Common Use Cases:
Passing functions to higher-order functions
Cleaner syntax over lambda expressions
Reusability and decoupling logic
19/23

Q19. What is the difference


between val, var, and const
val?

val: read-only (assigned once)


var: mutable
const val: compile-time constant (top-level or inside
object)
20/23

Q20. What is the difference


between lazy and lateinit in
Kotlin?

lazy: used with val, initialized on first access


lateinit: used with var, for non-nullable vars initialized
later (e.g., in Android)
21/23

Q21. How does Kotlin handle


property delegation (by)?

Kotlin lets you delegate property logic using the by


keyword. Instead of handling getter/setter manually,
you hand it over to a delegate.
Common Delegates:
lazy – initializes only when accessed
observable – listens to property changes
map – used in dynamic key-value property binding
custom – build your own delegate class
22/23

Q22. What are visibility


modifiers in Kotlin (public,
internal, private, protected)?

Visibility modifiers control where code (classes, functions,


properties) can be accessed from. Kotlin provides:

🔓 public (default) – Accessible from anywhere.


🔒 internal – Accessible within the same module.
🔐 private – Accessible within the same file (top-level) or
within the class (member-level).
🛡 protected – Accessible within the class and its
subclasses (but not from other files).
23/23

Q23. What are typealias and


when should you use them?

typealias creates an alternate name for an existing type


— helping improve readability and reduce repetition.

🟢 Common use cases:


Simplifying complex function types
Handling platform-specific declarations
Aliasing model or domain types for clarity
23/23

Q24. What is a value class


and why would you use it?

A value class (previously called inline class) in Kotlin


wraps a single value and avoids object allocation at
runtime.
It is useful for type safety, performance, and avoiding
unnecessary allocations.
23/23

Q25. How does Kotlin handle


null safety?

Kotlin enforces null safety at the type level.


By default, all variables are non-nullable, and you
must explicitly mark a variable as nullable using ?.
This helps prevent NullPointerException at compile
time.
23/23

Q26. What is the Elvis


operator (?:) and how does it
work?
The Elvis operator ?: is used to provide a default value
when an expression is null.
If the expression on the left is non-null, it returns that
value; otherwise, it returns the value on the right.
🔐 Safe fallback mechanism in null-prone situations.
23/23

Q27. What is the !! operator


and when should you avoid
it?

The !! operator tells the compiler: “I know this value isn’t


null.”
It forcefully unwraps a nullable variable — but if it is null,
it throws a NullPointerException.
❌ Avoid using !! unless you're absolutely sure the value
is never null.
✅ Prefer safe calls (?.) or proper null checks.
23/23

Q28. What is the safe call


operator (?.) and what does it
return?

The safe call operator ?. lets you safely access a property


or call a function on a nullable object.
If the object is null, it returns null without throwing an
exception.
This helps avoid NullPointerException in nullable chains.
23/23

Q29. What is smart casting in


Kotlin?

Smart casting is Kotlin’s ability to automatically cast a


variable to its specific type after a successful type-
check (like isor != null) — without needing an explicit
cast (as).
It works with:
Type checks using is
Null checks (!= null)
Safe type usage inside if, when, or smart flow
branches
23/23

Q30. What’s the difference


between == and ===?

== checks structural equality — it calls .equals()


under the hood
=== checks referential equality — it verifies if two
references point to the same object in memory

Use == when comparing values/content.


Use === when comparing object references.
23/23
Q31. What are default and
named arguments in Kotlin
functions?
Kotlin lets you define default values for function
parameters so callers can omit them if needed.
You can also use named arguments to make the code
more readable and flexible — especially when some
parameters are optional or skipped.

🔹 Default arguments reduce overloads


🔹 Named arguments improve clarity and avoid
ambiguity
23/23

Q32. How does Kotlin support


destructuring declarations?
Kotlin supports destructuring declarations using the
componentN() functions. These are auto-generated for
data classproperties and can be manually defined in
regular classes. This allows you to unpack multiple
properties into separate variables in a clean, readable
way.

Use cases:
Unpacking data class values
Iterating over maps
Returning multiple values from a function
23/23

Q33. When should you use


multiple constructors vs
default arguments?
Prefer default arguments because they reduce
boilerplate and improve readability. Kotlin allows
setting default values directly in the primary constructor
or function, making code cleaner and more concise.

Use secondary constructors only when:


You need different initialization logic
You're calling another constructor in the same class
using this()
You need to support Java-based libraries expecting
overloaded constructors
23/23

Q34. When should you prefer


a DSL in Kotlin and how do you
build one?
DSLs are useful for building domain-specific code like
HTML builders, Gradle, or configuration blocks.
Use lambda with receiver and extension functions.
Thank You for Reading!

Ready to dive deeper into Android, Kotlin, and


Clean Architecture?

I regularly share hands-on tutorials and


practical code tips that you can apply directly
to your projects.

Got questions or want to see specific topics


covered? Drop a comment below or send me
a DM - I'd love to hear what you're working on!

You might also like