• +91 9971497814
  • info@interviewmaterial.com

Kotlin Interview Questions Answers

Question 1 : How does Kotlin work on Android?

Answer 1 :

Just like Java, the Kotlin code is also compiled into the Java bytecode and is executed at runtime by the Java Virtual Machine i.e. JVM. When a Kotlin file named Main.kt is compiled then it will eventually turn into a class and then the bytecode of the class will be generated. The name of the bytecode file will be MainKt.class and this file will be executed by the JVM.

Question 2 : Why should we use Kotlin?

Answer 2 :

  1. Kotlin is concise
  2. Kotlin is null-safe
  3. Kotlin is interoperable

Question 3 : What is the difference between the variable declaration with var and val?

Answer 3 :

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 4 : What is the difference between the variable declaration with val and const?

Answer 4 :

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 5 : How to ensure null safety in Kotlin?

Answer 5 :

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 6 : What is the difference between safe calls(?.) and null check(!!)?

Answer 6 :

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 7 : Do we have a ternary operator in Kotlin just like java?

Answer 7 :

No, we don't have a ternary operator in Kotlin but you can use the functionality of ternary operator by using if-else or Elvis operator.

Question 8 : What is Elvis operator in Kotlin?

Answer 8 :

In Kotlin, you can assign null values to a variable by using the null safety property. To check if a value is having null value then you can use if-else or can use the Elvis operator i.e. ?: For example:

var name:String? = "Mindorks"
val nameLength = name?.length ?: -1
println(nameLength)

The Elvis operator(?:) used above will return the length of name if the value is not null otherwise if the value is null, then it will return -1.

Question 9 : How to convert a Kotlin source file to a Java source file?

Answer 9 :

Steps to convert your Kotlin source file to Java source file:

  1. Open your Kotlin project in the IntelliJ IDEA / Android Studio.
  2. Then navigate to Tools > Kotlin > Show Kotlin Bytecode.
  3. Now click on the Decompile button to get your Java code from the bytecode.

Question 10 : What is the use of @JvmStatic, @JvmOverloads, and @JvmFiled in Kotlin?

Answer 10 :

  1. @JvmStatic: This annotation is used to tell the compiler that the method is a static method and can be used in Java code.
  2. @JvmOverloads: To use the default values passed as an argument in Kotlin code from the Java code, we need to use the @JvmOverloads annotation.
  3. @JvmField: To access the fields of a Kotlin class from Java code without using any getters and setters, we need to use the @JvmField in the Kotlin code.


Selected

 

Kotlin Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners