Below are the list of few Android Interview questions being asked nowadays.
Q1. What is context ?
Context is the interface that contain global information about application environment. Context allows to access application-specific resources(strings,themes,assets etc.) and classes. Likewise, It deals with application-level operations such as launching activities, broadcasting and receiving intents. In short, Context represents a handle to get environment data.
Q2. Solid Principles :
- Single Responsibility Principle
- Open-Closed Principle
- Liskov Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
Q3. What are launch modes ?
There are five launch modes you can assign to the launchMode
attribute:
"standard"
The default mode. The system creates a new instance of the activity in the task it was started from and routes the intent to it. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances."singleTop"
If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to itsonNewIntent()
method, rather than creating a new instance of the activity. The activity is instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity)."singleTask"
The system creates the activity at the root of a new task or locates the activity on an existing task with the same affinity. If an instance of the activity already exists, the system routes the intent to the existing instance through a call to itsonNewIntent()
method, rather than creating a new instance. Meanwhile all of the other activities on top of it are destroyed.Note: Although the activity starts in a new task, the Back button and gesture still return the user to the previous activity."singleInstance"
.The behavior is the same as for"singleTask"
, except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task. Any activities started by this one open in a separate task."singleInstancePerTask"
.The activity can only run as the root activity of the task, the first activity that created the task, and therefore there can only be one instance of this activity in a task. In contrast to thesingleTask
launch mode, this activity can be started in multiple instances in different tasks if theFLAG_ACTIVITY_MULTIPLE_TASK
orFLAG_ACTIVITY_NEW_DOCUMENT
flag is set.
Q4. How many type of Dispatchers are there ?
There are majorly 4 types of Dispatchers.
- Main Dispatcher : It starts the coroutine in the main thread. It is mostly used when we need to perform the UI operations within the coroutine, as UI can only be changed from the main thread
- IO Dispatcher : it is used to perform all the data operations such as networking, reading, or writing from the database, reading, or writing to the files
- Default Dispatcher : We should choose this when we are planning to do Complex and long-running calculations, which can block the main thread and freeze the UI eg: Suppose we need to do the 10,000 calculations and we are doing all these calculations on the UI thread
- Unconfined Dispatcher : As the name suggests unconfined dispatcher is not confined to any specific thread. It executes the initial continuation of a coroutine in the current call-frame and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without mandating any specific threading policy.
Q5. What is the difference between MVP and MVVM ?
MVP : View has instance of presenter. Also Presenter has instance of View back in the form of interface which makes it tightly coupled.
MVVM : View has instance of ViewModel. But ViewModel doesn’t have instance of View. It opens streams of event which view needs to observe in order to listen and take action. Hence it is loosely coupled.
Q6. How Viewmodel survives configuration changes ?
We use a ViewModelProvider to instantiate a ViewModel. And we pass a ViewModelStoreOwner when we create a ViewModelProvider. And activity overides getViewModelStore() , so it owns a responsibility to retain the same instance in case of config changes.
Read more here —
https://proandroiddev.com/the-curious-case-of-survival-of-viewmodel-afe074992fbc
https://proandroiddev.com/how-viewmodel-works-under-the-hood-52a4f1ff64cf
Q7. Difference between LiveData and Flow
- Backpressure handling: Flow provides built-in support for backpressure, allowing control over the rate of data emission and processing, whereas LiveData doesn’t support backpressure handling.
- Sequential vs. parallel processing: Flow offers a rich set of operators for sequential and structured processing, while LiveData focuses on delivering the latest data to observers.
- Kotlin Coroutines integration: Flow is tightly integrated with Kotlin Coroutines, providing a cohesive asynchronous programming experience, whereas LiveData is not specifically tied to coroutines.
Q8. What is ANR in Android?
ANR(Application is Not Responding) is a popup that appears when the application is not responding. ANR dialogue is displayed whenever the main thread within an application has been unresponsive for a long time under the following conditions:
- When there is no response to an input event even after 5 seconds.
- When a broadcast receiver has not completed its execution within 10 seconds.
Q9. What is the content provider?
Content provider is one of the primary building blocks of Android applications, which manages access to a central repository of data. It acts as a standard interface that connects data in one process with code running in another process. So it can be used to share the data between different applications.
They are responsible for encapsulating the data and providing mechanisms for defining data security. It is implemented as a subclass of ContentProviderclass and must implement a set of APIs that will enable other applications to perform transactions.
Q10. What is the difference between compileSdkVersion and targetSdkVersion?
compileSdkVersion:
- The compileSdkVersion is the version of API the application is compiled against. You can use Android API features involved in that version of the API (as well as all previous versions).
- For example, if you try and use API 33 features but set compileSdkVersion to 31, you will get a compilation error. If you set compileSdkVersion to 33 you can still run the app on an API 31 device as long as your app’s execution paths do not attempt to invoke any APIs specific to API 33.
targetSdkVersion:
- The targetSdkVersion indicates that you have tested your app on (presumably up to and including) the version you specify. This is like a certification or sign-off you are giving the Android OS as a hint to how it should handle your application in terms of OS features.
- For example, setting the targetSdkVersion value to “33” or higher permits the system to need apps to request POST_NOTIFICATIONS permission on Android 33 or above.
Q11. What is difference between Deep Links vs Android App Links:
App Links are just deep links that have been verified for a website, AND allows opening URLs in the associated app directly without asking the user to select the app (via the disambiguation dialog). With App Links, your app designates itself as the default handler of a given type of link (though the user can override it from device system settings)
Q12. Difference between commit and apply in sharedpreferences
- commit() — writes the data synchronously (blocking the thread its called from). It then informs you about the success of the operation.
- apply() — schedules the data to be written asynchronously. It does not inform you about the success of the operation.
Q13. Difference between Serializable and Parcelable
Serializable interface is not a part of Android SDK and it uses reflection for marshaling operations and creates lots of temp objects. In Parcelable, you are able to choose which field you want to serialize. Because of the temp object creation and garbage collection, Serialization is slower than Parcelable
Q14. Difference between Hot and Cold Observable
When the data is produced by the Observable itself, we call it a cold Observable. When the data is produced outside the Observable, we call it a hot Observable.
https://luukgruijs.medium.com/understanding-hot-vs-cold-observables-62d04cf92e03
Q15. What is the difference between launch() and runBlocking in coroutines ?
launch() — Use this when you want to perform tasks asynchronously without blocking the current thread. It is suitable for tasks like making network requests, updating UI elements, or performing background computations.
runBlocking() — use this when you need to write sequential code that includes suspending functions or when testing suspending functions. The runBlocking() coroutine builder is used to start a new coroutine and block the current thread until the coroutine completes.
Q16. Difference between launch() and async() ?
Ref : https://www.geeksforgeeks.org/launch-vs-async-in-kotlin-coroutines/
— — — — — — — — — — — — — — — -
For Questions specific to Kotlin , refer this — https://kapilvij.medium.com/kotlin-interview-cheat-sheet-c62e7850ba73
That’s all folks.
But Keep watching this space coz I will keep updating the same article