Kotlin Interview Questions 2020 – Most Asked Interview Question

Kotlin Interview Questions
Kotlin Interview Questions

Kotlin Interview Questions 2020 – Most Asked Interview Question

Kotlin interview questions Set 1

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

The main function is the entry point of every Kotlin program. In Kotlin we can 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.

#2 What is a data class in Kotlin?

We frequently create classes whose main purpose is to hold data. In Kotlin, this is called a data class and is marked as data:

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

To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements:

  • The primary constructor needs to have at least one parameter;
  • All primary constructor parameters need to be marked as val or var;
  • Data classes cannot be abstract, open, sealed or inner;

#3 What are the advantages of Kotlin over Java? What are the advantages of Kotlin over Java?

Basically, for me less thinking required to write Kotlin equivalent to most java code:

data class

  • java: you have to write getters and setters for each thing, you have to write hashCode properly (or let IDE auto-generate, which you have to do again every time you change the class), toString (same problem as hashcode) and equals (same problem as hashCode). or you could use Lombok, but that comes with some quirky problems of its own. record types are hopefully on the way.
  • Kotlin: data class does it all for you.

getter and setter patterns

  • java: rewrite the getter and setter for each variable you use it for
  • Kotlin: don’t have to write getter and setter, and custom getter and setter take a lot less typing in Kotlin if you do want to. also delegates exist for identical getters\setters

abstract vs open classes

  • java: you have to make an abstract class implementation
  • Kotlin: open class lets you make an inheritable class while also being usable itself. nice mix of the interface and regular class IMO

extension functions

  • java: doesn’t exist
  • Kotlin: does exist, makes functions more clear in usage, and feels more natural.


  • java: Anything but primitives can be null at any time.
  • Kotlin: you get to decide what can and can’t be null. allows for nice things like inline class


  • java: Memorize singleton pattern
  • Kotlin: object instead of class


  • java: They’re alright, nothing fancy
  • Kotlin: Reified generics (you can access the actual type), in and out for covariance

named parameters

  • java: does not exist, easy to break API back-compatibility if you aren’t careful.
  • Kotlin: does exist, easy to preserve API back-compatibility.

primary constructor

  • java: does not have perse, you still have to define everything inside the class
  • Kotlin: very nice to be able to quickly write a constructor without any constructor function or extra needless declarations

#4 What is a primary constructor in Kotlin?

The primary constructor is part of the class header. Unlike Java, you don’t need to declare a constructor in the body of the class. Here’s an example:

 class Person(val firstName: String, var age: Int) {     
// class body

The main idea is by removing the constructor keyword, our code gets simplified and easy to understand.

#5 Explain the Null safety in Kotlin

Kotlin’s type system is aimed at eliminating the danger of null references from code, also known as The Billion Dollar Mistake.

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Java, this would be the equivalent of a NullPointerException or NPE for short.

In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references). For example, a regular variable of type String can not hold null:

 var a: String = "abc"
a = null // compilation error

To allow nulls, we can declare a variable as a nullable string, written String?:

var b: String? = "abc"  
b = null // ok

#6 What is the difference between suspending vs. blocking? What is the difference between suspending vs. blocking?

  • blocking call to a function means that a call to any other function, from the same thread, will halt the parent’s execution. Following up, this means that if you make a blocking call on the main thread’s execution, you effectively freeze the UI. Until that blocking calls finishes, the user will see a static screen, which is not a good thing.
  • Suspending doesn’t necessarily block your parent function’s execution. If you call a suspending function in some thread, you can easily push that function to a different thread. In case it is a heavy operation, it won’t block the main thread. If the suspending function has to suspend, it will simply pause its execution. This way you free up its thread for other work. Once it’s done suspending, it will get the next free thread from the pool, to finish its work. This is an important Kotlin Interview Questions

#7 What is the equivalent of Java static methods in Kotlin? What is the equivalent of Java static methods in Kotlin?

