Kotlin is a statically typed, open-source programming language primarily used for Android app development. It supports object-oriented and functional programming and is designed to work with Android and the Java Virtual Machine.
Let’s see some of the most commonly asked Android Kotlin interview questions and how you can answer them.
Mutable variables are those whose value can be changed after the initial declaration.
Immutable variables are those whose value cannot change after the initial declaration.
The keyword val (value) is used to declare immutable variables, and var (variable) is used to declare mutable variables.
Val and const keywords are used to declare immutable variables, and their values cannot be changed once declared. The key difference between the two is that const declares a constant at compile time, and you cannot change its value at runtime. With val, you can assign the variable's value at runtime, though it cannot be reassigned once set.
Null safety is a feature by which Kotlin eliminates the null pointer exceptions at runtime. This helps in improved code readability and increased productivity by ensuring that null references are handled explicitly.
The compiler provides mechanisms for safely handling nullable types, which should be handled specifically using safe call (?.), the Elvis operator (?:), or assertions (!!).
The Elvis operator is represented by the ?: operator. It is used for null safety in Kotlin.
The Elvis operator evaluates the expression on the left side of the operator. If the left side is not null, the Elvis operator returns the value of the expression on the left. If the value of the left side of the operator is null, then the Elvis operator returns the expression on the right.
The following example demonstrates the Elvis operator:
fun main() { val name: String? = null val defaultName = "Jane" // Using the Elvis operator val displayName = name ?: defaultName println(displayName) // Output: Jane } |
Non-nullable types are used for variables that cannot hold a null value.
Nullable types are used when variables need to hold a null value. In such cases, the variable declaration should have a ‘?’ after the type.
The lateinit property lets you declare a non-nullable variable immediately without initializing it. This is useful when you cannot initialize a variable at the time of declaration but are sure it will be initiated before it is used.
Lazy is used when you are unsure when a variable will be initialized. The keyword can be used to declare a variable that will only be initialized when accessed for the first time.
Lists and arrays are collections used to allocate sequential memory. The major difference between the two is that arrays are mutable, meaning their values can be changed and are of fixed size.
Lists can be mutable or immutable. Immutable lists have a fixed size and do not allow any modification of elements once they are created. You cannot add, remove, or change elements. Meanwhile, mutable lists can be dynamically resized and allow for adding, removing, and modifying elements.
In Kotlin, the public is a visibility modifier. This means any public class or member is visible and accessible by any other class or file. The public is the default modifier, and a class or member must not be explicitly marked public.
Open is an inheritance modifier. Any class specified as ‘open’ can be inherited or overridden. In Kotlin, the default inheritance modifier is final. So, any class that needs to be inherited or overridden should be explicitly marked open.
Blocking and suspending are used concerning coroutines.
Blocking calls stops the execution of a thread until a certain operation is completed. The thread is unable to do anything else during this time.
A suspend function, marked with the keyword suspend, can stop the execution of a coroutine without blocking the underlying thread.
A data class in Kotlin is a class specifically used to hold data. The keyword data is used to declare a class as a data class.
data class User(val name: String, val age: Int) |
The key to acing any interview is a strong understanding of the basic concepts and to be articulate with them. Practice with as many basic and advanced Kotlin interview questions as possible to identify the gaps in your knowledge of the programming language, learn, and build confidence.