• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Kotlin Interview Questions and Answers

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

Answer - 31 : -

Following are the main usage of @JvmStatic, @JvmOverloads, and @JvmFiled in Kotlin:

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

Question - 32 : - What are the names of some extension methods that Kotlin provides to java.io.File?

Answer - 32 : -

Following are some extension methods that Kotlin provides to java.io.File:

  • bufferedReader(): It is used for reading the contents of a file into BufferedReader.
  • readBytes(): It is used for reading the contents of the file to ByteArray.
  • readText(): It is used for reading contents of the file to a single String.
  • forEachLine(): It is used for reading a file line by line in Kotlin.
  • readLines(): It is used for reading lines in the file to List.

Question - 33 : - What do you understand by data classes in Kotlin?

Answer - 33 : -

Data classes are the type of classes that are made to store some data. In Kotlin, it is marked as data. The following is an example of a data class:

data class Developer(val name: String, val age: Int)  
When we mark a class as a data class, we don't have to implement or create the following functions like we have to do in Java: hashCode(), equals(), toString(), copy(). The compiler automatically creates these internally, so it also leads to clean code. Although, there are a few other requirements that data classes need to fulfill.

Question - 34 : - What is the use of Companion Objects in Kotlin?

Answer - 34 : -

Companion Objects are required in Kotlin because Kotlin doesn't have static members or member functions, unlike Java or C#. If we need to write a function that can be called without having a class instance but needs access to the internals of a class, we can write it as a member of a companion object declaration inside that class.

For example:

class EventManager {  
    companion object FirebaseManager {  
    }    
}  
val firebaseManager = EventManager.FirebaseManager   
The companion object is a singleton, and it is a proper object which you can assign to a variable and pass it around. If you integrate with Java code and need a true static member, you can annotate a member inside a companion object with @JvmStatic.

Question - 35 : - How can you handle null exceptions in Kotlin?

Answer - 35 : -

In Kotlin, Elvis Operator is used to handling null expectations.

Question - 36 : - How can we perform String Interpolation in Kotlin?

Answer - 36 : -

In Kotlin, String Interpolation is used when you want to use some variable or perform some operation inside a string. For String Interpolation, we can use the $ sign to use some variable in the string or can perform some operation in between the {} sign.

For example:

var name = "JavaTpoint"  
print("The best tutorial website is: $name")  

Question - 37 : - Name some features which are available in Kotlin but not in Java?

Answer - 37 : -

Following are some important Kotlin features that are not available in Java:

  • Null Safety
  • Operator Overloading
  • Coroutines
  • Range expressions
  • Smart casts
  • Companion Objects

Question - 38 : - What is the difference between == operator and === operator in Kotlin?

Answer - 38 : -

In Kotlin, the == operator is generally 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.

In the case of primitive types, the === operator is also used to check for the value and not reference.

Example:

// 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 - 39 : - Can we use primitive types such as int, double, float in Kotlin?

Answer - 39 : -

Kotlin doesn't support the primitive types so, we can't use primitive types directly in Kotlin. We can use classes like Int, Double, etc., as an object wrapper for primitives. But the compiled bytecode has these primitive types.

Question - 40 : - What do you understand by destructuring in Kotlin?

Answer - 40 : -

In Kotlin, destructuring is a convenient way to extract multiple values from data stored in objects and Arrays. It can be used in locations that receive data. It is used because sometimes, it is convenient to destructure an object into several variables.

For Example:

val (name, age) = developer  
Now, we can use name and age independently as follows:

println(name)  
println(age)  


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners