Kotlin Interview Questions and Answer 2020

  • By
  • December 27, 2019
  • Kotlin
Kotlin interview Question and answer 2020

Interview Questions on Kotlin

The new age Kotlin is  latest JVM programming language from the JetBrains. Kotlin was made the official language for Android Development along with Java by Google. Developers say it addresses the issues faced in Java programming

Below are Kotlin Interview Questions and Answers that will help you in your Kotlin Interviews. The Kotlin interview questions given below are good for beginners as well as experienced programmers. Coding questions are also included too to brush up your coding skills.

  1. What’s the Target Platform of Kotlin? How is Kotlin-Java interoperability possible?

Ans: The Target Platform of Kotlin is Java Virtual Machine(JVM) . Kotlin and Java are 100% interoperable since both, on compilation produce bytecode. Thus Kotlin code can be called from Java and vice-versa.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

  1. How do you declare variables in Kotlin? How declaration differ from the Java?

Ans: We have two major differences between Java and Kotlin variable declaration:

The type of declaration

In Java the declaration look like this:

String s = “Java String”;

int x = 10;

In Kotlin the declaration looks like:

val s: String = “Hi”

var x = 5

Declaration in Kotlin, begins with a val and a var followed by the optional type. The type can be automatically detected in Kotlin.

Default value

The following is possible in Java:

String s:

Variable declaration given below is not valid in Kotlin.

val s: String

  1. What’s Null Safety and Nullable Types in Kotlin? What is the Elvis Operator?

Ans: A lot of weight is put behind null safety by Kotlin which is an approach to prevent the dreaded Null Pointer Exceptions by using nullable types which are like String?, Int?, Float? etc. They acts as wrapper type and can hold null values. A nullable value cannot be added to another nullable or basic type of value.

In Kotlin we need to use safe calls to unwrap the Nullable types of basic type. After  unwrapping, if the value is null we can choose to ignore or use a default value instead. To safely unwrap the value from  the Nullable the Elvis Operator is used.

It’s represented as ?: over the nullable type. Right hand side value would be used if the nullable type holds a null.

var str: String?  = “JournalDev.com”

var newStr = str?: “Default Value”

str = null

newStr = str?: “Default Value”

kotlin interview questions null safety elvis operator

  1. What’s a const? How does it differ from a val?

Ans: The properties of val are set at runtime by default.Just by adding a const modifier with val would make the variable compile-time constant.

It is not allowed to use a const with a var or on its own.

On  local variable const is not applicable.

  1. What’s the entry point of every Kotlin Program?

Ans: Function main is the entry point of every Kotlin program. Kotlin gives us the freedom to choose not to write the main function inside the class. On compiling the JVM implicitly encapsulates it in a class.

The strings passed in the form of Array<String> are used to retrieve the command line arguments.

  1. How is !!different from ?. in unwrapping the nullable values? Is there any other way to unwrap nullable values safely?

Ans: !! is used to force unwrap the nullable type to get the value. If null is returned as value, it would lead to a runtime crash. Thus a !! operator is used when you’re absolutely sure that the value will not be null. Otherwise, you’ll get the dreaded null pointer exception. On the other hand, a ?. is Elvis Operator used to have a safe call.

The lambda expression can be used to let on the nullable value to unwrap safely as shown below.

kotlin interview questions let

The let expression of Kotlin is used for a safe call to unwrap the nullable type.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

  1. How is a function declared? Why are Kotlin functions known as top-level functions?

Ans: fun sumOf(a: Int, b: Int): Int{

return a + b

}

A function’s return type is defined after the :

We can declare kotlin functions at the root of the Kotlin file.

  1. What’s the difference between == and === operators in Kotlin?

Ans:

== : Compares the values are equal or not

=== : Check for references if they are equal or not.

List down the visibility modifiers available in Kotlin. What’s the default visibility modifier?

public

internal

protected

private

public is the default visibility modifier.

  1. Does the following inheritance structure compile?

Ans:

class A{

}

class B : A(){

}

  1. By default classes are final in Kotlin. If you want make them non-final, just add the open modifier.

open class A{

}

class B : A(){

}

——————————————————————————————————————-

  1. What are the types of constructors in Kotlin? How are they different? How do you define them in your class?

Ans:

Constructors in Kotlin are of two types:

Primary – Defined in the class headers. They cannot hold any logic. There’s only one primary constructor per class.

Secondary – They’re defined in the class body. Delegation to the primary constructor is must if it exists. They can hold logic. There can be more than one secondary constructors.

class User(name: String, isAdmin: Boolean){

constructor(name: String, isAdmin: Boolean, age: Int) :this(name, isAdmin)

{

this.age = age

}

}

  1. What’s init block in Kotlin

Ans:

init is the initialiser block in Kotlin. It’s executed once the primary constructor is instantiated. if you use secondary constructor, then it works after the primary one as it is composed in the chain.

  1. How does string interpolation work in Kotlin? Explain with a code snippet?

Ans:

String interpolation is used to evaluate string templates.

We use the symbol $ to add variables inside a string.

val n = “sevenmentor.com”

val d = “$n . ${n.length}”

Using {} we can compute an expression too. 

  1. What’s the type of arguments inside a constructor?

Ans:

By default, the constructor arguments areval unless explicitly set to var.

  1. Is new a keyword in Kotlin? How to instantiate a class object in Kotlin

Ans: In Kotlin, new isn’t a keyword.

We can instantiate a class in the following way:

class A

var a = A()

val new = A()

 

  1. What are data classes in Kotlin? What makes them so useful? How are they defined?

Ans:

In Java, to create a class that stores data, you need to set the variables, the getters and the setters, override the toString(), hash() and copy() functions.

In Kotlin we need to add the data keyword on the class and all of the above would automatically be created under the hood.

data class Book(var name: String, var authorName: String)

fun main(args: Array<String>) {

val book = Book(“Kotlin Tutorials”,”Anupam”)

}

Therefore, data classes will save us with lot of code.

It creates component functions such as component1().. componentN() for each of the variables.

 

  1. What are destructuring declarations in Kotlin? Explain it with an example.

Ans:

Destructuring Declarations is a smart way to assign multiple values to variables from data stored in objects/arrays.

Within paratheses, we’ve set the variable declarations. Under the hood, destructuring declarations create component functions for each of the class variables.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

  1. What’s the difference between inline and infix functions? Give an example of each.

Ans:

The function inline  are used to save memory overhead by preventing object allocations for the anonymous functions/lambda expressions called. Instead, it provides that functions body to the function that calls it at runtime. This will increase the bytecode size slightly but will save a lot of memory.

infix functions on the other are used to call functions without parentheses or brackets. This will make, the code looks much more like a natural language.

  1. What’s the difference between lazy and lateinit?

Ans:

Both lazy and lateinit are used to delay the property initializations in Kotlin

The modifier lateinit is used with var and is used to set the value to the var at a later point.

lazy is a method  in Kotlin or we can say lambda expression. It’s set on a val only. The val would be created at runtime when it’s required.

val x: Int by lazy { 10 }

lateinit var y: String

 

  1. Does Kotlin have the static keyword? How to create static methods in Kotlin?

Ans: Kotlin doesn’t have the static keyword.

For the creation of static method in our class we use the companion object.

Following is the Java code:

class A {

public static int returnMe() { return 5; }

}

The equivalent Kotlin code would look like this:

class A {

companion object {

fun a() : Int = 5

}

}

To invoke this we simply do: A.a().

  1. What’s the type of the following Array?

Ans:

val arr = arrayOf(1, 2, 3);

The type is Array<Int>.

  1. Does Kotlin allow us to use primitive types such as int, float, double?

Ans:

It is not possible to use above-mentioned types at langauge level. But compiled JVM bytecode does certainly have them. 

  1. What is the equivalent of switch expression in Kotlin? How does it differ from switch?

Ans:

when is the equivalent of switch in Kotlin.

To represente the default statement in a when the else statement is used.

var num = 10

when (num) {

0..4 -> print(“value is 0”)

5 -> print(“value is 5”)

else -> {

print(“value is in neither of the above.”)

}

}

when statments have a default break statement in them.

  1. How to create Singleton classes?

Ans:

For using singleton pattern for our class we must use the keyword object

An object cannot have a constructor set. It is possible to use the init block inside it though.

Author:

Jagtap, Nikita | SevenMentor Pvt Ltd.

For Free Demo classes Call: 8237077325
Registration Link: Click Here!

Call the Trainer and Book your free demo Class for now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*