Kotlin Interview Questions & Answers

1. Explain Higher-Order Functions?
Ans:
Higher-Order Functions: A higher-order function is a function that takes functions as parameters, or returns a function.
2. Explain Functions In Kotlin?
Ans:
Kotlin functions are first-class functions that are easily stored in variables and data structures and can be pass as arguments and returned from other higher-order functions.
Sample function declaration and usage in Kotlin
fun double(x: Int): Int {
return 2 * x
}
val result = double(2)
3. How to convert a String to an Int in Kotlin?
Ans:
toInt() method is used to convert a string value to integer or INT in Kotlin. Below is example uses
fun main(args: Array) {
val s: String = “Kotlin”
var x = 10
x = “8”.toInt()
}
4. What is the difference between declaration variable using val or var in Kotlin?
Ans:
In Kotlin a variable declared using val keyword is cannot be changed. It is similar to the final modifiers in Java whereas the variables declared using var keywords can be reassigned.
5. How to Declare a Variable in Kotlin?
Ans:
In Kotlin, you can declare a variable using var or val which followed by an optional data type.
Variable declaration in Kotlin looks like:
val s: String = “Hi”
var x = 5
6. List the Basic data types of Kotlin?
Ans:
Data types of a constant or variable decide what type of variable it is and how much space is required to store it.
The basic data types in Kotlin are:
Numbers
Characters
Strings
Arrays
Booleans
7. Why is Kotlin preferred over Java?
Ans:
Kotlin eases the coding process as it is simpler than Java and has many features required, that is not provided by Java yet like Extension functions, Null Safety, range expressions etc.
In Kotlin, we code approximately 40% less number of code lines as compared with Java.
8. Where does the Kotlin run and what is the entry point of Kotlin?
Ans:
The Kotlin program once compiled, can run on standard JVM like other programming codes.And, like many other programming languages main() function is the entry point of the Kotlin.
9. What are the different types of constructors in Kotlin?
Ans:
There are two types of constructors in Kotlin:
Primary constructor: It is a section of the Class header and is declared after the class name.
Secondary constructor: This constructor is declared inside the body.
Note: There can be more secondary constructors for a class.
10. Can you execute Kotlin code without JVM?
JVM, which stands for Java Virtual Machine is a feature of Kotlin. This feature compiles a Kotlin code into a native code, which can be done without JVM too.
11. Mention the structural expressions in Kotlin?
Ans:
There are three Structural expressions in Kotlin.They are:
Return: It returns from the nearest enclosing function or anonymous function by default.
Break: This expression terminates the closest enclosing loop.
Continue: This expression proceeds you to the next closest enclosing loop.
12. Explain the data classes in Kotlin?
Ans:
In programming, we use classes to hold data and these classes are called as data classes.
An object can be initialized in the data class and to access the individual parameters of these data classes, we use component functions.
13. What are the modifiers that are available in Kotlin?
Ans:
The modifier in Kotlin provides the developer to customize the declarations as per the requirements. Kotlin provides four modifiers. They are:
Private: This makes the declaration visible only inside the file containing declaration.
Public: It is by default, which means that the declarations will be visible everywhere.
Internal: This makes the declaration visible everywhere in the same modules.
Protected: This keeps the declaration protected and is not available for top-level declarations.
14. Can you migrate the code from Java to Kotlin? If yes how do you do it?
Ans:
Yes, we can migrate the code from Java to Kotlin.This can be done using JetBrains IDEA, which facilitates the conversion of Java code to Kotlin code.
15. State the differences between Val and Var?
Ans:
Val: Val, which is the short form of value, is a constant and it cannot be changed once assigned.
Var: Var, which is the short form of variable, is a storage location that accepts the reassignment of values that have the same data types.
16. Explain Kotlin’s Null safety?
Ans:
In Kotlin, the Null safety is used to eliminate the risk of countering the NullPointer exception in real-time.
17. What are the types of strings available in Kotlin? And, what do you mean by Kotlin String Interpolation?
Ans:
Strings are a collection of characters together.Kotlin features two types of strings, and they are:
Raw string
Escaped string
In Kotlin String, templates can be evaluated.This evaluation of string templates is called as the string template interpolation.
18. State the advantages and disadvantages of Kotlin?
Ans:
Advantages:
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.
Disadvantages:
Kotlin does not provide the static modifier, which causes problems for conventional java developer.
In Kotlin, the function declaration can be done in many places in the application, which creates the trouble for the developer to understand which function is being called.
19. What is Kotlin?
Ans:
It is an open-source programming language that combines object-oriented programming features.
The features like Range Expression, Extension Function, Companion Object, Smart casts, Data classes are considered to be surplus of the Kotlin Language.
20. Which type of Programming does Kotlin support?
Ans:
Kotlin supports only two types of programming, and they are:
Procedural programming
Object-oriented programming
21. Why did you switch to Kotlin from Java ?
Ans:
Kotlin seems to be simpler and cleaner than Java. It removes a lot of redundancies in code from Java. Kotlin also adds some needed features that Java doesn’t yet support, and is making code more idiomatic. Also Kotlin has been added to Android Studio’s list of supported languages recently. So, there is much to expect from Kotlin in easing out the development efforts and good support in future.
22. What are the features you think are there in Kotlin but not in Java?
Ans:
Kotlin has quite a number of features that Java doesn’t. To name some of them, they are
Extension Functions
Null Safety
Smart casts
Range expressions
Operator Overloading
Data classes
Companion Objects
Coroutines
etc.
23. What kinds of programming does Kotlin support?
Ans:
Kotlin supports two types of programming. They are
Procedural Programming
Object-Oriented Programming
Q4 – What is the entry point to a Kotlin program? Provide an example.
Like most of the other procedural languages, main() function is the entry point to a Kotlin program.
An Example for main() function is :
24. How do you think extension functions are useful ? 
Ans:
Extension functions help to extend a class with new functionality without having to inherit from the class. Also, you may use them like an inbuilt function for the class throughout the application.
25. What are Data classes? Aren’t they available in Java?
Ans:
Sometimes we use a class just to hold the data and nothing else. These classes are called Data classes. Of course, this kind of classes could be built using Java, but with the explicit implementation of getter and setter for each of the properties of class. Also, you may need to implement functions like equals, toString and copy separately. What Kotlin does is implementing all these automatically along with special functions called component functions. How cool is that, removing the redundant code bloat.

26. Does Kotlin provide any additional functionalities for standard Java packages or standard Java classes?
Ans:
Ofcourse, Yes. Kotlin uses the concept of extension functions, that we already talked about, to build some useful and more widely used functions among developers directly into the Kotlin library.
27.  Where does this Kotlin run ? Does it have some kind of different runtime environment ?
Ans:
Once compiled, Kotlin programs can run on standard JVM like some other compiled Java code. This means that Kotlin Compiler compiles Kotlin programs to byte-code, which is understood by JVM. So, Kotlin is like a flavor of Java, that goes alongside Java. Interesting fact is that, Kotlin applications can be built with parts of Java code.
28. So, how do you migrate the code from Java to Kotlin ?
Ans:
JetBrains IDEA provides inbuilt tools to convert Java code to Kotlin code. Then you may do the magic offered by Kotlin at some of the parts in code, to make it clean.
29. OK. Is there something called init block in Kotlin ?
Ans:
Yes.
30. What does init block do and Where does it appear in a class ?
Ans:
Instructions in the init block are executed right after Primary Constructor’s execution. init block goes in a class along with secondary constructors as a method.
Reference – Kotlin Init
31. How many types of constructors are there ? What are they ?
Ans:
There are two types of constructors. They are Primary Constructors and Secondary Constructors.
32. How are Primary Constructors different from Secondary Constructors ?
Ans:
Primary Constructors are declared intrinsically with class definition. Secondary Constructors are declared exclusively inside the class body.
In the following example, in the first line, the constructor keyword along with the variables declared right after it is the Primary Constructor. Inside the class body, we have another constructor, and this is Secondary Constructor.
Example of class with Primary and Secondary Constructors
class Person constructor(var name: String, var age: Int){
var profession: String = “Not Mentioned”

constructor (name: String, age: Int, profession: String): this(name,age){
this.profession = profession
}
}
33. Is there any dependency of Secondary Constructors on Primary Constructors ?
Ans:
Yes. Secondary Constructor has to make an exclusive call to Primary Constructor or other Secondary Constructor, which of course calls the Primary Constructor. Following is an example, and here the Secondary Constructor makes call to Primary Constructor using this(name, age).
Example of class with Primary and Secondary Constructors
class Person constructor(var name: String, var age: Int){
var profession: String = “Not Mentioned”

constructor (name: String, age: Int, profession: String): this(name,age){
this.profession = profession
}

fun printPersonDetails(){
println(“$name whose profession is $profession, is $age years old.”)
}
}
34. What is the difference between val and var ?
Ans:
Val (Value) is like a constant. Once assigned a value, you cannot change it. On the other hand Var (Variable) is designed to be a storage location that can accept reassignment of values of same data type or what ever feasible by the data type casting.
35. – What is Kotlin’s Null Safety ?
Ans:
Null Safety in Kotlin is to eliminate the risk of occurrence of NullPointerException in real time. Kotlin can differentiate between nullable references and non-nullable references. If a variable has to be allowed to store a null value, that has to be declared with a null (?) operator.
36. If you have worked with files, name some of the extension methods Kotlin provides to java.io.File
Ans:
Kotlin provides very useful extension functions to java.io.File. Some of them are :
File.bufferedReader() : to read contents of a file into BufferedReader
File.forEachLine() : to read a file line by line in Kotlin
File.inputStream() : to read contents of file to InputStream
File.readBytes() : to read contents of file to ByteArray
File.readLines() : to read lines in file to List
File.readText() : to read contents of file to a single String
For examples to these methods refer – Kotlin Read File Content
37. Is there Ternary Conditional Operator in Kotlin like in Java ?
Ans:
No.
38. How do you realize Ternary Conditional Operator in Kotlin ?
Ans:
A simple if else should do the job.

if (condition) a else b

39. How do you declare a variable as volatile in Kotlin ?
Ans:
By providing volatile annotation before the declaration of variable.

@Volatile var a: Long? = null

40. How do you check if two Strings are equal valued ?
Ans:
Using == (double equal to) operator.

fun main(args: Array<String>) {
val a: String = “kotlin is easy”
val b: String = “kotlin is” + ” easy”
if(a==b){
println(” a and b are equal.”)
} else {
println(” a and b are not equal.”)
}
}
For more  Click Here
For Course Content  Click Here