Interview questions for Kotlin

1. What is a data class in Kotlin? 

We frequently create classes whose main purpose is to hold data. In Kotlin, this is called a data class and is marked as data:
To ensure consistency and meaningful behavior of the generated code, data classes have to fulfill the following requirements:
• The primary constructor needs to have at least one parameter.
• All primary constructor parameters need to be marked as val or var.
• Data classes cannot be abstract, open, sealed or inner.

2. What is kotlin?

Kotlin 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 a surplus of the Kotlin Language.

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

4. What’s Null Safety and Nullable Types in Kotlin? What is the Elvis Operator?

Kotlin puts a lot of weight behind null safety which is an approach to prevent the dreaded Null Pointer Exceptions by using nullable types which are like String?, Int?, Float? etc. These act as a wrapper type and can hold null values. A nullable value cannot be added to another nullable or basic type of value.
To retrieve the basic types we need to use safe calls that unwrap the Nullable Types. If on unwrapping, the value is null we can choose to ignore or use a default value instead. The Elvis Operator is used to safely unwrap the value from the Nullable.
It’s represented as ?: over the nullable type. The value on the right hand side would be used if the nullable type holds a null.

5. Why did you switch to Kotlin from Java ?

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.

6. What kinds of programming does Kotlin support?

Kotlin supports two types of programming. They are
1. Procedural Programming
2. Object Oriented Programming

7. What are the types of constructors in Kotlin? How are they different? How do you define them in your class?

Constructors in Kotlin are of two types:
Primary – These are defined in the class headers. They cannot hold any logic. There’s only one primary constructor per class.
Secondary – They’re defined in the class body. They must delegate to the primary constructor if it exists. They can hold logic. There can be more than one secondary constructors

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

9. How do you think extension functions are useful?

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.

10. What’s init block in Kotlin?

init is the initialiser block in Kotlin. It’s executed once the primary constructor is instantiated. If you invoke a secondary constructor, then it works after the primary one as it is composed in the chain.

11. Does Kotlin provide any additional functionality for standard Java?

packages or standard Java classes? – Kotlin Interview Question
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.

12. Tell three most important benefits of using Kotlin?

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

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

No there is no ternary conditional operator in Kotlin language.

14. How do you migrate the code from Java to Kotlin?

Jet Brains 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.

15. How can you declare a variable in Kotlin?

value my_var: Char

16. How many constructors are available in Kotlin?

Two types of constructors available in Kotlin are:
1. Primary constructor
2. Secondary constructor

17. How many types of constructors are there ? What are they?

There are two types of constructors. They are Primary Constructors and Secondary Constructors.

18. What’s the difference between inline and infix functions? Give an example of each?

Inline functions are used to save us memory overhead by preventing object allocations for the anonymous functions/lambda expressions called. Instead, it provides that functions body to the function that calls it at runtime. This increases the bytecode size slightly but saves us a lot of memory.

19. What is Kotlin Null Safety?

Null Safety in Kotlin is to eliminate the risk of occurrence of Null Pointer Exception 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.

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

leave your comment


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