Kotlin Interview Cheat Sheet

Kapil Vij
6 min readOct 7, 2021

Two Months ago when i started appearing in interviews I used to get blank in some of tricky questions in Kotlin even when i spend some time coding in Kotlin in live projects. But I kept collecting questions which are not mentioned anywhere under single umbrella.

So i decided to collate some of important questions currently being asked for people who gets little time to prepare for interviews. This article will help you clear kotlin questions with ease and it's hardly a 15 mins read for beginners.

Here You go…………

Q1. What is the difference between val and var?
var is like a general variable and can be assigned multiple times and is known as the mutable variable in Kotlin. Whereas val is a constant variable which can be Initialized only single time and is known as the immutable variable in Kotlin.

Q2. What is the difference between val and const val?
const and val both represents the immutability and read only values and act as final keyword in java. val keyword must be used to declare for run time values and const keyword must be used to declare compile time values.

Q3. What is Difference between setValue() and PostValue() in MutableLiveData?
setValue() method must be called from the main thread. But if you need set a value from a background thread, postValue() should be used.

Q4. How to check if lateinit property is initialized or not?
You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized() method. It returns true if the lateinit property has been initialized otherwise false.

Q5. When to use lateinit and lazy keywords in kotlin?
Lateinit is used with mutable, while lazy is used with immutable
lateinit

lazy → It initializes variable only when it is required for the first time.

Lazy :- There are certain classes whose object initialization is very heavy and so much time taking that it results in the delay of the whole class creation process.

Q6. What is difference between companion object and object?Companion Object is initialized when class is loaded. But Object is initialized lazily by default — when accessed for the first time.

Q7. Difference between safe calls(?.) and Non-null Assertion(!!)?
Safe Call Operator (?.) is used when you want to make sure that your app shouldn’t crash even if variable reference you are holding is null.

var variable: String? = null
variable?.replace(“x”, “z”)

Please note we have not initialized variable above, but it will not throw NullPointerException as Safe call operator is used.

But in case of Non-Null Assertion, if you call any method on its reference it will throw KotlinNullPointerException.

variable!!.replace(“x”, “z”)

Q8. What are data classes in kotlin?

Only the property name will be used inside the toString(), equals(), hashCode(), and copy() implementations. While two Person objects can have different ages, they will be treated as equal for above example.

Q9. Why kotlin classes are final by default ?
Design and document for inheritance or else prohibit it — Core principle from book Effective Java by Joshua Bloch’s

In Simple words -> If classes were open by default and we would forget to mark class as final — (troubles might happen), but when we forget to mark class as open and try to extend it — we will be notified (no trouble).

Q10. Difference between == operator and === operator?
The == operator is used to compare the values of variables but === operator is used to check whether references of the variable is equal or not.
And in the case of primitive types, the === operator also checks for the value and not reference.
Please note both will result in same in case primitive data types.

Q11. Access/Visibility Modifiers in Kotlin
Four types of access modifiers

  • protected: visible inside that particular class or file and also in the subclass of that particular class where it is declared.
  • private: visible inside that particular class or file containing the declaration.
  • internal: visible everywhere in that particular module.
  • public: visible to everyone.

Note: By default, the visibility modifier in Kotlin is public.

Q12. What are extension functions in Kotlin?
Extension Function
provides an option to “add” methods to class without inheriting a class. The created extension functions are used as a regular function inside that class. See Example below:

Now you can call this same method on any type of view directly on its reference. e.g. textView.hideView()

Q13. What are inline functions ?
Inline function instruct compiler to insert complete body of the function wherever that function got used in the code. To use an Inline function, all you need to do is just add an inline keyword at the beginning of the function declaration.

Q14. What are scope functions in Kotlin ?
Scoped functions are functions that execute a block of code within the context of an object. There are five scoped functions in kotlin : let, run, with, also and apply.

The scope functions differ by the result they return:

  • apply and also return the context object. So they can be used in chaining function calls on the same object after them. They also can be used in return statements of functions
  • let, run, and with return the lambda result.

Learn more about scope functions here.

Q15. What are sealed classes in kotlin?
Sealed classes are similar to enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances, each with its own state.

To declare a sealed class or interface, put the sealed modifier before its name:

A sealed class is abstract by itself, it cannot be instantiated directly and can have abstract members.

Q16. What is significance of annotations : @JvmStatic, @JvmOverloads, and @JvmFiled in Kotlin?
-> @JvmStatic: This annotation is used to tell the compiler that the method is a static method and can be used in Java code.
-> @JvmOverloads: To use the default values passed as an argument in Kotlin code from the Java code.
-> @JvmField: To access the fields of a Kotlin class from Java code without using any getters and setters.

Q17. What are infix functions?
An infix function is used to call the function without using any bracket or parenthesis. You need to use the infix keyword to use the infix function.
e.g .

Q18. How to create a singleton in Kotlin?
Just use object

This is equivalent to below java code :

Q19. What are advantages to when over switch in kotlin?
It is more concise and powerful than a traditional switch. when can be used either as an expression or as a statement.

Q20. What are primary and secondary constructors in Kotlin?
Primary Constructor
is initialized in the class header, goes after the class name, using the constructor keyword. The parameters are optional in the primary constructor.
The primary constructor cannot contain any code, the initialization code can be placed in a separate initializer block prefixed with the init keyword.

Secondary Constructor — Kotlin may have one or more secondary constructors. Secondary constructors allow initialization of variables and allow to provide some logic to the class as well. They are prefixed with the constructor keyword.

Q21. What are Higher Order Functions?
A higher-order function is a function that takes functions as parameters, or returns a function.

That’s all folks.
But Keep watching this space coz I will keep updating the same article

Android Questions : https://kapilvij.medium.com/android-interview-questions-e52dfb2cd64c

References :

Biggest References are actual Interviews with Product companies like Walmart, Byju’s, OyoRooms, Deutsche Telekom, Zee Entertainment, GoJek and many more.

https://kotlinlang.org/docs/home.html

--

--