Tuesday, March 19, 2024

Android Interview Questions and Answers

Android Interview Questions help you kickstart your career into Android Development and hack yourself into Top companies. This page will provide you various android interview questions on topics like dagger, dagger2, RxJava, RxJava2, RxAndroid, Services, Android Framework etc.

Want to contribute to the interview Questions? Create a pull request in Github.

Related Links:

Java Interview Questions

These Android Interview Questions are being updated time to time. For earlier updates of these interview questions and to get the latest copy of PDF for Android Interview Questions, subscribe to our NewsLetter.

[adinserter block=”3″]

Android Interview Questions And Answers

Android Interview Questions Coderefer Thumbnail

Curated List of Real-time Android Interview Questions. Help fellow developers by contributing to these interview Questions – Create a pull request in Github.

Quick Jump to Topics:

Core Android

  • What are SOLID Principles? How they are applicable in Android?
    A) SOLID unites all the best practices of software development over the years to deliver good quality apps. Understanding SOLID Principles will help us write clean and elegant code. It helps us write the code with SOC (Separation of Concerns).
    SOLID Principles is an acronym for:

    1. S stands for Single Responsibility Principle(SRP) – A class should have only one reason to change
    2. O stands for Open Closed Principle – Software entities such as classes, functions, modues should be open for extension but closed for modification.
    3. L stands for Liskov Substitution Principle – Derived class must be usable through the base class interface, without the need for user to know the difference.
    4. I stands for Interface Segregation – No client should be forced to depend on methods that it doesn’t use.
    5. D stands for Dependency Inversion –
      1. High Level Modules should not directly depend on Low level modules. Instead both should depend on abstractions.
      2. Abstractions should not depend on details. Details should depend on abstractions.

    Learn More about SOLID principles with Android Examples Here.

  • Android Architecture
    A)
    Android Architecture Image

  • What are Android Components?
    A) 1) Activities,

    1. Intent and broadcast receivers,
    2. Services,
    3. Content Providers,
    4. Widgets and Notifications
  • What is an Application class?
    A) An Application class is a base class in your Application starts before all other classes like Activities or services are called. You can maintain your application’s global state here. While it is NOT mandatory that you need to extend Application class, you can do so by providing your own implementation by creating a subclass and specifying the fully-qualified name of this subclass as the “android:name” attribute in your AndroidManifest.xml’s tag.

  • What is a Context? What are different types of Contexts?
    A) As the name says, its the context of the current application or object. Context is like a handle to the environment your application is currently running in.
    We mainly use two types of context. Application context – whose scope is throughout the application and Activity Context – whose scope depends on the Activity Lifecycle.

  • What is an Activity?
    A) An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app. For instance, one of an app’s activities may implement a Preferences screen, while another activity implements a Select Photo screen.

  • Activity Lifecycle
    A)
    Activity Lifecycle Image

  • Fragment Lifecycle
    A)
    Fragment Lifecycle Image

  • Service Lifecycle
    A)
    Fragment Lifecycle Image

  • What is the correlation between activity and fragment life cycle?

  • A)
    Here is how Activity’s and Fragment’s lifecyle are called together:
    Activity Fragment Lifecycle

  • Is there any scenario where onDestoy() will be called without calling onPause() and onStop()?
    A) If we call finish() method inside onCreate() of our Activity, then onDestroy() will be called directly.

  • How do we save and restore an activity’s state during screen screen rotation?
    A) We can use onSavedInstanceState(bundle:Bundle) to save the activity’s state inside a bundle. Then we can use onRestoreInstanceState(bundle) to restore the state of activity. However with Modern Android Development(MAD), we can use ViewModel to achieve the same and it is the recommended approach.

  • What is an Intent Filter?
    A) Intent filters are a very powerful feature of the Android platform. They provide the ability to launch an activity based not only on an explicit request, but also an implicit one. For example, an explicit request might tell the system to “Start the Send Email activity in the Gmail app”. By contrast, an implicit request tells the system to “Start a Send Email screen in any activity that can do the job.” When the system UI asks a user which app to use in performing a task, that’s an intent filter at work. Here’s an example of how to declare Intent Filter in AndroidManifest:

    <activity android:name=".ExampleActivity" android:icon="@drawable/app_icon">
      <intent-filter>
          <action android:name="android.intent.action.SEND" />
          <category android:name="android.intent.category.DEFAULT" />
          <data android:mimeType="text/plain" />
      </intent-filter>
    </activity>
  • What is an Intent?
    A) It is a kind of message or information that is passed to the components. It is used to launch an activity, display a web page, send SMS, send email, etc. There are two types of intents in android:
    a)Implicit Intent
    b)Explicit Intent

  • What is AAPT?
    A) AAPT2 (Android Asset Packaging Tool) is a build tool that Android Studio and Android Gradle Plugin use to compile and package your app’s resources. AAPT2 parses, indexes, and compiles the resources into a binary format that is optimized for the Android platform.

  • What are the different types of Intents?
    A) There are two types of intents:

    Explicit intents specify which application will satisfy the intent, by supplying either the target app’s package name or a fully-qualified component class name. You’ll typically use an explicit intent to start a component in your own app, because you know the class name of the activity or service you want to start. For example, you might start a new activity within your app in response to a user action, or start a service to download a file in the background.
    Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.

  • What is HandlerThread?
    A) HandlerThread is a Handy class to start a thread that has a Looper.

  • What is a Looper?
    A) A Looper is a class used to loop through the Message Queue attached to the Thread. By default, a thread halts when the execution completes. But, for Example, if we take Android’s Main thread, it should not halt upon execution.
    Rather it should loop through the runnables(Messages) that its assigned in order to work properly. For more info, refer to this link.

  • What is a Service?
    A) A service is a component which doesn’t have UI and can perform long running operations like downloading stuff, playing music etc.. which can run even exiting the application. By default service runs on main thread. This might cause ANR errors. To avoid this, we can Start service by creating a new background thread or use an IntentService that can do work in background. Read More.

  • How to Stop a Service?
    A) To stop a service from an activity we can call stopService(Intent intent) method. To Stop a service from itself, we can call stopSelf() method.

  • What are different types of services?
    A) These are the three different types of services:

    Foreground Service:
    A foreground service performs some operation that is noticeable to the user. For example, an audio app would use a foreground service to play an audio track. Foreground services must display a Notification. Foreground services continue running even when the user isn’t interacting with the app.
    Background Service:
    A background service performs an operation that isn’t directly noticed by the user. For example, if an app used a service to compact its storage, that would usually be a background service. However there are restrictions to use background services from Android API 26 and above. We can use WorkManager to defer these background tasks.
    Bound Service:
    A service is bound when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, receive results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed by the system.
    Read More

  • Bound Service vs UnBounded service?
    A) A Bound service is started by using method bindService(). As mentioned above system destroys bound service when no application component is accessing it.
    Unbounded service (started service) is started by using a method called startService(). Once started, it will run indefinitely even if the application component that started it is destroyed.

  • When does a Bound Service stops?
    A) A Bound Service will stop automatically by the system when all the Application Components bound to it are unbinded.

  • What is an Intent Service?
    A) IntentService is a Service that can perform tasks using worker thread unlike service that blocks main thread.

  • What is the difference between START_NOT_STICKY, START_STICKY AND START_REDELIVER_INTENT?
    A) START_NOT_STICKY:
    If the system kills the service after onStartCommand() returns, do not recreate the service unless there are pending intents to deliver. This is the safest option to avoid running your service when not necessary and when your application can simply restart any unfinished jobs.
    START_STICKY:
    If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand(), but do not redeliver the last intent. Instead, the system calls onStartCommand() with a null intent unless there are pending intents to start the service. In that case, those intents are delivered. This is suitable for media players (or similar services) that are not executing commands but are running indefinitely and waiting for a job.
    START_REDELIVER_INTENT:
    If the system kills the service after onStartCommand() returns, recreate the service and call onStartCommand() with the last intent that was delivered to the service. Any pending intents are delivered in turn. This is suitable for services that are actively performing a job that should be immediately resumed, such as downloading a file.

  • What is Pending Intent?
    A)A PendingIntent is a token that you give to a foreign application (e.g. NotificationManager, AlarmManager, Home Screen AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application’s permissions to execute a predefined piece of code. It specifies a task that requires to be performed in future.

  • What is the method that differentiates it to make Service run in background?
    A) onHandleIntent() is the method that helps the IntentService to run a particular code block declared inside it, in worker/background thread.

  • How to Stop an IntentService?
    A) An IntentService automatically stops itself after its job is done. We do not need to explicitly call any methods to stop an IntentService unlike Service which requires stopSelf() or StopService(intent:Intent).

  • When Intent Service is Useful?
    A) The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

  • Advantage of Retrofit over Volley?
    A) Retrofit is type-safe. Type safety means that the compiler will validate types while compiling, and throw an error if you try to assign the wrong type to a variable.

  • Advantage of Volley over Retrofit?
    A) Android Volley has a very elaborate and flexible cache mechanism. When a request is made through Volley, first the cache is checked for Response. If it is found, then it is fetched and parsed, else, it will hit Network to fetch the data. Retrofit does not support cache by default.

  • What are different launch modes available in Android?
    A) There are four launch modes for an Activity in Android as follows:

    1. standard : Creates a new instance of an activity in the task from which it is started every single time. It is the default mode if not declared.

      Eg: If we have an activity stack of A->B->C, If we launch Activity C again using standard Mode, the activity stack will now be A->B->C->C. We can see that two instances of C are present in the activity stack.

    2. singleTop : Same as standard except that if the activity is at the top of the stack, then the same instance will be used. Now the existing Activity at the top will receive the intent through a call to its onNewIntent() method.

      Eg: If we have an activity stack of A->B->C, If we launch Activity C again using singleTop Mode, the activity stack remains to be A->B->C. However if we launch B, then B will be added as new Instance to the stack (A->B->C->B).

    3. singleTask : A new task will be created and activity will be created at the root of this new task whenever we use launch mode as singleTask. However, if there is already a separate task with same instance, the system will call that activity’s onNewIntent() method to route the intent. There can only be one instance of activity existing at a time.

      Eg: If our activity stack is A->B->C and if we launch D using singleTask, it will be A->B->C->[D]. Here braces represents the stack in separate stack. If we call E using standard mode, then it will be A->B->C->[D->E].
      If we have A->B->C and if we call B again using singleTask launch Mode, the stack will now be A->B with B in a separate task. Activity C will be destroyed.

    4. singleInstance : the activity will be created in a new task, and that task will contain only that activity. Also only one instance of that activity will be available for all the tasks.

      Eg: if the Activity stack is A->B and now we launched C using singleInstance Launch Mode, the new stack will be A->B->[C]. Now if we launch D from activity B, Then new stack will be A->B->D [C]. If we call C again, onNewIntent() of C will be called and new stack will be A->B->D->[C].

    You can read more about them here.

  • How to handle crashing of AsyncTask during screen rotation?
    A) The best way to handle AsyncTask crash is to create a RetainFragment, i.e., a fragment without UI as shown in the gist below: https://gist.github.com/vamsitallapudi/26030c15829d7be8118e42b1fcd0fa42
    We can also avoid this crash by using RxJava instead of AsyncTask as we will be subscribing and unsubscribing at onResume() and onPause() methods respectively.

  • Difference between serializable and parcelable? Why android introduced Parcelable?
    A) Serializable uses reflection while for parcelable, developers from android team wrote custom code that performs manual marshalling(converting data into byte stream) and unmarshalling(converting the byte stream back to their original data). Usually Parcelable is considered faster than Serializable.

  • How to reduce your app size?
    A)

    1. setting minifyEnabled to true
    2. setting shrinkResources to true
    3. using bundle instead of apk in developer console
    4. converting the images to vector drawables.
  • What is the advantage of using Retrofit over AsyncTask?
    A) Retrofit reduces boiler plate code by internally using GSON library which helps parsing the json file automatically.
    Retrofit is a type safe library. This means – it checks if wrong data type is assigned to variables at compilation time itself.
    More use-cases at: https://stackoverflow.com/a/16903205/3424919

  • How to handle multiple network calls using Retrofit?
    A) In Retrofit, we can call the operations asynchronously by using enqueue() method where as to call operations synchronously, we can use execute() method. In addition, we can use zip() operator from RxJava to perform multiple network calls using Retrofit library.

  • What is the role of Presenter in MVP?
    A) The Presenter is responsible to act as the middle man between View and Model. It retrieves data from the Model and returns it formatted to the View. But unlike the typical MVC, it also decides what happens when you interact with the View.

  • What is the advantage of MVVM over MVP?
    A) In MVP, Presenter is responsible for view data updates as well as data operations where as in MVVM, ViewModel does not hold any reference to View. It is the View’s responsibility to pick the changes from ViewModel. This helps in writing more maintainable test cases since ViewModel does not depend upon View.

  • When to use AsyncTask and when to use services?
    A) Services are useful when you want to run code even when your application’s Activity isn’t open. AsyncTask is a helper class used to run some code in a separate thread and publish results in main thread. Usually AsyncTask is used for small operations and services are used for long running operations.

  • When to use a service and when to use a thread?
    A) We will use a Thread when we want to perform background operations when application is running in foreground. We will use a service even when the application is not running.

  • What is a Handler?
    A) A Handler allows you to send and process Message and Runnable objects associated with a thread’s MessageQueue. Each Handler instance is associated with a single thread and that thread’s message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it — from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue. We will generally use handler class when we want to repeat task every few seconds.

  • How to save password safely in Android?
    A) Using Android Keystore
    https://medium.com/@josiassena/using-the-android-keystore-system-to-store-sensitive-information-3a56175a454b

  • String a = “abc”; String b = new String(“abc”); Will a == b ??
    A) It depends. Here with the first statement, i.e, String a = “abc”, JVM will search for a string with “abc” in String constant pool(SCP) and if its not there it will create a new Object.
    If we wrote second statement similarly, i.e., String b = “abc”, then b will point to same string from SCP.
    However, String b = new String(“abc”) always creates a new String object.

  • What is Alarm Manager?
    A) AlarmManager is a class which helps scheduling your Application code to run at some point of time or at particular time intervals in future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

  • How can I get continuous location updates in android like in Google Maps?
    A) We can use Fused location provider in Android set our interval in that.
    https://stackoverflow.com/a/41500910/3424919

