• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

Question - 11 : - How can you ensure null safety in Kotlin?

Answer - 11 : -

Null safety is a feature introduced in Kotlin. 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 overcome this issue, you have to assign null values to a variable, and 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 - 12 : - What is a data class in Kotlin?

Answer - 12 : -

In Kotlin, a data class is a class whose main purpose is to hold data. It is marked as "data".

Syntax:

data class User(val name: String, val age: Int)   
The data classes must have to fulfill the following requirements to ensure consistency and meaningful behavior of the generated code:

  • The primary constructor must have at least one parameter
  • , and all primary constructor parameters need to be marked as val or var.
  • Data classes cannot be abstract, open, sealed, or inner.

Question - 13 : - What is the default behavior of Kotlin classes?

Answer - 13 : -

By default, all classes are final in Kotlin. That's because Kotlin allows multiple inheritances for classes, and an open class is more expensive than a final class.

Question - 14 : - Does Kotlin provide support for primitive Datatypes?

Answer - 14 : -

No. Kotlin does not provide support for primitive Data types like in Java.

Question - 15 : - Does Kotlin provide support for macros?

Answer - 15 : -

No. Kotlin does not provide support for macros because the developers of Kotlin find it difficult to include them in the language.

Question - 16 : - What is the use of the open keyword in Kotlin?

Answer - 16 : -

In Kotlin, the classes and functions are final by default. So, it is not possible to inherit the class or override the functions. To achieve this, we need to use the open keyword before the class and function.

Question - 17 : - What do you understand by the Ranges operator in Kotlin?

Answer - 17 : -

Ranges operators help to iterate within a range. Its operator form is (..) For Example:

for (i in 1..15)  
print(i)  
The above example will give the output to print from 1 to 15.

Question - 18 : - Where should we use var and where val in Kotlin?

Answer - 18 : -

In Kotlin, var is used where value is frequently changing. For example, while getting the location of the android device:

var integerVariable : Int? = null  
In Kotlin, val is used where there is no change in value in the whole class. For example, when you want to set textview or button's text programmatically:

val stringVariables : String = "Button's Constant or final Text"  

Question - 19 : - What is the difference between a safe calls(?.) and a null check(!!) in Kotlin?

Answer - 19 : -

Difference between safe calls(?.) and a null check(!!) in Kotlin:

The safe call operator i.e. ?. is used to check if the variable's value is null or not. If it is null, then null will be returned otherwise it will return the desired value.

var name: String? = "JavaTpoint"  
println(name?.length) // 10  
name = null  
println(name?.length) // null  
If you want to throw NullPointerException when the variable's value is null, you can use the null check or !! Operator.

See the example:

var name: String? = "JavaTpoint"  
println(name?.length) // 10  
name = null  
println(name!!.length) // KotlinNullPointerException  

Question - 20 : - What is the basic difference between the fold and reduce in Kotlin? Also, specify when to use which?

Answer - 20 : -

Difference between the fold and reduce in Kotlin:

Fold: The fold takes an initial value and the first invocation of the lambda you pass to it. It will receive that initial value and the first element of the collection as parameters.

listOf(1, 2, 3).fold(0) { sum, element -> sum + element }  
The first call to the lambda will be with parameters 0 and 1. The ability to pass in an initial value is useful if you have to provide a default value or parameter for your operation.

Reduce: The "reduce" doesn't take an initial value. Instead, it starts with the first element of the collection as the accumulator.

listOf(1, 2, 3).reduce { sum, element -> sum + element }  
In the above example, it is denoted by sum. The first call to the lambda here will be with parameters 1 and 2.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners