Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Kotlin Interview Questions and Answers

Test your skills through the online practice test: Kotlin Quiz Online Practice Test

Related differences

Java vs Kotlin

Ques 21. Explain lazy initialization in the context of Kotlin.

There are some classes whose object initialization is so time-consuming that it causes the entire class creation process to be delayed. Lazy initialisation helps in such problems. When we declare an object using lazy initialisation, the object is initialised only once when the object is used. If the object is not used throughout, the object is not initialised. This makes the code more efficient and faster. 

For example, let us imagine you have a SlowClass class and you require an object of that SlowClass in a different class called FastClass:

// KOTLIN
class FastClass {
private val slowObject: SlowClass = SlowClass()
}

We are generating a large object here, which will cause the development of the FastClass to be slow or delayed. There may be times where the SlowClass object isn\\\'t required. As a result, the lazy keyword can assist you in this situation:

class FastClass {   
private val slowObject: SlowClass by lazy {
SlowClass()
}
}

For example,

// KOTLIN
class FastClass {
private val slowObject: SlowClass by lazy {
println("Slow Object initialised")
SlowClass()

fun access() {
println(slowObject)
}
}
fun main(args: Array) {
val fastClass = FastClass()
println("FastClass initialised")
fastClass.access()
fastClass.access()
}

Output:-

FastClass initialised 
Slow Object initialised 
SlowClass@2b12fkk7 
SlowClass@2b12fkk7

Explanation:- In the above code, we have instantiated an object of the SlowClass inside the class structure of the FastClass using lazy initialisation. The object of the SlowClass is generated only when it is accessed in the above code, that is, when we call the access() method of the FastClass object and the same object is present throughout the main() method.

Is it helpful? Add Comment View Comments
 

Ques 22. Differentiate between lateinit and lazy initialisation. Explain the cases when you should use lateinit and when you should use lazy initialisation.

lateinitlazy initialisation
The main purpose is to delay the initialisation to a later point in time.The main purpose is to initialise an object only when it is used at a later point in time. Also, a single copy of the object is maintained throughout the program. 
It\'s possible to initialise the object from anywhere in the program.Only the initializer lambda can be used to initialise it.
Multiple initializations are possible in this case.Only a single initialisation is possible in this case.
It\'s not thread-safe. In a multi-threaded system, it is up to the user to correctly initialise.Thread-safety is enabled by default, ensuring that the initializer is only called once.
It works only with var.It works only with val.
The isInitialized method is added to verify if the value has previously been initialised.It is impossible to uninitialize a property.
Properties of primitive types are not allowedAllowable on primitive type properties.

Is it helpful? Add Comment View Comments
 

Ques 23. What do you understand about coroutines in the context of Kotlin?

Unlike many other languages with equivalent capabilities, async and await are neither keywords nor part of Kotlin's standard library. JetBrains' kotlinx.coroutines library is a comprehensive library for coroutines. It includes a number of high-level coroutine-enabled primitives, such as launch and async. Kotlin Coroutines provide an API for writing asynchronous code in a sequential manner.

Coroutines are similar to thin threads. Coroutines are lightweight since they don't allocate new threads when they're created. Instead, they employ pre-defined thread pools as well as intelligent scheduling. The process of deciding which piece of work you will do next is known as scheduling. Coroutines can also be paused and resumed in the middle of their execution. This means you can have a long-term project that you can work on incrementally. You can pause it as many times as you want and continue it whenever you're ready.

Is it helpful? Add Comment View Comments
 

Ques 24. Explain scope functions in the context of Kotlin. What are the different types of Scope functions available in Kotlin?

The Kotlin standard library includes numerous functions that aid in the execution of a block of code within the context of an object. When you use a lambda expression to call these functions on an object, temporary scope is created. These functions are referred to as Scope functions. The object of these functions can be accessed without knowing its name. Scope functions make code more clear, legible, and succinct, which are key qualities of the Kotlin programming language.

Following are the different types of Scope functions available in Kotlin:-

  • let:- 
    Context object:   it 
    Return value:   lambda result
    The let function is frequently used for null safety calls. For null safety, use the safe call operator(?.) with ‘let'. It only runs the block with a non-null value.
  • apply:-
    Context object:  this
    Return value:   context object
    “Apply these to the object,” as the name suggests. It can be used to operate on receiver object members, primarily to initialise them.
  • with:-
    Context object:  this
    Return value:   lambda result
    When calling functions on context objects without supplying the lambda result, ‘with' is recommended.
  • run:-
    Context object:  this 
    Return value:   lambda result
    The ‘run' function is a combination of the ‘let' and ‘with' functions. When the object lambda involves both initialization and computation of the return value, this is the method to use. We can use run to make null safety calls as well as other calculations.
  • also:-
    Context object:  it
    Return value:   context object
    It's used when we need to do additional operations after the object members have been initialised.

Is it helpful? Add Comment View Comments
 

Ques 25. Explain suspend function in the context of Kotlin.

A function that may be started, halted, then resumed is known as a suspend function. One of the most important things to remember about the suspend functions is that they can only be invoked from another suspend function or from a coroutine. Suspending functions are merely standard Kotlin functions with the suspend modifier added, indicating that they can suspend coroutine execution without blocking the current thread. This means that the code you're looking at may pause execution when it calls a suspending function and restart execution at a later time. However, it makes no mention of what will happen to the present thread in the meantime.

Suspending functions can call any other ordinary functions, but another suspending function is required to suspend the execution. Because a suspending function cannot be called from a regular function, numerous coroutine builders are supplied, allowing you to call a suspending function from a non-suspending scope like launch, async, or runBlocking.

delay() function is an example of suspend function.

Is it helpful? Add Comment View Comments
 

Most helpful rated by users:

©2024 WithoutBook