Top 25 Kotlin Interview Questions and Answers

1) What is Kotlin?

Kotlin is a statically-typed programming language which runs on the JVM. It can be compiled either using Java source code and LLVM compiler.

2) Who is the developer of Kotlin?

Kotlin was developed by JetBrains.

3) Why you should switch to Kotlin from Java?
Kotlin language is quite simple compared to Java. It reduces may redundancies in code as compared to Java. Kotlin can offer some useful features which are not supported by Java.

4) Tell three most important benefits of using Kotlin?

  1. Kotlin language is easy to learn as its syntax is similar to Java.
  2. Kotlin is a functional language and based on JVM. So, it removes lots of boiler plate
  3. It is an expressive language which makes code readable and understandable.

5) Explain the use of extension functions

Extension functions are beneficial for extending class without the need to inherit from the class.

6) What does ‘Null Safety’ mean in Kotlin?

Null Safety feature allows removing the risk of occurrence of NullPointerException in real time. It is also possible to differentiate between nullable references and non-nullable references.

7) Why is Kotlin interoperable with Java?

Kotlin is interoperable with Java because it uses JVM bytecode.  Compiling it directly to bytecode helps to achieve faster compile time and makes no difference between Java and Kotlin for JVM.

8) Is there any Ternary Conditional Operator in Kotlin like in Java?

No there is no ternary conditional operator in Kotlin language.

9) How can you declare a variable in Kotlin?

value my_var: Char

10) How many constructors are available in Kotlin?
Two types of constructors available in Kotlin are:

  1. Primary constructor
  2. Secondary constructor

11) Can you tell me what kinds of programming types does Kotlin support?

  1. Procedural Programming
  2. OOPS

12) Give me name of the extension methods Kotlin provides to

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

13) How can you handle null exceptions in Kotlin?

Elvis Operator is used for handling null expectations in Kotlin.

14) What are some of the features which are there in Kotlin but not In Java?

Here, are few important Kotlin features that Java doesn’t have:

  1. Null Safety
  2. Operator Overloading
  3. Coroutines
  4. Range expressions
  5. Smart casts
  6. Companion Objects

15) Explain the use of data class in Kotlin?

Data class holds the basic data types. It does not contain any functionality.

16) Can we migrate code from Java to Kotlin?

Yes, JetBrains IDEA provides an inbuilt tool to migrate code from java to Kotlin.

17) Does Kotlin allow macros?

No. Kotlin does not offer support for macros because the developers of Kotlin find it difficult to include it in the language.

18) Tell me the default behavior of Kotlin classes?

In Kotlin all classes are final by default. That’s because Kotlin allows multiple inheritances for classes, and an open class is more expensive than a final class.

19) Does Kotlin support primitive Datatypes?

No, Kotlin does not provide support for primitive Data types like in Java.

20) What is Ranges operator in Kotlin?

Ranges operator helps to iterate through a range. Its operator form is (..)  For Example

It will print from 1 to 15 in output.

21) Can Kotline offer any additional functionality for standard Java packages or standard Java classes?

Kotlin programs can run on standard JVM like any another compiled Java code. It allows JVM to compile any program to byte-code. It is accessible using Java Virtual Machine. Therefore, Kotlin is almost similar to Java. Moreover, Kotlin applications can be built with parts of Java code.

22) Give a syntax for declaring a variable as volatile in Kotlin?

23) What is the use of abstraction in Kotlin?

Abstraction is the most important concept of Objected Oriented Programming. In Kotlin, abstraction class is used when you know what functionalities a class should have. But you are not aware of how the functionality is implemented or if the functionality can be implemented using different methods.

24) How to compare two strings in Kotlin?

Compares string in Kotlin are possible in the following ways:

  1. Using “==” operator:

You can use ah operator for comparison of two string. In Kotlin == operator is used.

  1. Using compareTo() extension function

Syntax of compareTo() function is given below :

Another code example

25) What does this code do?

The code passes lambda function that prints “Guru99!” as an argument into function bar()

0 replies

Leave a Reply

Leave a Reply

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