Kotlin Interview Questions and Answers
Question - 51 : - What do you mean by destructuring in Kotlin?
Answer - 51 : -
Destructuring is a convenient way of extracting multiple values from data stored in(possibly nested) objects and Arrays. It can be used in locations that receive data (such as the left-hand side of an assignment). Sometimes it is convenient to destructure an object into a number of variables, for example:
val (name, age) = developer
Now, we can use name and age independently like below:
println(name)
println(age)
Question - 52 : - When to use the lateinit keyword in Kotlin?
Answer - 52 : -
Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient.
For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class. To handle this case, you can mark the property with the lateinit modifier.
Question - 53 : - How to check if a lateinit variable has been initialized or not?
Answer - 53 : -
You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized method. This method will return true if the lateinit property has been initialized otherwise it will return false. For example:
class Person {
lateinit var name: String
fun initializeName() {
println(this::name.isInitialized)
name = "MindOrks" // initializing name
println(this::name.isInitialized)
}
}
fun main(args: Array) {
Person().initializeName()
}
Question - 54 : - What is the difference between lateinit and lazy in Kotlin?
Answer - 54 : -
- lazy can only be used for val properties, whereas lateinit can only be applied to var because it can’t be compiled to a final field, thus no immutability can be guaranteed.
- If you want your property to be initialized from outside in a way probably unknown beforehand, use lateinit.
Question - 55 : - Is there any difference between == operator and === operator?
Answer - 55 : -
Yes. The == operator is used to compare the values stored in variables and the === operator is used to check if the reference of the variables are equal or not. But in the case of primitive types, the === operator also checks for the value and not reference.
// primitive example
val int1 = 10
val int2 = 10
println(int1 == int2) // true
println(int1 === int2) // true
// wrapper example
val num1 = Integer(10)
val num2 = Integer(10)
println(num1 == num2) // true
println(num1 === num2) //false
Question - 56 : - What is the forEach in Kotlin?
Answer - 56 : -
In Kotlin, to use the functionality of a for-each loop just like in Java, we use a forEach function. The following is an example of the same:
var listOfMindOrks = listOf("mindorks.com", "blog.mindorks.com", "afteracademy.com")
listOfMindOrks.forEach {
Log.d(TAG,it)
}
Question - 57 : - What are companion objects in Kotlin?
Answer - 57 : -
In Kotlin, if you want to write a function or any member of the class that can be called without having the instance of the class then you can write the same as a member of a companion object inside the class.
To create a companion object, you need to add the companion keyword in front of the object declaration.
The following is an example of a companion object in Kotlin:
class ToBeCalled {
companion object Test {
fun callMe() = println("You are calling me :)")
}
}
fun main(args: Array) {
ToBeCalled.callMe()
}
Question - 58 : - What is the equivalent of Java static methods in Kotlin?
Answer - 58 : -
To achieve the functionality similar to Java static methods in Kotlin, we can use:
- companion object
- package-level function
- object
Question - 59 : - What is the difference between FlatMap and Map in Kotlin?
Answer - 59 : -
- FlatMap is used to combine all the items of lists into one list.
- Map is used to transform a list based on certain conditions.
Question - 60 : - What is the difference between List and Array types in Kotlin?
Answer - 60 : -
If you have a list of data that is having a fixed size, then you can use an Array. But if the size of the list can vary, then we have to use a mutable list.