Android Security Related

  • How do you know if the device is rooted?
    A) We can check if superUser apk is installed in the device or if it contains su file or xbin folder. Alternatively you can use RootBeer library available in GitHub.

    For code part, click Here.
  • What is Symmetric Encryption?
    A) Symmetric encryption deals with creating a passphrase and encrypting the file with it. Then the server needs to send the key to the client so that the client can decrypt. Here the problem is sending that key to decrypt the file. Hackers can easily access that key and could misuse the data.
  • What is Asymmetric Encryption?
    A) Using algorithms like RSA, the server generates 2 keys – public key and private key. The server then gives public key to clients. Client then encrypts the sensitive data with that public key and send it back to server. Now as the server alone has the private key, only it can decrypt the data. This is the most efficient way of sending data across the client and server.

    Example of this Asymmetric encryption are HTTPS using SSL certificate, Bitcoin, etc.
    For more info, refer to this video

Android Battery Related

  • How do you reduce battery consumption?
    A)

    1. Never poll the server for updates.
    2. Sync only when required. Ideally, sync when phone is on Wi-Fi and plugged in.
    3. Defer your work using WorkManager.
    4. Compress your data
    5. Defer non immediate requests until the phone is plugged in or wifi is turned on. The Wi-Fi radio uses significantly less battery than the mobile radio.
  • How do you improve battery while fetching location for an app?
    A)

    1. By changing Accuracy -> we can use setPriority() to PRIORITY_LOW_POWER
    2. By changing Frequency of fetching location -> we can use setInterval() to specify the time interval
    3. By increasing latency -> After our call, we can wait for longer time – we can use setMaxWaitTime() to set large timeout.

Dagger 2 Related Questions:

  • What is the use-case of @BindsInstance Annotation?
    A) @BindsInstance is used to bind the available data at the time building the Component. Suppose I have user name available before building a component then I can use as shown in the following example:
    https://google.github.io/dagger/users-guide.html#binding-instances

  • What is the use-case of @Module Annotation?
    A) @Module is the Annotation used on the class for the Dagger to look inside it, to provide dependencies. We may be declaring methods inside the module class that are enclosed with @Provides annotation.

  • What is the use-case of @Provides Annotation?
    A) @Provides annotation is used on a method in Module class and can return / provide a Dependency object.

  • What is the use-case of @Component Annotation?
    A) @Component is used on Interface or abstract class. Dagger uses this interface to generate an implementation class with fully formed, dependency injected implementation, using the modules declared along with it. This generated class will be preceded by Dagger. For example if i create an interface named ProgramComponent with @Component annotation, Dagger will generate a Class named ‘DaggerProgramComponent’ implementing the ProgramComponent interface.

  • What is the use-case of @Scope Annotation?
    A) @Scope is an annotation used on Interface to create a new Custom Scope. A Scope declaration helps to keep single instance of a class as long as its scope exists. For example, in Android, we can use @ApplicationScope for the object to live as long as the Application is live or @ActivityScope for the object to be available till the activity is killed.

  • What is the use of Qualifier in Dagger?
    A) We are often in a situation where we will be needing multiple objects with different instance values. For example, we need declare Student(“Vamsi”) and Student(“Krishna”). In such case we can use a Qualifier to tell Dagger that we need multiple instances of same class. The default implementation of Qualifier is using @Named annotation, for eg., @Named(“student_vamsi”) and @Named(“student_krishna”)
    If we want to create a Custom Qualifier we would be using @Qualifier to declare a custom Qualifier interface.

  • What is the use-case of @Inject Annotation in Dagger?
    A) @Inject annotation is used to request dagger to provide the respective Object. We use @Inject on Constructor, Fields (mostly where constructor is not accessible like Activities, Fragments, etc.) and Methods.

