Acing the Android interview — GlorySheet
This article would take you through a curated set of Interview Questions and answers from Java, Kotlin, and Android that one might expect in an Interview while applying for the position of an Android Developer (Beginner-Intermediate).
Java Questions
Q) What is inheritance
in Java?
Ans: Inheritance in Java is a mechanism in which one object acquires all the properties and behaviors of a parent object. It is an important part of OOPs (Object Oriented programming system).
The idea behind inheritance in Java is that you can create new classes that are built upon existing classes. When we inherit from an existing class, we can reuse methods and fields of the parent class.
Q) What are the other OOP
concepts?
Ans: Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects. It simplifies software development and maintenance by providing some concepts:
Q) What do you mean by the Volatile
keyword in Java?
Ans: volatile
keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache and that writes to a volatile variable will be written to main memory, and not just to the CPU cache. The Java volatile
keyword guarantees visibility of changes to variables across threads. Read more.
Q) What is the Synchronized keyword in Java?
Ans: Java provides a way of creating threads and synchronizing their task by using synchronized blocks. Synchronized blocks in Java are marked with the synchronized keyword. A synchronized block in Java is synchronized on some object. All synchronized blocks, synchronized on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.
Android Questions
Q) What is context
in Android?
Ans: Context is an interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
If you want to get in-depth knowledge about contexts in Android, read :
Q) What is an intent
in Android?
Ans: An Intent
is a messaging object you can use to request an action from another app component. Although intents facilitate communication between components in several ways, there are three fundamental use cases:
- Starting an activity
- Starting a service
- Delivering a broadcast
Q) How does an Android app start?
Ans: We are all aware of how the main function is the entry point of a Java/Kotlin program. Similarly in and Android app, we use an intent-filter
inside the main activity tag of our AndroidManifes.xml
file :
Here the <category android:name=”android.intent.category.LAUNCHER” />
tells Android that anything which is treated as a Launcher by the Android system should show an icon for this activity.
And the <action android:name=”android.intent.action.MAIN” />
denotes that this particular activity is the entry point of the application very much like the main
function we spoke about. You can read more here.
Q) What is the difference between Service
, Intent-Service
, AsyncTask
and Thread
?
Ans: The differences are:
- Thread — A thread is a thread of execution in a program. The Java Virtual Machine allows an application to have multiple threads of execution running concurrently. We can create a thread by extending the
Thread
class or implementing theRunnable
interface and then calling thestart
method for each. - Service — A
Service
is an application component that can perform long-running operations in the background. It does not provide a user interface. Once started, a service might continue running for some time, even after the user switches to another application. Additionally, a component can bind to a service to interact with it and even perform interprocess communication (IPC). The most important thing to remember here is, even though we say that aService
performs operations in background, it will by default use the thread from which it was started (often the main thread in Android apps) unless we move to the background thread explicitly. - Intent-service — IntentService is a sub class of
Service
class that handles asynchronous requests (expressed asIntent
s) on demand. Clients send requests throughContext.startService(Intent)
calls; the service is started as needed, handles each Intent in turn using a worker thread (and not the main thread) inherently, and stops itself when it runs out of work. - AsyncTask —
AsyncTask
was originally supposed to help developers move off the main thread to a worker thread. However, it caused a lot of context-leaks, missed callbacks, etc and thus is not advisable to use. Also as of API level 30, it is deprecated. It is advisable to use RxJava or Kotlin Coroutines in place ofAsyncTask
to perform background jobs
Q) What are the various activity launch-modes
?
Ans: The following are the various launch modes in Android
- Standard: It creates a new instance of an activity in the task from which it was started. Multiple instances of the activity can be created and multiple instances can be added to the same or different tasks.
Eg: Suppose there is an activity stack of A -> B -> C.
Now if we launch B again with the launch mode as “standard”, the new stack will be A -> B -> C -> B. - SingleTop: It is the same as the standard, except if there is a previous instance of the activity that exists in the top of the stack, then it will not create a new instance but rather send the intent to the existing instance of the activity.
Eg: Suppose there is an activity stack of A -> B.
Now if we launch C with the launch mode as “singleTop”, the new stack will be A -> B -> C as usual.
Now if there is an activity stack of A -> B -> C.
If we launch C again with the launch mode as “singleTop”, the new stack will still be A -> B -> C. - SingleTask: A new task will always be created and a new instance will be pushed to the task as the root one. So if the activity is already in the task, the intent will be redirected to onNewIntent() else a new instance will be created. At a time only one instance of activity will exist.
Eg: Suppose there is an activity stack of A -> B -> C -> D.
Now if we launch D with the launch mode as “singleTask”, the new stack will be A -> B -> C -> D as usual.
Now if there is an activity stack of A -> B -> C -> D.
If we launch activity B again with the launch mode as “singleTask”, the new activity stack will be A -> B. Activities C and D will be destroyed. - SingleInstance: Same as single task but the system does not launch any activities in the same task as this activity. If new activities are launched, they are done so in a separate task.
Eg: Suppose there is an activity stack of A -> B -> C -> D. If we launch activity B again with the launch mode as “singleInstance”, the new activity stack will be:
Task1 — A -> B -> C
Task2 — D
Q) What are the lifecycle changes that occur when going from Activity A to Activity B and back?
Ans: Let Say Activity A is starting Activity B by Calling StartActivity(Intent) method then lifecycle call be like this:-
- A onCreate()
- A onStart()
- A onResume()
Now Button click for startActivity(intent)
- A onPause()
- B onCreate()
- B onStart()
- B onResume()
- A onStop()
Now, If you press back button from Activity B then lifeCycle call will be :
- B onPause()
- A onRestart()
- A onStart()
- A onResume()
- B onStop()
- B onDestory()
Now, one more scenario “From Activity A start Activity B by calling StartActivity(Intent) on button click and use finish() method inside onstart() method on Activity B”
- A onPause()
- B onCreate()
- B onStart()
- A onResume()
- B onStop()
- B onDestory()
Q) What are the differences between MVP
and MVVM
architecture?
Ans: MVP stands for Model View Presenter. Where Model
is the data layer that acts as a single source of truth for all our data requirements. The View
layer describes the UI
and other view components. The Presenter
has the presentation logic in it which decides when and how a certain data obtained from the model should be presented on to the screen.
MVVM stands for Model View ViewModel. Where Model
is the data layer that acts as a single source of truth for all our data requirements. The View
layer describes the UI
but also knows how to present certain data on to the screen. The ViewModel is responsible for providing reactive streams of data from the data layer to the view layer and also maintains the data during orientation changes or in other cases when the view needs to be recreated.
The main differences are :
- The
Presenter
inMVP
is highly coupled with theView
and you cannot share a presenter with multiple views. Whereas, in the case ofViewModel
, you can easily share oneViewModel
with multipleViews
as there is little or no coupling. - The
Presenter
inherently does not survive beyond the lifecycle of the view, whereas theViewModel
can survive beyond the lifecycle of the view.
To know more about how ViewModel
survives beyond the View
, below is a very good article describing the same.
Q) Mention some features of Clean Code
?
Ans: Some features of Clean Code are:
- Following the SOLID principles
- Proper and indicative naming of
classes
andmethods
- Limiting the number of arguments of a
method
to as less as possible - Writing
unit tests
with respectable coverage
Q) What is LiveData
?
Ans: LiveData
is an observable data holder class. Unlike a regular observable, LiveData is lifecycle-aware, meaning it respects the lifecycle of other app components, such as activities, fragments, or services. This awareness ensures LiveData only updates app component observers that are in an active lifecycle state. It is generally used with a ViewModel
.
Q) What is DataBinding
?
Ans: The Data Binding Library is a support library that allows you to bind UI components in your layouts to data sources in your app using a declarative format rather than programmatically. Please check the basic implementation steps here.
Q) What is ViewBinding
?
Ans: View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module. An instance of a binding class contains direct references to all views that have an ID in the corresponding layout. Please check the basic implementation steps here.
Q) What are the Changes in Android 11?
Ans: The most significant changes in Android 11 are:
- The use of one-time permissions
- Repeated denials of a permission implies “don’t ask again”
- If users haven’t interacted with an app for a few months, the system auto-resets the app’s sensitive permissions
- Access into external storage directories is limited to an app-specific directory and specific types of media that the app has created
- When an app queries for the list of installed apps on the device, the returned list is filtered.
Q) What are the SOLID
principles?
Ans: SOLID principles stand for
- Singe responsibility principle
- Open/closed principle
- Liskov’s substitution principle
- Interface segregation principle
- Dependency Injection principle
To understand them in greater detail please read:
Q) Example of a dependency injection tool in Android?
Ans: Dagger is the most widely used DI tool in Android. However, the Android team has released Hilt as a part of the Android Jetpack Components
. Hilt
acts as a wrapper around Dagger
and makes it much easier to implement DI
in an app with much less boilerplate code.
Q) What is the difference between Serializable and Parcellable?
Ans: Serializable is a standard Java interface. It is not a part of the Android SDK
. Its simplicity is its beauty. Just by implementing this interface your POJO will be ready to jump from one Activity to another. However, Serializable uses reflection
and hence is slow and creates a lot of temporary objects during the process.
Parcelable is a similar interface, but it is a part of the Android SDK. Parcelable was specifically designed in such a way that there is no reflection when using it. That is because we are being really explicit for the serialization process. Thus it is much faster than Serializable and does not create temporary objects.
Please check how to use them here.
Q)Why do you get TransactionTooLargeException
exception while navigation to a different activity?
Ans: As we all know, to pass arguments between activities or fragments, we use Bundle
. We serialize and write the data into the bundle and then when required, we read the data out of the Bundle, deserialize it, and then use it as required. However, there is an upper limit(1 MB) to the amount of data you can write into a Bundle. Thus, if you ever put data (eg.- an Image bitmap) exceeding 1MB into a bundle and try to make a transaction using it, the transaction will throw TransactionTooLargeException
.
Kotlin Questions
Q) Difference between collections.filter
, list.any
and list.find
keyword of list in Kotlin?
Ans: The filter
keyword is used to filter a collection’s items based on any provided predicate. Let’s understand with an example :
The any
keyword is used to get a boolean value which signifies if any element in the collection matches the provided predicate. If yes, return true else return false. Example:
The find
keyword is used to find the first element which matches the supplied predicate. Incase none of the elements match the predicate, it reutrns null
. Example:
Q) What does the transient
keyword do?
Ans: The transient keyword is used to avoid serialization. If any object of a data structure is defined as a transient, then it will not be serialized.
Q) How would you define a Singleton class in Kotling?
Ans: Using the object
keyword.
Q) What are Coroutines
in Kotlin?
Ans: A coroutine
is a concurrency design pattern that you can use on Android to simplify code that executes asynchronously.
Noteworthy features include the following:
- Lightweight: You can run many coroutines on a single thread due to support for suspension, which doesn’t block the thread where the coroutine is running. Suspending saves memory over blocking while supporting many concurrent operations.
- Fewer memory leaks: Use structured concurrency to run operations within a scope.
- Built-in cancellation support: Cancellation is propagated automatically through the running coroutine hierarchy.
- Jetpack integration: Many Jetpack libraries include extensions that provide full coroutines support. Some libraries also provide their own coroutine scope that you can use for structured concurrency.
To know more about Coroutines in-depth, I can recommend this article:
Q) How does an `Extension` function work in Kotlin?
Ans: Extension functions are resolved statically, i.e. they are normal static methods bearing no connection with the class they are extending, other than taking an instance of this class as a parameters. The target classes are not modified. Extensions are visible from Java reflection as static methods in the classes they are defined in (.i.e. in package-classes for top-level extension functions).
Q) Difference between Inline
, NoInline
and CrossInline
functions in Kotlin?
Ans: Inline function — In Kotlin, the higher order functions or lambda expressions, all stored as an object so memory allocation, for both function objects and classes, and virtual calls might introduce runtime overhead. Sometimes we can eliminate the memory overhead by inlining the lambda expression.
In order to reduce the memory overhead of such higher order functions or lambda expression we can use the inline
keyword which ultimately requests the compiler to not allocate memory and simply copy the inlined code of that function at the calling place.
NoInline function — Incase you have multiple lambdas in your inlined function and you don’t want all of them to be inlined, you can mark the lambdas you don’t want to be inlined with the noinline
keyword
CrossInline function — The crossinline
marker is used to mark lambdas that mustn’t allow non-local returns, especially when such lambda is passed to another execution context such as a higher order function that is not inlined, a local object or a nested function. In other words, you won’t be able to do a return
in such lambdas.
To get more depth, please read:
Bonus Questions
Q) How would you reduce your app download size?
Ans: A few ways to optiize the size of the apk delivered to the user are:
- Using
vector assets
instead ofpngs
- Code obfuscation by enabling R8 for release builds
- Building and uploading
.aab
file to the play store instead of.apk
file. For more details on app bundles, read this
Q) Say if you suddenly see a drop of 30% daily active users on your app, how would you debug it?
Ans: To debug the scenario, I would try the following steps:
- Check if there is any crash report, that occured a lot of times recently, in my analytics dashboard. If any such issue is present, I will go forward and debug it using the logs from the analytics dashboard.
- If no such report is present, I will check the health monitor of all my backend services to see if the APIs are up and working properly.
- If the above two didn’t help, then, in that case, we need to check the funnels in our crash reporting tool and see if users are dropping off at a particular step. Say you have steps A — B — C and you see that a lot of users are dropping off after step B, then we can go forward and perform steps A and step B manually and debug the issue as there might be some problem when going from step B to step C.
That’s all folks. I hope this glorysheet helps you. All the best for your upcoming interviews. Hope you can ace it! 👍
If you liked it, please help me with your precious 👏 to this article. Thanks.