ANDROID INTERVIEW QUESTIONS

Thulasi Ram
9 min readFeb 11, 2020

Hi,

I am here to tell about my last interview experience for a senior Android developer profile. I have tired answering a few and the rest I have just given the question. These are some of the basic questions asked during the interview session which we should be aware of.

  • Why bytecode cannot be run in Android?
  • What is Application class?
  • Difference between Service & Intent Service.
  • What are Handlers?
  • What is the disadvantage in using AsyncTask?
  • What is a ThreadPool?
  • Difference between Activity & Service.
  • What is an intent?
  • What is a Sticky Intent?
  • What is a Pending Intent?
  • What are intent Filters?
  • What is View Group? How are they different from Views?
  • When do you use a FrameLayout?
  • What are Adapters?
  • What are the permission protection levels in Android?
  • What is an Application Not Responding (ANR) error, and how can you prevent them from occurring in an app?
  • What is a singleton class in Android?
  • How does RecyclerView differ from ListView?
  • What is a ‘bundle’ in android?
  • How does RecyclerView work?
  • What’s the difference between commit() and apply() in SharedPreferences?

Some of them have also been answered to the best capacity of my knowledge and few have been taken from other websites.

  1. Why bytecode cannot be run in Android?
  • Android uses DVM (Dalvik Virtual Machine ) rather using JVM(Java Virtual Machine).
  • The Dalvik Virtual Machine (DVM) is an android virtual machine optimized for mobile devices. It optimizes the virtual machine for memory, battery life and performance.
  • The Dex compiler converts the class files into the .dex file that run on the Dalvik VM. Multiple class files are converted into one dex file.

2. What is Application class?

  • Base class for maintaining global application state..
  • You can provide 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.
  • The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.

3. Difference between Service & Intent Service.

  • In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for “fire and forget” operations, taking care of background Thread creation and cleanup.
  • Service: A Service is an application component representing either an application’s desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
  • IntentService Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through startService (Intent) calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

4. What are Handlers?

  • Handlers are objects for managing threads. It receives messages and writes code on how to handle the message. They run outside of the activity’s lifecycle, so they need to be cleaned up properly or else you will have thread leaks.
  • Handlers allow communicating between the background thread and the main thread.
  • A Handler class is preferred when we need to perform a background task repeatedly after every x seconds/minutes.

5. What is the disadvantage in using AsyncTask?

  • An AsyncTask is not tied to the life cycle of the Activity that contains it. So, for example, if you start an AsyncTask inside an Activity and the user rotates the device, the Activity will be destroyed (and a new Activity instance will be created) but the AsyncTask will not die but instead goes on living until it completes.
  • Then, when the AsyncTask does complete, rather than updating the UI of the new Activity, it updates the former instance of the Activity (i.e., the one in which it was created but that is not displayed anymore!). This can lead to an Exception (of the type java.lang.IllegalArgumentException: View not attached to window manager if you use, for instance, findViewById to retrieve a view inside the Activity).
  • There’s also the potential for this to result in a memory leak since the AsyncTask maintains a reference to the Activity, which prevents the Activity from being garbage collected as long as the AsyncTask remains alive.
  • For these reasons, using AsyncTasks for long-running background tasks is generally a bad idea . Rather, for long-running background tasks, a different mechanism (such as a service) should be employed.

6. What is a ThreadPool?

  • ThreadPool consists of a task queue and a group of worker threads, which allows it to run multiple parallel instances of a task.

7. Difference between Activity & Service.

  • Activities are basically containers or windows to the user interface. Services is a component that is used to perform operations on the background. It does not have an UI.

8. What is an intent?

  • Intents are messages that can be used to pass information to the various components of android. For instance, launch an activity, starting a service, open a webview etc.
  • Two types of intents-
  • Implicit Intent: Implicit intent is when you call system default intent like send email, send SMS, dial number.
  • Explicit Intent: Explicit intent is when you call an application activity from another activity of the same application.

9. What is a Sticky Intent?

  • Sticky Intents allows communication between a function and a service.
  • sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter).
  • For example, if you take an intent for ACTION_BATTERY_CHANGED to get battery change events: When you call registerReceiver() for that action — even with a null BroadcastReceiver — you get the Intent that was last Broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

10. What is a Pending Intent?

  • A Pending Intent is a token you give to some app to perform an action on your apps’ behalf irrespective of whether your application process is alive or not.
  • Just think of use-cases for Pending Intents like (Broadcasting Intents, scheduling alarms).