RxJava Related Questions:

More additional info to get started with RxJava is available at:
Getting Started with RxJava2

  • What is an Observable in RXJava2?
    A) An Observable simply emits the data to those which subscribed to it. All the emission is done asynchronously to the subscribers. A simple Observable can be created as follows:

    // RxAndroid Tutorial - Adding Observable
    Observable<String> stringObservable = Observable.just("Hello Reactive Programming!");
  • What is an Observer in RXJava2?
    A) Observer consumes the data emitted by the Observable. To do this, Observer needs to subscribe to the Observable. Example shows how to create an Observable in RxJava2.

    // RxAndroid Tutorial - Adding observer
    Observer<String> stringObserver = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }
    
            @Override
            public void onNext(String s) {
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
            }
    
            @Override
            public void onError(Throwable e) {
            }
    
            @Override
            public void onComplete() {
            }
        };
  • How to Subscribe / Unsubscribe in RXJava?
    A) We can make an Observer to subscribe to Observable as follows:

    // RxAndroid tutorial - observer subscribing to observable
    stringObservable.subscribe(stringObserver);
  • What are the different types of Observables in RxJava?
    A)1) single
    2) Maybe
    3) Completable
    4) Observable
    5) Flowable

  • What is a Single in RxJava?
    A) A Single in RxJava is an Observable which emits only one item if completed or returns error.

  • What is Maybe in RxJava?
    A) A Maybe in RxJava is used when the Observable needs to emit a value or a no value or an error.

  • What is Completable in RxJava?
    A) A Completable in RxJava is an Observable which just completes the task and does not emit anything if completed. It returns an error if anything fails.
    It is similar to reactive concept of runnable.

  • What is Back Pressure in RxJava?
    A) Back Pressure is the state where your observable (publisher) is creating more events than your subscriber can handle.

  • What is Flowable in RxJava?
    A) A Flowable in RxJava is used when the Observable emits more data than the Observer can consume. In Other words, Flowable can handle back pressure where as an Observable cannot.

  • What is a Cold Observable?
    A) A Cold Observable is an Observable that does not emit items until a Subscriber subscribes. If we have more than one Subscriber, then the Cold Observable will emit each sequence of items to all Subscribers one by one.

  • What is a Hot Observable?
    A) A Hot observable is an Observer that will emit items

  • Hot Observables vs Cold Observables

  • Explain about reactive programming?