Android Interview Questions and Answers

In this tutorial, you’ll learn Android-specific interview questions and answers. By Evana Margain Puig.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

Kotlin

The next questions cover Kotlin as the primary Android programming language. Most of today’s projects are written in Kotlin. And most shops have migrated their older projects to it as part of their ongoing maintenance.

Note: When interviewing for a position as an Android developer, you may also encounter Java questions. Java is out of scope for this article.

Android robot with Kotlin cheatsheet

What Are Coroutines?

Coroutines are pieces of code that convert asynchronous callbacks for long-running tasks into sequential code. They make asynchronous programming much easier in Kotlin and Android.

Coroutines can be a confusing topic if you haven’t used them before. But you’ll love how they make your life easier! To learn more about coroutines, take a look at our Kotlin Coroutines: Fundamentals.

What Are Companion Objects?

Companion Objects are objects in your Kotlin file. They contain functions or properties that are tied to the class rather than to instances of it. They are similar to static members in other languages. Here’s an example:

class MyLoginFragment : Fragment(R.layout.fragment_my_login) {
  
  ...

  companion object {
        private const val TAG = "MyLoginFragment"
    }
}

For more information on companion objects, you can view our video
Companion Objects. This video is one episode of our course Programming in Kotlin: Functions & Custom Types.

How and When Do You Use Nullables in Kotlin?

Kotlin apps distinguish between elements that can be null and those that can’t. This special feature in Kotlin helps you avoid errors from null references. These errors are common in other languages!

Here’s an example of a variable that is not nullable:

var myVariable: String = "variable is not nullable"
myVariable = null //Compiler error. You can't do this!

And here’s what the same variable would look like if it were nullable:

var myVariable: String? = "This is my nullable variable"
myVariable = null

Notice the ? symbol. That’s how you identify an item as nullable.

For a more in detail explanation on nullables, please see our video Create and Consume Nullables. This is one episode in our Programming in Kotlin: Fundamentals
course.

How Do You Do Type Checking in Kotlin?

Kotlin uses the operator is, and its opposite, !is, for type checking. These operators return a boolean value. You run a type check to validate the type of the incoming value before performing an operation on it. Look at this example:

if (name is String) {
    // do my operation if name is a String
}

if (name !is String) { 
    // do my operation if name is not a String
}

This check prevents errors that can occur when you attempt to perform an operation on a value with an unexpected type.

How Do You Declare Switch Statements in Kotlin?

In Kotlin, you use the when keyword where you would use switch in some other languages. Here’s an example:

when (lotteryNumber) {
    329013 -> print("You won!!!")
    else -> print("Sorry you didn't win the lottery, try again!")
}

For a more comprehensive look at the when expression, see our video Simplify Code With When Expressions. This video is part of our course Programming in Kotlin: Fundamentals.

What Is a Lambda Expression?

A lambda is a function that is not declared, but is passed immediately as an expression.

val amountOfCandies = { 
  candiesStored: Int, candiesBought: Int -> candiesStored + candiesBought 
}

Cool right? Simple, concise, and no return statement needed :] Best of all, it can be passed around as a parameter to other functions.

If you want a better understanding of lambdas, see our tutorial Introduction to Kotlin Lambdas: Getting Started.

When Is the Keyword it Used in Lambda Expressions?

When you have a lambda expression with a single parameter, you can use the keyword it to refer to that parameter.

val isPositiveNumber = {
    it > 0
}

Again, you can learn more about lambdas in our tutorial Introduction to Kotlin Lambdas: Getting Started.

What Is a Class Extension?

Extensions exist in many programming languages. As their name states, they “extend” the functionality of a class.

Take a look at this example of an extension on String. This one validates an email address:

// 1
import java.util.regex.Pattern

// 2
fun String.isEmailAddressValid(): Boolean {
  // 3
  val expression = "^[\\w.-]+@([\\w\\-]+\\.)+[A-Z]{2,8}$"
  val pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE)
  val matcher = pattern.matcher(this)
  // 4  
  return matcher.matches()
}

Here’s what you’re doing in this code:

  1. You import the Java library for regex patterns.
  2. You declare the function expression. Notice that the name of the class it’s extending, String, precedes the name of the function isEmailAddressValid().
  3. Here you create three constants:
    • The first constant contains a regular expression for emails.
    • The second constant creates a pattern matcher. Notice that it is not case sensitive.
    • The third constant makes it a pattern matcher.
  4. Finally, you run matches on it. This function returns a boolean value that tells you whether the data entered is an email or not.

Extensions are very useful. Demonstrating that you know how to apply them correctly will definitely help your interview! Read more on this topic in our article Kotlin Android Extensions.

What Are Ranges in Kotlin?

The term range is very similar to its equivalent in math. It represents a group of numbers that goes from a lower to a higher bound (or the other way around). Look at the example below to see how it looks in code:

i in 1..10  // equivalent of 1 <= i && i <= 10

Notice the range has two dots between the numbers. To iterate down instead up, use the keyword downTo instead of the two dots:

i in 10 downTo 1

If you want to broaden your knowledge of the Kotlin language, review our Programming in Kotlin: Fundamentals and Programming in Kotlin: Functions & Custom Types courses.

Object-Oriented Programming

As an Android developer, you will write code using Object-Oriented Programming. Many interviews will include general questions on OOP.

What Is Polymorphism?

The word polymorphism means having multiple forms. In OOP, the most general definition of polymorphism is the ability to define one interface or class with many implementations. In Android, there are two types of polymorphism, dynamic and static.

In dynamic polymorphism, the type of object on which method is being invoked is not known at compile time but will be decided at run time. For example, a parent class Animal might have two subclasses, Dog and Cat, both inheriting from Animal.

Static polymorphism refers to constructors. You can have multiple constructors with the same name, but they can receive different parameters. The intended constructor will be executed depending on the arguments passed.

For example, a school system uses an app to maintain student information. You write a constructor to create a student object:

fun createStudent(name: String, nickname: String) {
  addStudentToDatabase(name: String, nickname: String)
}

Some students, but not all of them, play sports. You write another constructor to create an object for a student who plays sports:

fun createStudent(name: String, nickname: String, sport: Sport) {
  addStudentToDatabase(name: String, nickname: String)
  assignSportToStudent(sport: Sport)
}

This second constructor will also create a student object, just as the first one does. But afterward, it will also assign the desired sport. The compiler determines which constructor to execute based on the parameters it receives.