11. What are intent Filters?

  • An intent filter specifies the type of intents that the component(activity/service) would respond to.

12. What is View Group? How are they different from Views?

  • View: View objects are the basic building blocks of User Interface(UI) elements in Android. View is a simple rectangle box which responds to the user’s actions. Examples are EditText, Button, CheckBox etc. View refers to the android.view.View class, which is the base class of all UI classes.
  • ViewGroup: ViewGroup is the invisible container. It holds View and ViewGroup. For example, LinearLayout is the ViewGroup that contains Button(View), and other Layouts also. ViewGroup is the base class for Layouts.

13. When do you use a FrameLayout?

  • If you add multiple Views to a FrameLayout then it’ll stack them one above the other, so FrameLayouts are also useful if you need overlapping Views.

14. What are Adapters?

  • An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.
  • An adapter responsible for converting each data entry into a View that can then be added to the AdapterView (ListView/RecyclerView).

15. What are the permission protection levels in Android?

  • Normal — A lower-risk permission that gives requesting applications access to isolated application-level features, with minimal risk to other applications, the system, or the user. The system automatically grants this type of permission to a requesting application at installation, without asking for the user’s explicit approval.
  • Dangerous — A higher-risk permission. Any dangerous permissions requested by an application may be displayed to the user and require confirmation before proceeding, or some other approach may be taken to avoid the user automatically allowing the use of such facilities.
  • Signature — A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user’s explicit approval.
  • SignatureOrSystem — A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificate as the application that declared the permission.

16. What is an Application Not Responding (ANR) error, and how can you prevent them from occurring in an app?

  • An ANR dialog appears when your UI has been unresponsive for more than 5 seconds, usually because you’ve blocked the main thread. To avoid encountering ANR errors, you should move as much work off the main thread as possible.

17. What is a singleton class in Android?

  • A singleton class is a class which can create only an object that can be shared all other classes.

private static volatile ApiService instance;
protected ApiService(Context context) {
super(context);
}
public static ApiService getInstance(Context context) {
if (instance == null) {
synchronized (ApiService.class) {
if (instance == null) instance = new ApiService(context);
}
}
return instance;
}

18. How does RecyclerView differ from ListView?

  • ViewHolder Pattern: Recyclerview implements the ViewHolders pattern whereas it is not mandatory in a ListView. A RecyclerView recycles and reuses cells when scrolling.
  • What is a ViewHolder Pattern? — A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly. In ListView, the code might call findViewById() frequently during the scrolling of ListView, which can slow down performance. Even when the Adapter returns an inflated view for recycling, you still need to look up the elements and update them. A way around repeated use of findViewById() is to use the “view holder” design pattern.
  • LayoutManager: In a ListView, the only type of view available is the vertical ListView. A RecyclerView decouples list from its container so we can put list items easily at run time in the different containers (linearLayout, gridLayout) by setting LayoutManager.
  • Item Animator: ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it.

19. What is a ‘bundle’ in android?

  • Android Bundle is used to pass data between activities. The values that are to be passed are mapped to String keys which are later used in the next activity to retrieve the values.

20. How does RecyclerView work?

  • RecyclerView is designed to display long lists (or grids) of items. Say you want to display 100 rows of something. A simple approach would be to just create 100 views, one for each row and lay all of them out. But that would be wasteful because at any point of time, only 10 or so items could fit on screen and the remaining items would be off screen. So RecyclerView instead creates only the 10 or so views that are on screen. This way you get 10x better speed and memory usage.
  • What happens when you start scrolling and need to start showing next views?
  • Again a simple approach would be to create a new view for each new row that you need to show. But this way by the time you reach the end of the list you will have created 100 views and your memory usage would be the same as in the first approach. And creating views takes time, so your scrolling most probably wouldn’t be smooth. This is why RecyclerView takes advantage of the fact that as you scroll, new rows come on screen also old rows disappear off screen. Instead of creating new view for each new row, an old view is recycled and reused by binding new data to it.
  • This happens inside the onBindViewHolder() method. Initially you will get new unused view holders and you have to fill them with data you want to display. But as you scroll you will start getting view holders that were used for rows that went off screen and you have to replace old data that they held with new data.

21. What’s the difference between commit() and apply() in SharedPreferences?

  • commit() writes the data synchronously and returns a boolean value of success or failure depending on the result immediately.
  • apply() is asynchronous and it won’t return any boolean response. Also if there is an apply() outstanding and we perform another commit(). The commit() will be blocked until the apply() is not completed.

--

--