1. What is Kotlin?

Kotlin could be a statically-typed artificial language which runs on the JVM. It will be compiled either using Java ASCII text file and LLVM compiler.

2. Why should we use Kotlin?

  • Kotlin is concise
  • Kotlin is null-safe
  • Kotlin is interoperable

3. Differentiate var and val in kotlin?

Value of var variable can be reassigned it can be changed once assigned it is mutable in nature but variable declared using val keyword can not change its value once assigned it is immutable in nature.

4. Differentiate val and const in kotlin?

Both the variables are immutable in nature we can not reassigned their value. But the value of the const variable can be assigned at the compile-time while the value of the val variable can be assigned at runtime.

5. How to ensure null safety in Kotlin?

One of the most important advantages of using Kotlin is null safety. In Java, if you access some null variable then you’ll get a NullPointerException. So, the below code in Kotlin will produce a compile-time error:

var name: String = “Codinglance”
name = null //error

So, to assign null values to a variable, you would like to declare the name variable as a nullable string and so during the access of this variable, you have to use a secure call operator i.e. ?.

var name: String? = "Codinglance"
print(name?.length) // ok
name = null // ok

6. what’s the difference between safe calls(?.) and null check(!!)?

Safe call operator i.e. ?. is used to check if the value assigned to the variable is null or not. If value is null then null will be returned otherwise it will return the desired value.

var name: String? = “Codinglance”
println(name?.length) // 8
name = null
println(name?.length) // null

If you want to throw NullPointerException when the assigned value of the variable is null, then you have to use the null check or !! operator.

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

7. What is Elvis operator in Kotlin?

In Kotlin, you can easily assign null values to a variable with the help of  null safety property. To check if a value is having null value or not  you can simply use if-else or can use the Elvis operator i.e. ?: For example:

var name:String? = "Codinglance"
val nameLength = name?.length ?: -1

The Elvis operator(?:) in this example will return the length of name variable if the value is not null otherwise  it will return -1 if the value is null.

8. How can we  convert a Kotlin source file to a Java source file?

To follow these three steps we can simply do this:

  1. Firstly open your Kotlin project in the IntelliJ IDEA / Android Studio.
  2. Go to the Tools > Kotlin > Show Kotlin Bytecode.
  3. Now you have to click on the Decompile button to get your Java code from the bytecode.

9. What is a data class in Kotlin?

Data classes are those classes which are used to store some data. In Kotlin, these class is create with data class. The following is an example of data class:

data class User(val name: String, val age: Int)

In data class we don’t to create functions like we do in java hashCode(), equals(), toString(), copy(). The compiler automatically creates these internally,

10. what’s String Interpolation in Kotlin?

If you wish to use some variable or perform some operation inside a string then String Interpolation are often used. you’ll use the $ sign to use some variable within the string or can perform some operation in between sign.

var name = “Codinglance”
print("Hello! i'm learning from $name")

11. When to use the lateinit keyword in Kotlin?

lateinit is late initialization.it means late initialization means if we don’t want to initialize variable in the constructor instead you want to initialize it later. It is used only with the mutable data type 

12. How can we  check if a lateinit variable has been initialized or not?

We can easily check if the lateinit variable has been initialized or not with the help of isInitialized method. This method returns true if the lateinit property has been initialized or it returns false. For example:

class User {
 lateinit var userName: String

 fun initializeUserName() {
 userName = “Codinglance” 

fun main(args: Array<String>) {

13. Differentiate lateinit and lazy in Kotlin?

lazy keyword used only with val properties, while lateinit keyword used for with var .You can use lazy for val properties only, while lateinit can be used only with var because it is not able to compiled to a final field.

14. What is the difference between == operator and === operator?

The == operator is used to compare the values which is stored in variables and the === operator is used to check if the reference of the variables are equal or not. But in primitive data types, the === operator also checks for the value and not reference.

// primitive data example
val int1 = 10 
val int2 = 10
println(int1 == int2) // true
println(int1 === int2) // true

// wrapper class example
val num1 = Integer(10)
val num2 = Integer(10)
println(num1 == num2) // true
println(num1 === num2) //false

15. What are companion objects in Kotlin?

In Kotlin, we can call a function or any member function without making the instance of the class with the help of companion object for getting this  you have to define function or any variable   as a companion object member within the class. So, by doing this, you can easily access the members and member faction of the class by class name only.

16. what is alternate of Java static methods in Kotlin?

To achieve the functionality just like Java static methods in Kotlin, we are able to use:

  • companion object
  • package-level function
  • object

17. Differentiate List and Array types in Kotlin?

If you have got a listing of information that’s having a set size, then you’ll use an Array. But if the scale of the list can vary, then we’ve to use a mutable list.

18. Can we use the new keyword to instantiate a category object in Kotlin?

No, in Kotlin we do not need to use the new keyword to instantiate a category object. To instantiate a category object, simply we use:

var varName = ClassName()

19. What are visibility modifiers in Kotlin?

A visibility modifier or access specifier or access modifier could be a concept that’s accustomed define the scope of something in an exceedingly artificial language. In Kotlin, we’ve got four visibility modifiers:

  • private: visible inside that specific class or file containing the declaration.
  • protected: visible inside that individual class or file and also within the subclass of that individual class where it’s declared.
  • internal: visible everywhere therein particular module.
  • public: visible to everyone.

20. a way to create a Singleton class in Kotlin?

A singleton class may be a class that’s defined in such some way that only 1 instance of the category is created and is employed where we’d like just one instance of the category like in logging, database connections, etc. To create a Singleton class in Kotlin, you wish to use the object keyword.

object AnySingletonClassName

Note: you cannot use constructor in object, but you’ll be able to use init.

You may also like...


No Comment.