Just like Java, the Kotlin code is also compiled into the Java bytecode and is executed at runtime by the Java Virtual Machine i.e. JVM. When a Kotlin file named Main.kt
is compiled then it will eventually turn into a class and then the bytecode of the class will be generated. The name of the bytecode file will be MainKt.class
and this file will be executed by the JVM.
- Kotlin is concise
- Kotlin is null-safe
- Kotlin is interoperable —. easily convert kotlin to java and vice-versa
Learn about these features from MindOrks video.
If you want to declare some mutable(changeable) variable, then you can use var
. For the immutable variable, use val
i.e. val
variables can't be changed once assigned.
-
4. What is the difference between the variable declaration with val and const?
Both the variables that are declared with
val
andconst
are immutable in nature. But the value of theconst
variable must be known at the compile-time whereas the value of theval
variable can be assigned at runtime also.A property must satisfy the following to be a
const
property:- must be at top-level or member of object or member of a companion object
- must be initialised with a
String
type or primitive type - no custom getter
So, you can't assign a
const
variable to a function or to some class because in this case the variable will be initialised at the runtime and not at compile-time.eg. of file name and concating file extension
Learn more about the difference between const and val from MindOrks blog.
-
5. How to ensure null safety in Kotlin?
One of the major advantages of using Kotlin is null safety. In Java, if you access some null variable then you will get a
NullPointerException
. So, the following code in Kotlin will produce a compile-time error:var name: String = "MindOrks" name =null//error
So, to assign null values to a variable, you need to declare the
name
variable as a nullable string and then during the access of this variable, you need to use a safe call operator i.e.?.
var name: String? = "MindOrks" print(name?.length)// ok name =null// ok
Learn more about safe calls(?.) and null check(!!) from MindOrks blog.
-
6. What is the difference between safe calls(?.) and null check(!!)?
Safe call operator i.e.
?.
is used to check if the value of the variable is null or not. If it is null then null will be returned otherwise it will return the desired value.var name: String? = "MindOrks" println(name?.length)// 8 name =nullprintln(name?.length)// null
If you want to throw NullPointerException when the value of the variable is null, then you can use the null check or
!!
operator.var name: String? = "MindOrks" println(name?.length)// 8 name =nullprintln(name!!.length)// KotlinNullPointerException
Learn more about safe calls(?.) and null check(!!) from MindOrks blog.
No, we don't have a ternary operator in Kotlin but you can use the functionality of ternary operator by using if-else or Elvis operator.
In Kotlin, you can assign null values to a variable by using the null safety property. To check if a value is having null value then you can use if-else or can use the Elvis operator i.e. ?:
For example:
var name:String? = "Mindorks"
val nameLength = name?.length ?: -1
println(nameLength)
The Elvis operator(?:
) used above will return the length of name if the value is not null otherwise if the value is null, then it will return -1
.
Steps to convert your Kotlin source file to Java source file:
- Open your Kotlin project in the IntelliJ IDEA / Android Studio.
- Then navigate to Tools > Kotlin > Show Kotlin Bytecode.
- Now click on the Decompile button to get your Java code from the bytecode.
Learn more about the conversion steps from MindOrks blog.
- @JvmStatic: This annotation is used to tell the compiler that the method is a static method and can be used in Java code.
- @JvmOverloads: To use the default values passed as an argument in Kotlin code from the Java code, we need to use the
@JvmOverloads
annotation. - @JvmField: To access the fields of a Kotlin class from Java code without using any getters and setters, we need to use the
@JvmField
in the Kotlin code.
Learn more about @JvmStatic, @JvmOverloads, and @JvmField in Kotlin from MindOrks blog.
Data classes are those classes which are made just to store some data. In Kotlin, it is marked as data. The following is an example of the same:
data classDeveloper(val name: String, val age: Int)
When we mark a class as a data class, you don’t have to implement or create the following functions like we do in Java: hashCode()
, equals()
, toString()
, copy()
. The compiler automatically creates these internally, so it also leads to clean code. Although, there are few other requirements that data classes need to fulfill.
Learn more about data class from MindOrks blog.
In Kotlin, we can't use primitive types directly. We can use classes like Int, Double, etc. as an object wrapper for primitives. But the compiled bytecode has these primitive types.
If you want to use some variable or perform some operation inside a string then String Interpolation can be used. You can use the $
sign to use some variable in the string or can perform some operation in between {}
sign.
var name = "MindOrks"
print("Hello! I am learning from $name")
Destructuring is a convenient way of extracting multiple values from data stored in(possibly nested) objects and Arrays. It can be used in locations that receive data (such as the left-hand side of an assignment). Sometimes it is convenient to destructure an object into a number of variables, for example:
val (name, age) = developer
Now, we can use name and age independently like below:
println(name)
println(age)
Learn more about Kotlin Destructuring from MindOrks blog.
lateinit
is late initialization.
Normally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient.
For example, properties can be initialized through dependency injection, or in the setup method of a unit test. In this case, you cannot supply a non-null initializer in the constructor, but you still want to avoid null checks when referencing the property inside the body of a class. To handle this case, you can mark the property with the lateinit modifier.
Learn more about lateinit from MindOrks blog.
You can check if the lateinit variable has been initialized or not before using it with the help of isInitialized
method. This method will return true if the lateinit property has been initialized otherwise it will return false. For example:
classPerson {
lateinit var name: String
funinitializeName() {
println(this::name.isInitialized)
name = "MindOrks"// initializing name
println(this::name.isInitialized)
}
}
funmain(args: Array<String>) {
Person().initializeName()
}
Learn more about it from MindOrks blog.
- lazy can only be used for val properties, whereas lateinit can only be applied to var because it can’t be compiled to a final field, thus no immutability can be guaranteed.
- If you want your property to be initialized from outside in a way probably unknown beforehand, use lateinit.
Learn more about the difference between lateinit and lazy from MindOrks blog.
Yes. The ==
operator is used to compare the values stored in variables and the ===
operator is used to check if the reference of the variables are equal or not. But in the case of primitive types, the ===
operator also checks for the value and not reference.
// primitive example
val int1 = 10
val int2 = 10
println(int1 == int2)// true
println(int1 === int2)// true// wrapper example
val num1 = Integer(10)
val num2 = Integer(10)
println(num1 == num2)// true
println(num1 === num2)//false
In Kotlin, to use the functionality of a for-each loop just like in Java, we use a forEach
function. The following is an example of the same:
var listOfMindOrks = listOf("mindorks.com", "blog.mindorks.com", "afteracademy.com")
listOfMindOrks.forEach {
Log.d(TAG,it)
}
In Kotlin, if you want to write a function or any member of the class that can be called without having the instance of the class then you can write the same as a member of a companion object inside the class.
To create a companion object, you need to add the companion
keyword in front of the object declaration.
The following is an example of a companion object in Kotlin:
classToBeCalled {
companion object Test {
funcallMe() = println("You are calling me :)")
}
}
funmain(args: Array<String>) {
ToBeCalled.callMe()
}
Learn more about companion object from MindOrks blog.
To achieve the functionality similar to Java static methods in Kotlin, we can use:
- companion object
- package-level function
- object
Read more about this from MindOrks blog.
- FlatMap is used to combine all the items of lists into one list.
- Map is used to transform a list based on certain conditions.
Read more about FlatMap and Map in Kotlin from MindOrks blog.
If you have a list of data that is having a fixed size, then you can use an Array. But if the size of the list can vary, then we have to use a mutable list.
Learn more about the difference between List and Array from MindOrks blog.
No, in Kotlin we don't have to use the new
keyword to instantiate a class object. To instantiate a class object, simply we use:
var varName = ClassName()
A visibility modifier or access specifier or access modifier is a concept that is used to define the scope of something in a programming language. In Kotlin, we have four visibility modifiers:
- private: visible inside that particular class or file containing the declaration.
- protected: visible inside that particular class or file and also in the subclass of that particular class where it is declared.
- internal: visible everywhere in that particular module.
- public: visible to everyone.
Note: By default, the visibility modifier in Kotlin is public.
Learn more about visibility modifiers in Kotlin from MindOrks blog.
A singleton class is a class that is defined in such a way that only one instance of the class can be created and is used where we need only one instance of the class like in logging, database connections, etc.
To create a Singleton class in Kotlin, you need to use the object keyword.
object AnySingletonClassName
Note: You can't use constructor in object, but you can use init.
Learn more about Singleton class from MindOrks blog.
init
blocks are initializer blocks that are executed just after the execution of the primary constructor. A class file can have one or more init blocks that will be executed in series. If you want to perform some operation in the primary constructor, then it is not possible in Kotlin, for that, you need to use the init
block.
Learn more about init blocks from MindOrks blog.
- Primary constructor: These constructors are defined in the class header and you can't perform some operation in it, unlike Java's constructor.
- Secondary constructor: These constructors are declared inside the class body by using the constructor keyword. You must call the primary constructor from the secondary constructor explicitly. Also, the property of the class can’t be declared inside the secondary constructor. There can be more than one secondary constructors in Kotlin.
Learn more about Primary and Secondary constructors from MindOrks blog.
Yes, when using a secondary constructor, you need to call the primary constructor explicitly.
By default, the type of arguments of a constructor in val. But you can change it to var explicitly.
A framework to manage concurrency in a more performant and simple way with its lightweight thread which is written on top of the actual threading framework to get the most out of it by taking the advantage of cooperative nature of functions.
This is an important interview question.
Learn more about Kotlin Coroutines with examples from MindOrks blog.
Suspend function is the building block of the Coroutines in Kotlin. Suspend function is a function that could be started, paused, and resume. To use a suspend function, we need to use the suspend keyword in our normal function definition.
Learn more about the suspend function from MindOrks blog.
The difference is that the launch{}
does not return anything and the async{}
returns an instance of Deferred<T>
, which has an await()
function that returns the result of the coroutine like we have future in Java in which we do future.get()
to the get the result.
In other words:
- launch: fire and forget
- async: perform a task and return a result
Learn more about Launch vs Async from MindOrks video.
Whenever we want to handle many if-else conditions, then we generally use switch-case statements. But Kotlin provides a more concise option i.e. in Kotlin, we can use when in place of the switch. And, when can be used as:
- expression
- arbitrary condition expression
- without argument
- with two or more choices
For example:
when(number) {
1 -> println("One")
2, 3 -> println("Two or Three")
4 -> println("Four")
else -> println("Number is not between 1 and 4")
}
Learn more about when from MindOrks blog.
By default, the classes and functions are final in Kotlin. So, you can't inherit the class or override the functions. To do so, you need to use the open keyword before the class and function. For example:
Learn more about open keyword from MindOrks blog.
Lambdas expressions are anonymous functions that can be treated as values i.e. we can pass the lambdas expressions as arguments to a function return them, or do any other thing we could do with a normal object. For example:
val add : (Int, Int) -> Int = { a, b -> a + b }
val result = add(9, 10)
Learn more about Lambdas from MindOrks blog.
A higher-order function is a function that takes functions as parameters or returns a function. For example, A function can take functions as parameters.
funpassMeFunction(abc: () -> Unit) {
// I can take function// do something here// execute the function
abc()
}
For example, A function can return another function.
funadd(a: Int, b: Int): Int {
return a + b
}
And, we have a function returnMeAddFunction which takes zero parameters and returns a function of the type ((Int, Int) -> Int).
funreturnMeAddFunction(): ((Int, Int) -> Int) {
// can do something and return function as well// returning functionreturn ::add
}
And to call the above function, we can do:
val add = returnMeAddFunction()
val result = add(2, 2)
Learn more about Higher-Order functions from MindOrks blog.
Extension functions are like extensive properties attached to any class in Kotlin. By using extension functions, you can add some methods or functionalities to an existing class even without inheriting the class. For example: Let's say, we have views where we need to play with the visibility of the views. So, we can create an extension function for views like,
fun View.show() {
this.visibility = View.VISIBLE
}
fun View.hide() {
this.visibility = View.GONE
}
and to use it we use, like,
toolbar.hide()
Learn more about extension functions from MindOrks blog.
An infix function is used to call the function without using any bracket or parenthesis. You need to use the infix keyword to use the infix function.
classOperations {
var x = 10;
infix funminus(num: Int) {
this.x =this.x - num
}
}
funmain() {
val opr = Operations()
opr minus 8
print(opr.x)
}
Inline function instruct compiler to insert complete body of the function wherever that function got used in the code. To use an Inline function, all you need to do is just add an inline keyword at the beginning of the function declaration.
Learn more about Inline functions from MindOrks blog.
While using an inline function and want to pass some lambda function and not all lambda function as inline, then you can explicitly tell the compiler which lambda it shouldn't inline.
inline fundoSomethingElse(abc: () -> Unit, noinline xyz: () -> Unit) {
abc()
xyz()
}
Learn more about noinline functions from MindOrks blog.
When you are using the concept of Generics to pass some class as a parameter to some function and you need to access the type of that class, then you need to use the reified keyword in Kotlin.
For example:
inline fun <reified T> genericsExample(value: T) {
println(value)
println("Type of T: ${T::class.java}")
}
funmain() {
genericsExample<String>("Learning Generics!")
genericsExample<Int>(100)
}
Learn more about Reified keyword from MindOrks blog.
In Kotlin, we can use the same operator to perform various tasks and this is known as operator overloading. To do so, we need to provide a member function or an extension function with a fixed name and operator keyword before the function name because normally also, when we are using some operator then under the hood some function gets called. For example, if you are writing num1+num2
, then it gets converted to num1.plus(num2)
.
For example:
funmain() {
val bluePen = Pen(inkColor = "Blue")
bluePen.showInkColor()
val blackPen = Pen(inkColor = "Black")
blackPen.showInkColor()
val blueBlackPen = bluePen + blackPen
blueBlackPen.showInkColor()
}
operator fun Pen.plus(otherPen: Pen):Pen{
val ink = "$inkColor, ${otherPen.inkColor}"return Pen(inkColor = ink)
}
data classPen(val inkColor:String){
funshowInkColor(){ println(inkColor)}
}
Learn more about operator overloading from MindOrks blog.
Pair and Triples are used to return two and three values respectively from a function and the returned values can be of the same data type or different.
val pair = Pair("My Age: ", 25)
print(pair.first + pair.second)
Learn more about Pairs and Triples from MindOrks blog.
Any expression written in Kotlin is called a label. For example, if we are having a for-loop in our Kotlin code then we can name that for-loop expression as a label and will use the label name for the for-loop.
We can create a label by using an identifier followed by the @
sign. For example, name@
, loop@
, xyz@
, etc. The following is an example of a label:
loop@for (i in 1..10) {
// some code goes here
}
The name of the above for-loop is loop
.
Learn more about labels in Kotlin from MindOrks blog.
Sealed classes give us the flexibility of having different types of subclasses and also containing the state. The important point to be noted here is the subclasses that are extending the Sealed classes should be either nested classes of the Sealed class or should be declared in the same file as that of the Sealed class.
Learn more about Sealed classes from MindOrks blog.
Prepare these 50 questions for your interview and also go through the given link in each question to have a better knowledge of the topic.