Kotlin Interview Questions

Kotlin Interview Questions | Freshers & Experienced

  • Om Prakash
  • 16th Nov, 2019

Q1. What is the difference between object { } block and companion object { } code block in Kotlin?

Q2. What is the lateinit modifier used for?

lateinit modifier in Kotlin is used to delay the initialization of a variable.

The requirement of using lateinit modifier

  • These variables must be declared using var keyword, val is not allowed.
  • Must be either a property inside the body of a class or a top-level property
  • Can only be of non-null type
  • Primitive types disallowed

Declaration Syntax

lateinit var myVar: MyObject

Q3. What is Elvis Expression in Kotlin?

The Elvis operator in Kotlin is an operator that receives two inputs and returns the first argument if it is non-null or the second one otherwise. It is fancily called the null-coalescing operator. It is a variant of the ternary operator but for null-safety checking.


x ?: y // yields `x` if `x` is not null, `y` otherwise.

Q4. For what vararg keyword in Kotlin used for?

Vararg (variable number of arguments) is a parameter in Kotlin where we can pass 'n' number of arguments of a defined datatype or a generic datatype. To simply put, vararg allows a variable number of arguments to be passed to the function. This parameter is normally used as the last parameter of a function.

Q5. What are High Level Functions in Kotlin?

A higher order or high level function is a function that accepts or return another function.

Q6. What are Coroutines?

Coroutines are light weight thread in Kotlin that convert async callbacks for long-running tasks, such as database or network access, into sequential code.

Defining a simple Coroutine

coroutine {
    progress.visibility = View.VISIBLE
    val user = suspended { userService.doLogin(username, password) }
    val currentFriends = suspended { userService.requestCurrentFriends(user) }
    val finalUser = user.copy(friends = currentFriends)
    toast("User ${finalUser.name} has ${finalUser.friends.size} friends")
    progress.visibility = View.GONE

Above is comman structure for a coroutine that have have a coroutine builder, and a set of suspending functions that will suspend the execution of the coroutine until they have the result.

Q7. What is diffrence between Val and Var?

The major difference between var and val in Kotlin is:

A variable declared with var is a mutable variable that value can be changed any time whereas variable declared with val keyword are final their value that not be changed.

Q8. What are different types of constructors available in Kotlin?

Constructors are used to initializing the properties of a class. There are two types of Constructors available in Kotlin.

Primary constructor: It is declared at the class header level.


class User(val name: String, var age: Int) {
    // class body

Secondary constructor: In Kotlin you can create one or more secondary constructors using the "constructor" keyword. Example

class UserProfile {
    constructor(data: String) {
        // some code
    constructor(data: String, numberOfData: Int) {
        // some code

Q9. Why is Kotlin preferred over Java?

KOTLIN IS PREFERRED OVER JAVA because it requires fewer lines of codes to solve the same problem in JAVA. It is safer as it prevents common programming errors by design which may lead to system failures or application crashes. It has better support for functional programming. Here the performance can be made better through inlining, which is not presently possible in JAVA. Kotlin has a variety of handy features that speeds up everyday development tasks that are not provided by JAVA. The compiler of Kotlin runs many checks to prevent runtime errors and thereby reducing the cost and effort of error fixes.

Q10. Explain Kotlin's Null safety?

KOTLIN NULL SAFETY is a feature of its compiler which by default doesn’t allow any types to have a null value at compile type. It is also referred to as The Billion Dollar Mistake, which is to be removed from the code. It is the most common drawback of many programming languages, including JAVA. The access member of a null reference that will result in a null reference exception. In Kotlin by default, variables are non-null references and therefore cannot be set to null. NPE or NULL POINTER EXCEPTION are used for security vulnerabilities, especially for security-related code or processes. It is defensive programming which helps to make sure that our application still functions under unexpected conditions

Q11. What it the main the difference between lazy and lateinit in Kotlin?

Difference between lazy and lateinit in Kotlin

LATEINIT in Property Initialization refers to the late initialization. In this type of property initialization property has not been null or primitive type and must be initialized in the constructor. Here the modifier can only be used on VAR properties declared inside the body of the class and only when it does not have a custom setter or getter. It can be initialized from anywhere the object is seen from. Example

public class Test {

  lateinit var mock: Mock

  @SetUp fun setup() {
     mock = Mock()

  @Test fun test() {

Lazy is lazy initialization. The LAZY () function takes a lambda and returns an instance of lazy that serves as a delegate for implementing a lazy property. It can only be applied to VARS as it cannot be compiled to a final field, thereby no immutability can be guaranteed. They are required to be initialized once and shared by all and is set internally. Example

public class Example{
  val name: String by lazy { "Coursey" }

Q12. How to define Constant in Kotlin.

Val keyword is used to define a Constant in Kotlin.

Syntax for defining a Local Constant in Kotlin.

val firstName: String = "Chike"

Creating a public constant in Kotlin like Java public static final

companion object{

     const val MY_CONSTANT = "Constants"


Q13. What is difference between inline and infix functions in Kotlin?

Difference between inline and infix functions in Kotlin

INLINE FUNCTIONS are used when we want to reduce the memory overhead, but they usually increase the resulting bytecode. In Kotlin, inlining is done with the inline keyword, which affects both the functions and arguments that are lambdas. It is a default setting for the compiler. We have to use a non-inline keyword to make lambda arguments not inlined.

INFIX FUNCTIONS is a particular type of function which needs specific rules to satisfy. It can be invoked through infix notation. This function needs to be attached to a class, either because it is a member or an extension where the single parameter still applies.

Q14. What is suspend in Kotlin?

Suspend is a function in Kotlin Coroutines. A suspending function is simply a function that can be paused and resumed at a later time. They can execute a long-running operation and wait for it to complete without blocking.

Q15. How to create AsyncTask in Android with Kotlin ?

The AsyncTask is used to execute long operations in the background. It is created to be a helper class around Thread and Handler.

The three types used by the AsyncTask are Params, Progress, and Result.

Different methods that are executed with the AsyncTask are PreExecute, doInBackgroud, onProgressUpdate, onPostExecute. Developers need to extend the class to create the AsyncTask. That is, the AyncTask should be subclassed to be used by the developers.

//syntax for creating an AsyncTask
inner class SomeTask extends AsyncTask<Params,Progress,Result>

Q16. Explain Launch vs Async in Kotlin ?

Both Launch and async are coroutines in Kotlin.

Launch: It is used to fire and forget coroutine. It is like starting a new thread. If the code inside the launch terminates with an exception, then it is treated like an uncaught exception in a thread -- usually printed to stderr in backend JVM applications and crashes Android applications.

Async: It is used to start a coroutine that computes some results. The result is represented by an instance of Deferred and you must use await on it. An uncaught exception inside the async code is stored inside the resulting Deferred and is not delivered anywhere else, it will get silently dropped unless processed.

About Author :

  • Author of Kotlin Interview Questions

    OM Prakash

    OM Prakash is Senior Android developer from new Delhi, India. He have rich experience in creating mobiles apps in Android, Ionic, Kotlin, Java. He loves listening old song and watching bolywood movies.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.

Explore Popular Courses on Kotlin

Start working with Landrick that can provide everything you need to generate awareness, drive traffic, connect.