• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

Question - 71 : - What are lambdas expressions?

Answer - 71 : -

Lambdas expressions are anonymous functions that can be treated as values i.e. we can pass the lambdas expressions as arguments to a function return them, or do any other thing we could do with a normal object. For example:

val add : (Int, Int) -> Int = { a, b -> a + b }
val result = add(9, 10)

Question - 72 : - What are Higher-Order functions in Kotlin?

Answer - 72 : -

A higher-order function is a function that takes functions as parameters or returns a function. For example, A function can take functions as parameters.

fun passMeFunction(abc: () -> Unit) {
 // I can take function
 // do something here
 // execute the function
 abc()
}
For example, A function can return another function.

fun add(a: Int, b: Int): Int {
 return a + b
}
And, we have a function returnMeAddFunction which takes zero parameters and returns a function of the type ((Int, Int) -> Int).

fun returnMeAddFunction(): ((Int, Int) -> Int) {
 // can do something and return function as well
 // returning function
 return ::add
}
And to call the above function, we can do:

val add = returnMeAddFunction()
val result = add(2, 2)

Question - 73 : - What are extension functions in Kotlin?

Answer - 73 : -

Extension functions are like extensive properties attached to any class in Kotlin. By using extension functions, you can add some methods or functionalities to an existing class even without inheriting the class. For example: Let's say, we have views where we need to play with the visibility of the views. So, we can create an extension function for views like,

fun View.show() {
 this.visibility = View.VISIBLE
}

fun View.hide() {
 this.visibility = View.GONE
}
and to use it we use, like,

toolbar.hide()

Question - 74 : - What is an infix function in Kotlin?

Answer - 74 : -

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.

class Operations {
 var x = 10; 
 infix fun minus(num: Int) {
  this.x = this.x - num
 } 
}
fun main() {
 val opr = Operations()
 opr minus 8
 print(opr.x)
}

Question - 75 : - What is an inline function in Kotlin?

Answer - 75 : -

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.

Question - 76 : - What is noinline in Kotlin?

Answer - 76 : -

While using an inline function and want to pass some lambda function and not all lambda function as inline, then you can explicitly tell the compiler which lambda it shouldn't inline.

inline fun doSomethingElse(abc: () -> Unit, noinline xyz: () -> Unit) {
 abc()
 xyz()
}

Question - 77 : - What are Reified types in Kotlin?

Answer - 77 : -

When you are using the concept of Generics to pass some class as a parameter to some function and you need to access the type of that class, then you need to use the reified keyword in Kotlin.

For example:

inline fun genericsExample(value: T) {
 println(value)
 println("Type of T: ${T::class.java}")
}
fun main() {
 genericsExample("Learning Generics!")
 genericsExample(100)
}

Question - 78 : - What is the operator overloading in Kotlin?

Answer - 78 : -

In Kotlin, we can use the same operator to perform various tasks and this is known as operator overloading. To do so, we need to provide a member function or an extension function with a fixed name and operator keyword before the function name because normally also, when we are using some operator then under the hood some function gets called. For example, if you are writing num1+num2, then it gets converted to num1.plus(num2).

For example:

fun main() {
 val bluePen = Pen(inkColor = "Blue")
 bluePen.showInkColor()

 val blackPen = Pen(inkColor = "Black")
 blackPen.showInkColor()

 val blueBlackPen = bluePen + blackPen
 blueBlackPen.showInkColor()
}

 operator fun Pen.plus(otherPen: Pen):Pen{
 val ink = "$inkColor, ${otherPen.inkColor}"
 return Pen(inkColor = ink)
 }

 data class Pen(val inkColor:String){
 fun showInkColor(){ println(inkColor)}
 }

Question - 79 : - What are pair and triple in Kotlin?

Answer - 79 : -

Pair and Triples are used to return two and three values respectively from a function and the returned values can be of the same data type or different.

val pair = Pair("My Age: ", 25)
print(pair.first + pair.second)

Question - 80 : - What are labels in Kotlin?

Answer - 80 : -

Any expression written in Kotlin is called a label. For example, if we are having a for-loop in our Kotlin code then we can name that for-loop expression as a label and will use the label name for the for-loop.

We can create a label by using an identifier followed by the @ sign. For example, name@, loop@, xyz@, etc. The following is an example of a label:

loop@ for (i in 1..10) {
 // some code goes here
}
The name of the above for-loop is loop.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners