Kotlin Interview Questions - trungungtoan-agilityio/kotlin-learning GitHub Wiki
Kotlin Interview Questions
1. What is Kotlin?
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.
2. 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.
3. Why is Kotlin preferred over Java?
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.
4. Explain Kotlin’s Null safety?
In Kotlin, the Null safety is used to eliminate the risk of countering the NullPointer exception in real time.
5. Explain Functions In Kotlin?
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)
6. Where does the Kotlin run and what is the entry point of Kotlin?
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.
7. Explain the data classes in Kotlin?
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.
8. What is the difference between declaration variable using val or var in Kotlin?
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.
9. State the differences between Val and Var?
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.
10. How to convert a String to an Int in Kotlin?
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()
}
11. Which type of Programming does Kotlin support?
Kotlin supports only two types of programming, and they are: Procedural programming Object-oriented programming
12. What are the different types of constructors in Kotlin?
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.
13. Explain Higher-Order Functions?
Higher-Order Functions: A higher-order function is a function that takes functions as parameters, or returns a function.
14. List the Basic data types of Kotlin?
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
15. Mention the structural expressions in Kotlin?
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.