Basic Kotlin interview questions and answers

What’s the Target Platform of Kotlin? How is Kotlin-Java interoperability possible?

Java Virtual Machine(JVM) is the Target Platform of Kotlin. Kotlin is 100% interoperable with Java since both, on compilation produce bytecode. Hence Kotlin code can be called from Java and vice-versa.

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.

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.


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

Mid
Answer

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 interface and regular class imo

extension functions

  • java: doesnt exist

  • kotlin: does exist, makes functions more clear in usage and feels more natural.

null

  • java: Anything but primitives can be null at any time.

  • kotlin: you get to decide what can and cant be null. allows for nice things like inline class

singleton

  • java: Memorize singleton pattern

  • kotlin: object instead of class

generics

  • java: Theyre 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 arent careful.

  • kotlin: does exist, easy to preserve api back-compatiblity.

primary constructor

  • java: does not have per-se, 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

What are some disadvantages of Kotlin? What are some disadvantages of Kotlin?

Mid
Answer

Some think that Kotlin is a mess of extra syntax and keywords. Here are a few keywords which have non-obvious meanings: internal, crossinline, expect, reified, sealed, inner, open. Java has none of these. Kotlin is also amusingly inconsistent in its keywords: a function is is declared with ‘fun’, but an interface is declared with ‘interface’ (not ‘inter’?). Kotlin also doesn’t have checked exceptions. Checked exceptions have become unfashionable, yet many (including me) find them a powerful way to ensure that your code is robust. Finally, Kotlin hides a lot of what goes on. In Java, you can trace through almost every step of program logic. This can be vital for hunting down bugs. In Kotlin, if you define a data class, then getters, setters, equality testing, to string, and hash code are added for you invisibly. This can be a bad idea.

Also according 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

How does Kotlin work on Android?

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.

Why should we use Kotlin?

  • Kotlin is concise
  • Kotlin is null-safe
  • Kotlin is interoperable

Learn about these features from MindOrks video.

Variable

1) How do you declare variables in Kotlin? How does the declaration differ from the Java counterpart?

There are two major differences between Java and Kotlin variable declaration:

  • The type of declaration
    In Java the declaration look like this:

    
    String s = "Java String";
    int x = 10;
    

    In Kotlin the declaration looks like:

    
    val s: String = "Hi"
    var x = 5
    

    In Kotlin, the declaration begins with a val and a var followed by the optional type. Kotlin can automatically detect the type using type inference.

  • Default value
    The following is possible in Java:

    
    String s:
    

    The following variable declaration in Kotlin is not valid.

    
    val s: String

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.

What is the difference between lazy and lateinit?

Both are used to delay the property initializations in Kotlin
lateinit is a modifier used with var and is used to set the value to the var at a later point.
lazy is a method or rather say lambda expression. It’s set on a val only. The val would be created at runtime when it’s required.


val x: Int by lazy { 10 }
lateinit var y: String


What’s the Difference Between const and val?


Earlier we illustrated how to create variables that only contain fixed data using the val keyword. However, Kotlin offers the const keyword for creating constants like the C programming language. The key difference between val and const is their execution phase. Kotlin sets the properties of val at runtime by default. On the other hand, const is set by the compiler during the program’s compiler time.

const val str = "Fixed string!" // global variable
fun main(args: Array<String>) {
const val x = 4
const val str = "New string.." // local variable
}

Additionally, you can not use const inside the local scope, thus the above code block will fail to compile. This modifier is also not applicable on var.

What’s a const? How does it differ from a val?

Ans: The properties of val are set at runtime by default.Just by adding a const modifier with val would make the variable compile-time constant.

It is not allowed to use a const with a var or on its own.

On  local variable const is not applicab

What is the difference between the variable declaration with val and const?

Both the variables that are declared with val and const are immutable in nature. But the value of the const variable must be known at the compile-time whereas the value of the val variable can be assigned at runtime also.

Learn more about the difference between const and val from MindOrks blog.

How is it recommended to create constants in Kotlin? How is it recommended to create constants in Kotlin?
Mid
Answer

In Kotlin, if you want to create the local constants which are supposed to be used with in the class then you can create it like below:

val MY_CONSTANT_1 = "Constants1"
// or 
const val MY_CONSTANT_2 = "Constants2"

Like val, variables defined with the const keyword are immutable. The difference here is that const is used for variables that are known at compile-time.

Also avoid using companion objects. Behind the hood, getter and setter instance methods are created for the fields to be accessible. Calling instance methods is technically more expensive than calling static methods. Instead define the constants in object:

object DbConstants {
        const val TABLE_USER_ATTRIBUTE_EMPID = "_id"
        const val TABLE_USER_ATTRIBUTE_DATA = "data"
}


Write about the Kotlin Property Initialization?

To initialize a property in the constructor, there are two important ways of property initialization in Kotlin.
  • lateinit
  • lazy

Generally, properties declared as having a non-null type must be initialized in the constructor. However, fairly often this is not convenient.

Example

  1. public class Test {
  2. lateinit var mock: Mock
  3. @SetUp fun setup() {
  4. mock = Mock()
  5. }
  6. @Test fun test() {
  7. mock.do()
  8. }
  9. }

The lazy() function that takes a lambda and returns an instance of lazy which can serve as a delegate for implementing a lazy property.

Example

  1. public class Example{
  2. val name: String by lazy { “Amit Shekhar” }
  3. }

Why we use const and val in Kotlin?
 
We Use Const because the Keyword const is used for compile-time initializations.
Val Keyword is used for runtime initialization

What’s a const? How does it differ from a val?

By default val properties are set at runtime. Adding a const modifier on a val would make a compile-time constant

const cannot be used with a var or on its own.

A const is not applicable on a local variable.

  1. kotlin interview questions const val


Where should I use var and where val? Where should I use var and where val?

 Junior
Answer

Use var where value is changing frequently. For example while getting location of android device:

var integerVariable : Int? = null

Use val where there is no change in value in whole class. For example you want set textview or button's text programmatically.

val stringVariables : String = "Button's Constant or final Text"


What will be result of the following code execution? What will be result of the following code execution?

Mid
Details

What will be the output?

val aVar by lazy {
    println("I am computing this value")
    "Hola"
}
fun main(args: Array<String>) {
    println(aVar)
    println(aVar)
}
Solution

For lazy the first time you access the Lazy property, the initialisation (lazy() function invocation) takes place. The second time, this value is remembered and returned:

I am computing this value
Hola
Hola


What is Lateinit in Kotlin and when would you use it? What is Lateinit in Kotlin and when would you use it?

Mid
Answer

lateinit means late initialization. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized. You cannot use lateinit for primitive type properties like Int, Long etc.

lateinit var test: String

fun doSomething() {
    test = "Some value"
    println("Length of string is "+test.length)
    test = "change value"
}

There are a handful of use cases where this is extremely helpful, for example:

  • Android: variables that get initialized in lifecycle methods;
  • Using Dagger for DI: injected class variables are initialized outside and independently from the constructor;
  • Setup for unit tests: test environment variables are initialized in a @Before - annotated method;
  • Spring Boot annotations (eg. @Autowired).

When to use the lateinit keyword in Kotlin?

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.

When to use lateinit over lazy initialization in Kotlin? When to use lateinit over lazy initialization in Kotlin?

Mid
Answer

There are some simple rules to determined if you should use one or the other for properties initialisation:

  • If properties are mutable (i.e. might change at a later stage) use lateInit
  • If properties are set externally (e.g. need to pass in some external variable to set it), use lateinit. There’s still workaround to use lazy but not as direct.
  • If they are only meant to initialized once and shared by all, and it’s more internally set (dependent on variable internal to the class), then use lazy. Tactically, you could still use lateinit, but using** lazy** would better encapsulate your initialization code.

Also compare:

lateinit varby lazy
Can be initialized from anywhere the object seen from.Can only be initialized from the initializer lambda.
Multiple initialization possible.Only initialize single time.
Non-thread safe. It’s up to user to initialize correctly in a multi-threaded environment.Thread-safety by default and guarntees that the initializer is invoked by once.
Can only be used for var.Can only be used for val.
Not eligible for nonnull properties.Not eligible for nonnull properties.
An isInitialized method added to check whether the value has been initialized before.Property never able to un-initialized.
Not allowed on properties of primitive types.Allowed on properties of primitive types.

How to check if a lateinit variable has been initialized or not?

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:

class Person {
 lateinit var name: String
 fun initializeName() {
 println(this::name.isInitialized)
 name = "MindOrks" // initializing name
 println(this::name.isInitialized)
 }
}
fun main(args: Array<String>) {
 Person().initializeName()
}

Learn more about it from MindOrks blog.

What is the difference between lateinit and lazy in Kotlin?

  • 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.

What will be result of the following code execution?

What will be the output?

val aVar by lazy {
println("I am computing this value")
"Hola"
}
fun main(args: Array<String>) {
println(aVar)
println(aVar)
}

For lazy the first time you access the Lazy property, the initialisation (lazy() function invocation) takes place. The second time, this value is remembered and returned:

I am computing this value
Hola
Hola


What is Lateinit in Kotlin and when would you use it? ☆☆☆

Answer: lateinit means late initialization. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized. You cannot use lateinit for primitive type properties like Int, Long etc.

lateinit var test: String

fun doSomething() {
    test = "Some value"
    println("Length of string is "+test.length)
    test = "change value"
}

There are a handful of use cases where this is extremely helpful, for example:

  • Android: variables that get initialized in lifecycle methods;
  • Using Dagger for DI: injected class variables are initialized outside and independently from the constructor;
  • Setup for unit tests: test environment variables are initialized in a @Before - annotated method;
  • Spring Boot annotations (eg. @Autowired).

Source: medium.com


Explain lazy initialization in Kotlin ☆☆☆

Answer: 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.

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 to get() executes the lambda passed to lazy() and remembers the result, subsequent calls to get() simply return the remembered result.

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

Source: blog.mindorks.com

When to use lateinit over lazy initialization in Kotlin? ☆☆☆

Answer:

There are some simple rules to determined if you should use one or the other for properties initialisation:

  • If properties are mutable (i.e. might change at a later stage) use lateInit
  • If properties are set externally (e.g. need to pass in some external variable to set it), use lateinit. There’s still workaround to use lazy but not as direct.
  • If they are only meant to initialized once and shared by all, and it’s more internally set (dependent on variable internal to the class), then use lazy. Tactically, you could still use lateinit, but using** lazy** would better encapsulate your initialization code.

Also compare:

lateinit varby lazy
Can be initialized from anywhere the object seen from.Can only be initialized from the initializer lambda.
Multiple initialization possible.Only initialize single time.
Non-thread safe. It’s up to user to initialize correctly in a multi-threaded environment.Thread-safety by default and guarntees that the initializer is invoked by once.
Can only be used for var.Can only be used for val.
Not eligible for nonnull properties.Not eligible for nonnull properties.
An isInitialized method added to check whether the value has been initialized before.Property never able to un-initialized.
Not allowed on properties of primitive types.Allowed on properties of primitive types.

Source: ahsensaeed.com

Which one is better to use - val mutableList or var immutableList in the context of Kotlin?

The program's design clarity is improved by using mutable and immutable lists. This is done to have the developer think about and clarify the collection's purpose.

We use a mutable list if the collection will alter as part of the design. On the other hand, we use an immutable list if the model is only meant to be viewed.

Val and var serve a distinct purpose than immutable and mutable lists. The val and var keywords specify how a variable's value/reference should be handled. We use var when the value or reference of a variable can be altered at any moment. On the other hand, we use val when a variable's value/reference can only be assigned once and cannot be modified later in the execution.

Immutable lists are frequently preferred for a variety of reasons:

  • They promote functional programming, in which state is passed on to the next function, which constructs a new state based on it, rather than being altered. This is evident in Kotlin collection methods like map, filter, reduce, and so forth.
  • It's often easier to understand and debug software that doesn't have any side effects (you can be sure that the value of an object will always be the one at its definition).
  • Because no write access is required in multi-threaded systems, immutable resources cannot induce race conditions.

However, there are some disadvantages of using immutable lists as well. They are as follows :

  • Copying large collections simply to add/remove a single piece is very expensive.
  • When you need to alter single fields frequently, immutability can make the code more difficult. Data classes in Kotlin provide a built-in copy() method that allows you to clone an instance while changing only part of the fields' values.

What do you understand about lateinit in Kotlin? When would you consider using it?

lateinit is an abbreviation for late initiation. If you don't want to initialize a variable in the constructor and instead want to do it later, and you can guarantee the initialization before using it, use the lateinit keyword to declare that variable. It won't start allocating memory until it's been initialized. Lateinit cannot be used for primitive type attributes like Int, Long, and so on. Because the lateinit variable will be initialized later, you cannot use val. When a lateinit property is accessed before it has been initialized, a special exception is thrown that explicitly identifies the property and the fact that it hasn't been initialized.

For example,

// KOTLIN
lateinit var test: String
fun testFunction() {
   test = "Interview"
   println("The length of string is "+test.length)
   test = "Bit"
}

When the testFunction is called, we get the following output:-

9 

There are a few scenarios in which this is particularly useful, for example:

  • Variables that are initialized in lifecycle methods in Android;
  • Using Dagger for DI: injected class variables are initialized outside of the constructor and independently;
  • Setup for unit tests: in a @Before - annotated function, test environment variables are initialized;
  • Annotations in Spring Boot (for example, @Autowired).

17. Explain lazy initialization in the context of Kotlin.

There are some classes whose object initialization is so time-consuming that it causes the entire class creation process to be delayed. Lazy initialisation helps in such problems. When we declare an object using lazy initialisation, the object is initialised only once when the object is used. If the object is not used throughout, the object is not initialised. This makes the code more efficient and faster. 

For example, let us imagine you have a SlowClass class and you require an object of that SlowClass in a different class called FastClass:

// KOTLIN
class FastClass {
   private val slowObject: SlowClass = SlowClass()
}

We are generating a large object here, which will cause the development of the FastClass to be slow or delayed. There may be times where the SlowClass object isn't required. As a result, the lazy keyword can assist you in this situation:

class FastClass {
   private val slowObject: SlowClass by lazy {
       SlowClass()
   }  
}

For example,

// KOTLIN
class FastClass {
   private val slowObject: SlowClass by lazy {
       println("Slow Object initialised")
       SlowClass()
   } 
   
   fun access() {
       println(slowObject)
   }
}
fun main(args: Array<String>) {
   val fastClass = FastClass()
   println("FastClass initialised")
   fastClass.access()
   fastClass.access()
}

Output:-

FastClass initialised 
Slow Object initialised 
SlowClass@2b12fkk7 
SlowClass@2b12fkk7

Explanation:- In the above code, we have instantiated an object of the SlowClass inside the class structure of the FastClass using lazy initialisation. The object of the SlowClass is generated only when it is accessed in the above code, that is, when we call the access() method of the FastClass object and the same object is present throughout the main() method.

18. Differentiate between lateinit and lazy initialisation. Explain the cases when you should use lateinit and when you should use lazy initialisation.

Following are the differences between lateinit and lazy initialisation:-


lateinitlazy initialisation
The main purpose is to delay the initialisation to a later point in time.The main purpose is to initialise an object only when it is used at a later point in time. Also, a single copy of the object is maintained throughout the program. 
It's possible to initialise the object from anywhere in the program.Only the initializer lambda can be used to initialise it.
Multiple initializations are possible in this case.Only a single initialisation is possible in this case.
It's not thread-safe. In a multi-threaded system, it is up to the user to correctly initialise.Thread-safety is enabled by default, ensuring that the initializer is only called once.
It works only with var.It works only with val.
The isInitialized method is added to verify if the value has previously been initialised.It is impossible to uninitialize a property.
Properties of primitive types are not allowedAllowable on primitive type properties.

There are a few easy principles to follow when deciding whether to use lateinit or lazy initialisation for property initialization:

  • Use lateInit if properties are mutable (i.e., they may change later).
  • Use lateinit if properties are set externally (for example, if you need to pass in an external variable to set it). There is still a way to use lazy, but it isn't as obvious.
  • If they're only meant to be initialised once and shared by everybody, and they're more internally set (depending on a class variable), then lazy is the way to go. We could still use lateinit in a tactical sense, but utilising lazy initialisation would better encapsulate our initialization code.

Data type

Does Kotlin allow us to use primitive types such as int, float, double?

At the language level, we cannot use the above-mentioned types. But the JVM bytecode that’s compiled does certainly have them.

Does Kotlin support primitive Datatypes?

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

Type conversation

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()
}

What’s the Problem with Below Code?


val name = "UBUNTU"
val upperCase = name.toUpperCase()

name.inc()

The above code snippet will fail to compile due to type exception. Kotlin is statically typed and implements strong type inferences. Since the value of the name variable is a String, Kotlin assumes that name is also a type of String. Thus the second line is perfectly valid because the method toUpperCase() is a String method. The last line, however, tries to perform an increment operation. This line causes Kotlin to throw a compilation error since inc() can only work with Int values, not Strings.

Can we use primitive types such as int, double, float in Kotlin?

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.


Aaaaas
Operators

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

No there is no ternary conditional operator in Kotlin language

What’s the difference between == and === operators in Kotlin?

== is used to compare the values are equal or not. === is used to check if the references are equal or not.Input/Output

Is there a Ternary Conditional Operator in Kotlin like in Java?
 
No, the ternary conditional operator in Kotlin is not like Java but is very different from it

Comments


Is there any difference between == operator and === operator?

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

What is the operator overloading in Kotlin?

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:

fun main() {
 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 class Pen(val inkColor:String){
 fun showInkColor(){ println(inkColor)}
 }

Learn more about operator overloading from MindOrks blog.

aaaaaaaaaaaaaaaaaaaa

Comments

Popular posts from this blog

Jetpack Compose based Android interview and questions

Null safety based Kotlin interview questions and answers

CustomView based Android interview questions and answers