Kotlin

Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. So if you are having an interview in coming days, then you must checkout these question and answers which guide you further to ace the job interview.

Q.1 How do you define the Target Platform of Kotlin and how is Kotlin-Java interoperability possible?
Java Virtual Machine(JVM) is the Target Platform of Kotlin. Such that Kotlin is 100% interoperable with Java since both, on compilation produce bytecode. Therefore Kotlin code can be referred from Java and vice-versa.
Q.2 Differentiate between const and a val?
Some of the points of difference between const and val are -
1. By default val properties are set at runtime. Therefore by adding a const modifier on a val would make a compile-time constant.
2.A const cannot be used with a var or on its own.
3 A const is not applicable on a local variable.
Q.3 Does Kotlin permit the use of primitive types like int, float, double?
Kotlin does not permit the use of primitive types like int, float, double at the language level. The JVM bytecode that’s compiled does certainly have them.
Q.4 What is the entry point of every Kotlin Program?
The primary function is the entry point of every Kotlin program. Such that in Kotlin we can choose not to write the main function inside the class. Thereby on compiling the JVM implicitly encapsulates it in a class. Also the strings passed in the form of Array are used to retrieve the command line arguments.
Q.5 Differentiate between == and === operators in Kotlin?
== is used to compare the values are equal or not. On the other hand === is used to check if the references are equal or not.
Q.6 What are the visibility modifiers available in Kotlin and what is the default visibility modifier?
Some of the visibility modifiers available in Kotlin are - public, internal, protected, and private. Also 'public' is the default visibility modifier.
Q.7 Name the types of constructors in Kotlin?
Mainly constructors in Kotlin are of two types namely -
1. Primary constructors – These are defined in the class headers. Such that they cannot hold any logic. There’s only one primary constructor per class.
2. Secondary constructors – They are 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.
Q.8 How do you define 'init block' in Kotlin?
init is the initialiser block in Kotlin which is executed once the primary constructor is instantiated. Such that if you invoke a secondary constructor, then it works after the primary one as it is composed in the chain.
Q.9 Name the different type of arguments inside a constructor?
The constructor arguments are val unless explicitly set to var.
Q.10 Differentiate between lazy and lateinit.
The points of difference between lazy and lateinit are -
1. Both are used to delay the property initializations in Kotlin
On one hand lateinit is a modifier used with var and is used to set the value to the var at a later point whereas 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
Q.11 What are the advantages of using Kotlin?
The advantages of using Kotlin are -
1. Kotlin language is easy to learn as its syntax is similar to Java.
2. Kotlin is a functional language and based on JVM. So, it removes lots of boiler plate
3.It is an expressive language which makes code readable and understandable.
Q.12 How do you define ‘Null Safety’ in Kotlin?
In Kotlin, Null Safety feature permits to remove the risk of occurrence of NullPointerException in real time. Such that it is also possible to differentiate between nullable references and non-nullable references.
Q.13 Why is Kotlin interoperable with Java?
The reason for Kotlin to interoperable with Java is sine it uses JVM bytecode. Therefore the process of compiling it directly to bytecode helps to achieve faster compile time and makes no difference between Java and Kotlin for JVM.
Q.14 What are the extension methods Kotlin provides to java.io.File?
The extension methods Kotlin provides to java.io.File are -
1. bufferedReader(): Use for reading contents of a file into BufferedReader
2. readBytes() : Use for reading contents of file to ByteArray
3. readText(): Use of reading contents of file to a single String
4. forEachLine() : Use for reading a file line by line in Kotlin
5. readLines(): Use to reading lines in file to List
Q.15 What is the default behavior of Kotlin classes?
In Kotlin all classes are final by default since Kotlin permits multiple inheritances for classes, and an open class is more expensive than a final class.
Q.16 Does Kotlin offer any additional functionality for standard Java packages or classes?
Kotlin programs can easily run on standard JVM like any another compiled Java code. It permits JVM to compile any program to byte-code. Kotlin is accessible using Java Virtual Machine. Therefore, Kotlin is almost similar to Java. Also Kotlin applications can be built with parts of Java code.
Q.17 Define the use of abstraction in Kotlin.
Abstraction is defined as one of the most important concept of Objected Oriented Programming. In Kotlin, abstraction class is used when you know the functionalities a class should have. One may not be aware of how the functionality is implemented or if the functionality can be implemented using different methods.
Q.18 Describe the default behavior of Kotlin classes?
All classes are final by default, in Kotlin because Kotlin allows multiple inheritances for classes, and an open class is more expensive than a final class.
Q.19 Describe the features that are there in Kotlin but not In Java.
Some of the important Kotlin features that Java does not have -
1. Null Safety
2. Operator Overloading
3. Coroutines
4. Range expressions
5. Smart casts
6. Companion Objects
Q.20 What are Higher-Order Functions?
Higher-Order Functions can be defined as a function that takes functions as parameters, or returns a function.
Q.21 Name the different types of strings available in Kotlin and what do you understand by Kotlin String Interpolation?
The different types of strings available in Kotlin are -
1. Strings are a collection of characters together. Such that Kotlin features two types of strings, and they are - Raw string and Escaped string
In Kotlin String, templates can be evaluated. This evaluation of string templates is referred as the string template interpolation.
Get Govt. Certified Take Test