Android Interview Questions

Ranjeet Kumar yadav
5 min readJan 14, 2023

--

Image from Unsplash

Here in this article, I will be sharing some famous Kotlin Android Interview questions that are asked in Android Interviews. So, if you are preparing for Android Interviews, then this article is a must for you.

1.What is the difference between val and var in Kotlin?

val is used to declare a read-only variable, while var is used to declare a mutable variable. For example, a variable declared as val cannot be reassigned, while a variable declared as var can be reassigned.

val myString = "Hello World"
myString = "Hello Kotlin" // This will give a compile error

var myString = "Hello World"
myString = "Hello Kotlin" // This is valid

2.What is the difference between lateinit and lazy in Kotlin?

lateinit is used to initialize a non-nullable property outside of the constructor. lazy is used to create a property whose value will be computed only when it is first accessed.

class MyClass {
lateinit var myProperty: String
fun initialize() {
myProperty = "Hello"
}
}
val myClass = MyClass()
myClass.initialize()
println(myClass.myProperty) // Output: Hello

val myLazyProperty: String by lazy {
"Hello"
}
println(myLazyProperty) // Output: Hello

3.How does the ?. operator (Safe Call Operator)work in Kotlin?

The Safe Call Operator is used to safely access an object’s properties or methods. It checks if the object is null before accessing it, and returns null if it is. This is also known as the null-safe operator. For example,

val user: User? = getUser()
val name = user?.name //if user is null, name will be null

4.Explain the difference between a regular class and an object class in Kotlin.

A regular class is a template for creating objects, and an object class is a singleton class that can only have one instance throughout the entire application. A regular class is declared with the “class” keyword, while an object class is declared with the “object” keyword. For example,

class MyRegularClass { //regular class
//properties and methods
}

object MyObjectClass { //object class
//properties and methods
}

5.Explain the use of coroutines in Android.

Coroutines are a lightweight concurrency framework that allows you to write asynchronous code in a more readable and manageable way. They are used to perform long-running tasks such as network requests or database operations without blocking the main thread. This makes the app more responsive and improves the user experience. For example,

suspend fun getDataFromApi(): String {
return withContext(Dispatchers.IO) {
// perform network request
}
}

6.Explain the use of sealed classes in Kotlin.

Sealed classes are used to represent a closed set of cases for a given type. They are used to express the possible states of a variable or object in a more explicit and readable way. They can only have a limited set of subclasses and are defined within the same file as the sealed class. For example,

sealed class ApiResponse {
data class Success(val data: Any): ApiResponse()
data class Error(val error: String): ApiResponse()
object Loading: ApiResponse()
}

fun handleApiResponse(response: ApiResponse) {
when (response) {
is ApiResponse.Success -> handleSuccess(response.data)
is ApiResponse.Error -> handleError(response.error)
ApiResponse.Loading -> showLoading()
}
}

7.What is the difference between a Companion Object and an object declaration in Kotlin?

A Companion Object is a singleton object that is associated with a class, whereas an object declaration creates a singleton object without association to a class. Companion objects can access the private members of its associated class, whereas regular objects cannot. For example

class MyClass {
companion object {
fun create(): MyClass {
// access private members of MyClass
}
}
}

object MySingleton {
fun doSomething() {
// not associated with any class
}
}

8.What is the difference between a data class and a regular class in Kotlin?

A data class is a class that is specifically designed to hold data, and automatically includes functionality such as equals, hashCode, and toString methods. A regular class is a class that does not include this functionality by default and must be implemented manually. For example,

data class User(val name: String, val age: Int)

class Employee(val name: String, val age: Int) {
override fun equals(other: Any?) = ...
override fun hashCode() = ...
override fun toString() = ...
}

9.How does the when expression work in Kotlin?

The when expression is a concise way to express a switch statement in Kotlin. It evaluates an expression and executes the corresponding branch of code based on the matching value. It also includes an “else” branch as a catch-all. For example,

val x = 2
when (x) {
1 -> print("x is 1")
2 -> print("x is 2")
else -> print("x is neither 1 nor 2")
}

10.Explain the use of extension functions in Kotlin.

Extension functions in Kotlin allow for the adding of new functionality to existing classes without the need to inherit or use design patterns such as decorators. They are defined outside of the class and can be called as if they were part of the class. For example,

fun String.addExclamation(): String {
return "$this!"
}

val myString = "Hello"
println(myString.addExclamation()) //prints "Hello!"

11.How does the null safety feature work in Kotlin?

The null safety feature in Kotlin helps to prevent null pointer exceptions by providing a way to explicitly identify variables that can hold null values. A variable that can hold null is defined by adding a “?” after the variable type. For example,

var myString: String = "Hello"
myString = null // Compile-time error

var myNullableString: String? = "Hello"
myNullableString = null // valid

12.How does the with function work in Kotlin?

The with function in Kotlin is used to call multiple methods on an object without having to repeat the object’s name for each method call. It is especially useful for performing multiple operations on an object in a concise and readable way. For example,

val myTextView = TextView(this)
with(myTextView) {
text = "Hello"
textSize = 20f
setPadding(10, 10, 10, 10)
}

13.How does the apply function work in Kotlin?

The apply function in Kotlin is used to call multiple methods on an object and return the object itself. It is similar to the with function, but it returns the object being operated on, allowing for method chaining. It is useful for configuring an object and then returning it for further use. For example,

val myTextView = TextView(this).apply {
text = "Hello"
textSize = 20f
setPadding(10, 10, 10, 10)
}

In this example, the apply function is used to configure the TextView object and then returned for further use.

14.How does the let function work in Kotlin?

The let function in Kotlin is used to perform an action on an object only if the object is not null. It is useful for avoiding null checks and making the code more readable. It takes a lambda expression as an argument and the object is passed to the lambda as an argument. For example,

val myString: String? = "Hello"
myString?.let { print(it) } // prints "Hello"
val myString: String? = null
myString?.let { print(it) } // does nothing

Prepare these questions for your interview .

Share this blog with other developers who are going to give some Android interview.

All the best for your interview!

For more interview question and their answer please visit the below articles

Kotlin Android Interview Questions — Part 2

Android workmanager interview question and answer

Android interview questions and answer

Beginner Android Interview Question

--

--