Place the function in the companion object.

 class Foo{  
public static int a() { return1 ; }

will become:

class Foo{  
funa(): Int =1
// to run

Another way is to solve most of the needs for static functions with package-level functions. They are simply declared outside a class in a source code file. The package of a file can be specified at the beginning of a file with the package keyword. Under the hood, these “top-level” or “package” functions are actually compiled into their own class. In the above example, the compiler would create a class FooPackage with all of the top-level properties and functions, and route all of your references to them appropriately.


package foo
fun bar() = {}


import foo.bar


#8 Explain lazy initialization in Kotlin

lazy means lazy initialization. Your variable will not be initialized unless you use that variable in your code. It will be initialized only once after that we always use the same value. This is an important Kotlin Interview Questions

lazy() is a function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property: the first call toget()executes the lambda passed tolazy()and remembers the result, subsequent calls to get()simply return the remembered result.

 val test: String by lazy { 
    val testString = "some value"

#9 What is the difference between List and Array types? What is the difference between List and Array types?

The major difference from the user side is that Array has a fixed size while(Mutable)Listcan adjust their size dynamically. Moreover, Array is mutable whereas List is not.

Furthermore kotlin.collections.List is an interface implemented among others by java.util.ArrayList. It’s also extended bykotlin.collections.MutableListto be used when a collection that allows for item modification is needed.

On the JVM level Array is represented by arrays. List on the other hand, is represented by java.util.List since there are no immutable collections equivalents available in Java.

#10 What’s the difference between val and var declaration? How to convert a String to an Int?

val variables cannot be changed. They’re like final modifiers in Java. A var can be reassigned. The reassigned value must be of the same data type.

fun main(args:
Array<String>) {
val s: String = “Hi”
var x = 5
x = “6”.toInt()

We use the toInt() method to convert the String to an Int.

#11 Explain what is wrong with that code? Explain what is wrong with that code?

Why is this code wrong?

class Student (var name: String) {
init() {
println("Student has got a name as $name")
constructor(sectionName: String, var id: Int) this(sectionName) { }


The property of the class can’t be declared inside the secondary constructor. This will give an error because here we are declaring a property id of the class in the secondary constructor, which is not allowed.

If you want to use some property inside the secondary constructor, then declare the property inside the class and use it in the secondary constructor:

class Student (var name: String) {
var id: Int = -1 init() { println("Student has got a name as $name")

#12 Which type of Programming does Kotlin support?

Kotlin supports only two types of programming, and they are:

  • Procedural programming
  • Object-oriented programming

#13 State the advantages and disadvantages of Kotlin?


Kotlin is simple and easy to learn as its syntax is similar to that of Java.

It is the functional language that is based on JVM (Java Virtual Machine), which removes the boilerplate codes. Upon all this, Kotlin is considered as an expressive language that is easily readable and understandable and the performance is substantially good.
It can be used by any desktop, web server, or mobile-based applications.


Kotlin does not provide static modifier, which causes problems for conventional java developers.

In Kotlin, the function declaration can be done in many places in the application, which creates trouble for the developer to understand which function is being called.

Also according to docs, what Java has that Kotlin does not:

  • Checked exceptions
  • Primitive types that are not classes
  • Static members
  • Non-private fields
  • Wildcard-types
  • Ternary-operator a? b: c

#14 What is the Kotlin double-bang (!!) operator

The not-null assertion operator !! converts any value to a non-null type and throws a KotlinNullPointerException exception if the value is null.


fun main(args: Array) {
var email: String?
email = null

This operator should be used in cases where the developer is guaranteeing – it allows you to be 100% sure that its value is not null.

#15 What is a difference between a class and an object in Kotlin? An object is a singleton. You do not need to create an instance to use it.

  • class needs to be instantiated to be used.

The primary use case of object in Kotlin is because Kotlin tries to do away with static, and primitives, leaving us with a purely object-oriented language. Kotlin still uses static and primitives underneath the hood, but it discourages devs to use those concepts any more. Instead, now Kotlin replaces static with singleton object instances. Where you would previously use a static field in Java, in Kotlin you will now create an object, and put that field in the object.

#16 How to convert List to Map in Kotlin?

You have two choices:

  • The first and most performant is to use associateBy a function that takes two lambdas for generating the key and value, and inlines the creation of the map:
val map = friends.associateBy({it.facebookId}, {it.points})
  • The second, less performant, is to use the standard map function to create a list of Pair which can be used by toMap to generate the final map:
val map = friends.map { it.facebookId to it.points }.toMap()

#17 Imagine you moving your code from Java to Kotlin. How would you rewrite this code in Kotlin?


 public class Foo { 
    private static final Logger LOG = LoggerFactory.getLogger(Foo.class);

Answer: Use Static-like approach:

class MyClass {
companion object {
val LOG = Logger.getLogger(MyClass::class.java.name)
fun foo() {
LOG.warning("Hello from MyClass")

#18 What is the difference between “*” and “Any” in Kotlin generics?


  • List<*> can contain objects of any type, but only that type, so it can contain Strings (but only Strings)
  • while List<Any> can contain Strings and Integers and whatnot, all in the same list

#19 What is the difference between “open” and “public” in Kotlin? 

  • The open keyword means “open for extension“. The open annotation on a class is the opposite of Java’s final: it allows others to inherit from this class.
  • If you do not specify any visibility modifier, the public is used by default, which means that your declarations will be visible everywhere. the public is the default if nothing else is specified explicitly.

#20 How to create a singleton in Kotlin?

Just use object.

object SomeSingleton

The above Kotlin object will be compiled to the following equivalent Java code:

public final class SomeSingleton {
public static final SomeSingleton INSTANCE;
private SomeSingleton() {
INSTANCE = (SomeSingleton)this;
System.out.println("init complete");
static {
new SomeSingleton();

This is the preferred way to implement singletons on a JVM because it enables thread-safe lazy initialization without having to rely on a locking algorithm like the complex double-checked locking. This is an important Kotlin Interview Questions

#21 What is The Billion Dollar Mistake?

Kotlin’s type system is aimed at eliminating the danger of null references from code, also known as The Billion Dollar Mistake.

One of the most common pitfalls in many programming languages, including Java, is that accessing a member of a null reference will result in a null reference exception. In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that can not (non-null references).

I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

Tony Hoare at QCon London in 2009 https://en.wikipedia.org/wiki/Tony_Hoare

These were the Kotlin interview questions Stay tuned to learn more.

Check it out Latest Jobs for Kotlin Developer: Click here

If You Want To Get More Daily Such Jobs Updates, Career Advice Then Join the Telegram Group From Given Link And Never Miss Update.

Join Telegram Group of Daily Jobs Updates for 2010-2021 Batch: Click Here

Why You’re Not Getting Response From Recruiter?: Click here

How To Get a Job Easily: Professional Advice For Job Seekers: Click here

Cognizant Latest News: Up To 20K+ Employees Will Be Hired: Click here

COVID-19 Live Tracker India & Coronavirus Live Update: Click here

Why Remove China Apps took down from Play store?: Click here

Feel Like Demotivated? Check Out our Motivation For You: Click here

List of Best Sites To Watch Free Movies Online in 2020: Click here

5 Proven Tips For How To Look Beautiful and Attractive: Click here