Kotlin Interview Questions and Answers
Question - 41 : - What are coroutines in Kotlin?
Answer - 41 : -
Unlike many other programming languages with similar capabilities, Kotlin doesn't have async and await keywords, and these keywords are not even part of its standard library.
In Kotlin, kotlinx.coroutines is a rich library for coroutines developed by JetBrains. This library contains some high-level coroutine-enabled primitives, including launch, async, and others. Kotlin Coroutines provide us with an API to write our asynchronous code sequentially.
According to Kotlin documentation, Coroutines are like lightweight threads. They are lightweight because while creating them, they don't allocate new threads. Instead, they use predefined thread pools and smart scheduling. Scheduling is the process of determining the work in a sequential process, and it decides which piece of work you will execute next. We can suspend and resume the Coroutines while execution. This means we can have a long-running task, which can be executed one by one. We can pause it any number of times and resume it when required.
Question - 42 : - What is the difference between Launch and Async in Kotlin Coroutines?
Answer - 42 : -
In Kotlin, the main difference between Launch and Async is that the launch{} does not return anything and the async{} returns an instance of Deferred, which has an await() function. In other words, we can say that launch is used to fire and forget, and async is used to perform a task and return a result.
Question - 43 : - What are the extension functions in Kotlin?
Answer - 43 : -
Extension functions are like extensive properties attached to any class in Kotlin. Extension functions are used to add methods or functionalities to an existing class even without inheriting the class. For example: Suppose, we have views where we need to play with the visibility of the views. So, we can create an extension function for views as follows:
fun View.show() {
this.visibility = View.VISIBLE
}
fun View.hide() {
this.visibility = View.GONE
}
and to use it, we use, like,
toolbar.hide()
Question - 44 : - What do you understand by the Kotlin double-bang (!!) operator?
Answer - 44 : -
The Kotlin double-bang (!!) operator converts any value to a non-null type and throws a KotlinNullPointerException exception if the value is null. It is also called the not-null assertion operator.
Example:
fun main(args: Array) {
var email: String?
email = null
println(email!!)
}
This operator should be used in cases where the developer is 100% sure that its value is not null.
Question - 45 : - What is the difference between the variable declaration with var and val?
Answer - 45 : -
If you want to declare some mutable(changeable) variable, then you can use var. For the immutable variable, use val i.e. val variables can't be changed once assigned.
Question - 46 : - What is the difference between the variable declaration with val and const?
Answer - 46 : -
Both the variables that are declared with val and const are immutable in nature. But the value of the const variable must be known at the compile-time whereas the value of the val variable can be assigned at runtime also.
Question - 47 : - How to ensure null safety in Kotlin?
Answer - 47 : -
One of the major advantages of using Kotlin is null safety. In Java, if you access some null variable then you will get a NullPointerException. So, the following code in Kotlin will produce a compile-time error:
var name: String = "MindOrks"
name = null //error
So, to assign null values to a variable, you need to declare the name variable as a nullable string and then during the access of this variable, you need to use a safe call operator i.e. ?.
var name: String? = "MindOrks"
print(name?.length) // ok
name = null // ok
Question - 48 : - What is the difference between safe calls(?.) and null check(!!)?
Answer - 48 : -
Safe call operator i.e. ?. is used to check if the value of the variable is null or not. If it is null then null will be returned otherwise it will return the desired value.
var name: String? = "MindOrks"
println(name?.length) // 8
name = null
println(name?.length) // null
If you want to throw NullPointerException when the value of the variable is null, then you can use the null check or !! operator.
var name: String? = "MindOrks"
println(name?.length) // 8
name = null
println(name!!.length) // KotlinNullPointerException
Question - 49 : - How to convert a Kotlin source file to a Java source file?
Answer - 49 : -
Steps to convert your Kotlin source file to Java source file:
- Open your Kotlin project in the IntelliJ IDEA / Android Studio.
- Then navigate to Tools > Kotlin > Show Kotlin Bytecode.
- Now click on the Decompile button to get your Java code from the bytecode.
Question - 50 : - What is String Interpolation in Kotlin?
Answer - 50 : -
If you want to use some variable or perform some operation inside a string then String Interpolation can be used. You can use the $ sign to use some variable in the string or can perform some operation in between {} sign.
var name = "MindOrks"
print("Hello! I am learning from $name")