Kotlin Interview Questions For Freshers & Experienced

  • Question 1) What is Kotlin?
  • Question 2) Enlist major features of Kotlin?
  • Question 3) Why Kotlin is preffered over Java?
  • Question 4) Enlist different types of constructors available in Kotlin?
  • Question 5) What is difference between val and var in Kotlin?
  • Question 6) What is Null Safety in Kotlin?
  • Question 7) What are high order functions in Kotlin?
  • Question 8) What is use of data class in Kotlin?
  • Question 9) What is the Elvis Operator in Kotlin?
  • Question 10) What is init block in Kotlin?
  • Question 11) What are kotlin coroutines?
  • Sharad Jaiswal
  • 22nd Dec, 2019
  • 733 Followers

Kotlin Interview Questions
Updated ON

22nd Dec, 2019

No. of Questions

11

Posted In

Android

Author

Sharad Jaiswal

What is Kotlin?

Kotlin is open-sourced and focuses on security, clarity, tooling support, and interoperability. Kotlin has been designed to be able to fully interoperable with Java. It relaxes Java’s constraints of permitting variables and methods to subsist in a class framework.

Kotlin also complies with native code or JavaScript. It also aids JVM and androids. For communion with Java, it offers a Jvm Name comment that states a class denomination when a package is observed in the Java project.

Key Features of Kotlin

Below are few major features of Kotlin

Open source

Full Java Interoperability

Null Safety

Write less code

Clean and compact syntax

Immutability

Operator overloading

Cross Platform

Easy to Maintain

Q1. What is Kotlin?

Kotlin is a relatively new programming language developed by the team behind JetBrains as an alternative to Java. Mainly used in the development of Android applications, Kotlin is a statically typed, general-purpose programming language with type inference. More streamlined and easy to interpret than Java, Kotlin mainly targets the JVM but also compiles to JavaScript or native code. Officially supported by Google for Android development, Koltlin has its compiler as an alternative to the standard Java compiler in the Android studio.

Q2. Enlist major features of Kotlin?

Kotlin was developed to make it feature-rich in mind, so there are many features that makes Kotlin a unique programming language.

Below are some major Features of Kotlin Programming Language:

  • Extension functions: In Kotlin, you can add some extra functionality to the existing component to make it much more versatile.
  • Higher-Order Function: You can pass a function as an argument to a method or return a function as a result of the method in Kotlin.
  • Smart Cast: Smart Cast checks for some type, and then it will allow to perform all the operation allowed for that particular type.
  • Destruction Declaration: With this, it is possible to return more than one value from a function.
  • Default and Named arguments: You can assign a default value to the parameter whom you want to make optional, and while calling that method, there is no bound on giving values to the default parameters. You can easily change the sequence of the parameter at the time of calling with Named arguments. It is done by writing the name of the argument and then assigning the value to it.

Q3. Why Kotlin is preffered over Java?

The code written in Kotlin is much more concise than Java. That is, you can solve the same problem with fewer lines of code in Kotlin. By default, Kotlin has better support for functional programming than Java, and Kotlin eliminates a big drawback in Java, the null pointer exception. It is also easy to learn, less prone to errors, and fully interoperable with Java. It also removes tons of boilerplate code that comes with Java and saves space and typing effort. For these reasons, Kotlin is preferred over Java.

Q4. Enlist different types of constructors available in Kotlin?

There are two types of constructors in Kotlin:

  • Primary Constructor
  • Secondary Constructor

Primary Constructor It initializes the class and is declared at the class header. Surrounded by parenthesis, it can have optional parameters.

class className(valname: String,varid: Int) { // body of the class }

Secondary Constructor The Secondary constructor can be created one or more in class. It is created using the "constructor" keyword.

class className{ constructor(id: Int){ //code } constructor(name: String){ //code } }

Q5. What is difference between val and var in Kotlin?

They are both used to declare a variable.

The variables declared as var can be assigned multiple times. It can be read and write. So, it is called a mutable variable.

var y: String = "my String" // y can be reassigned.

The variables declared as val is a constant one and cannot be changed or assigned multiple times. It is initialized only once and can be read-only. It is called an Immutable variable.

val y: Double = 21.5 // y is constant.

Q6. What is Null Safety in Kotlin?

Kotlin is null safety that is, it eliminates the jeopardy of null reference from the code. NullPointerExceptions are thrown by the program at runtime and sometimes cause application failure or system crashes. In Kotlin, the type system differentiates between references that can hold null (nullable references) and those that cannot (non-null references).

var c: String = "abc"
c = null // Null exception error cause the String cannot hold null.
you can add "?" to the type to decalre a nullable variable
var c: String? = "abc"
c= null // ok

Q7. What are high order functions in Kotlin?

Kotlin supports higher-order function, that is functions can be passed as an argument to other functions or function can be returned as a value by a function. It is possible because Kotlin functions can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions.

Functions can be declared as Lambda statements and can be passed or returned.
      // lambda expression 
var lambda = {println("higher order function") } 
      // higher-order function 
fun higherfunc( lmbd: () -> Unit ) {    // accepting lambda as parameter 
   //statemnts
} 
fun main(args: Array) { 
     //invoke higher-order function 
    higherfunc(lambda)       // passing lambda as parameter 
}  

Q8. What is use of data class in Kotlin?

A data class is a concept to represent, encapsulate, and move data around. A data class has fields as data and the getter & setter method for accessing the data. A data class in Kotlin introduces the "data" keyword and it automatically creates getter and setter properties when we declare data.

data class User(var name: String, var age: Int) // A data class in Kotlin

Getter and Setter property are automatically declared for var name and var age. data class also implements toString(), equals(), and hashcode() functions automatically. Along with it, it creates a function for every property (destructing declarations) and copy function for copying classes.

Q9. What is the Elvis Operator in Kotlin?

As you may know, Kotlin removes the dreaded NULL POINTER error. It is possible because of the Elvis operator. It receives two inputs and returns the first one if it is non-null or the second one.

The syntax of the Elvis operator is ?:
fun elvis(arg : String?) {	   
val value = arg ?: " "	   
}

In the above code, value is assigned with arg if it is non-null or it is assigned with empty space(" "). In a sense, it is a variant of ternary operator. Elvis operator in conjunction with safe call operator(?.) can be used to access nullable properties or methods. However, the resulting type will be nullable.

Q10. What is init block in Kotlin?

init block is a group of code declared with init operator, which is executed as soon as the class gets instantiated. It acts as a constructor and the block is executed every time the class gets instantiated.

init{
        print("Class instance is initialized.") //gets executed when the object is created.
    }

However, the priority for the init block is less than the primary constructor. So, if there is a primary constructor in a class, it gets executed first and then the init block. A class can have multiple init blocks in it.

Q11. What are kotlin coroutines?

Kotlin's coroutines are lightweight threads. It provides a good way to write asynchronous code that is perfectly readable and maintainable. Kotlin provides only minimal low-level APIs in its standard library to enable various other libraries to utilize coroutines.

About Author :

  • Author of Kotlin Interview Questions

    Sharad Jaiswal

    Sharad Jaiswal is Sr. Web Developer from Noida area. He have rich experience in PHP, Angular Js, React, javascript and Node .If you any query or project on these programming you can drop your enquiry in comment section.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.