Thursday, May 4, 2017

Android Interview questions and answers

1.    What is an ANR Exception in Android?
 ANR is short for Application Not Responding. Android systems show this dialog if the application is performing too much of task on the main thread or UI thread and been unresponsive for a long period of time. Create user thread or worker thread to avoid ANR exception.  By making use of AsynTask, one can avoid this exception.
2.     What is the difference between an implicit intent and explicit intent?
 There are two types of Intent implicit and explicit intent
1) Implicit Intent:-
Implicit intent when you call system default intent like send email, send SMS, dial number etc
e.g.:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
sendIntent.setType("text/plain")
startactivity(sendIntent);

2) Explicit Intent:-
Explicit intent when you call you're on application activity from one activity to another
e.g. first activity to second activity

Intent intent = new Intent(MainActivity.this, SecondAct.class);
startactivity(intent);

3.    What is Android?
It is an open-sourced operating system that is used primarily on mobile devices, such as cell phones and tablets. It is a Linux kernel-based system that’s been equipped with rich components that allows developers to create and run apps that can perform both basic and advanced functions.
4.    What is AAPT?
AAPT is short for Android Asset Packaging Tool. This tool provides developers with the ability to deal with zip-compatible archives, which includes creating, extracting as well as viewing its contents.
5.    Differentiate Activities from Services.
Activities can be closed, or terminated anytime the user wishes. On the other hand, services are designed to run behind the scenes, and can act independently. Most services run continuously, regardless of whether there are certain or no activities being executed.
6.    What are the core building blocks of android?
The core building blocks of android are:
Activity, View, Intent, Service, Content Provider, Fragment etc…
7.    Which language does Android support to develop an application?
Android applications are written by using the java (Android SDK) and C/C++ (Android NDK).
8.    What is Adapter in Android?
An adapter is used to create a child view to present the parent view items.
9.    What is adb?
Adb is short for Android Debug Bridge. It allows developers the power to execute remote shell commands. Its basic function is to allow and control communication towards and from the emulator port
10.  When is the onStop() method invoked?
A call to onStop method happens when an activity is no longer visible to the user, either because another activity has taken over or if in front of that activity
11.  How can the ANR be prevented?
One technique that prevents the Android system from concluding a code that has been responsive for a long period of time is to create a child thread. Within the child thread, most of the actual workings of the codes can be placed, so that the main thread runs with minimal periods of unresponsive times
12.  Why cannot you run standard Java bytecode on Android?
A. Android uses Dalvik Virtual Machine (DVM) which requires a special bytecode. We need to convert Java class files into Dalvik Executable files using an Android tool called "dx". In normal circumstances, developers will not be using this tool directly and build tools will care for the generation of DVM compatible files.
13.  How does an application run in isolation from other applications?
A. Each application gets its own Linux user ID and virtual machine which means that the application code runs in isolation from other applications.
14.   Which dialog boxes can you use in you Android application?
A. AlertDialog: an alert dialog box and supports 0 to 3 buttons and a list of selectable elements. ProgressDialog: an extension of AlertDialog and you may add buttons to it. It shows a progress wheel or a progress bar. DatePickerDialog: used for selecting a date by the user. TimePickerDialog: used for selecting time by the user.
15.  What is a ContentProvider and what is it typically used for?
A ContentProvider manages access to a structured set of data. It encapsulates the data and provide mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process.
More information about content providers can be found here in the Android Developer’s Guide


16.  What is DDMS? Describe some of its capabilities.
DDMS is the Dalvik Debug Monitor Server that ships with Android. It provides a wide array of debugging features including:
·         port-forwarding services
·         screen capture
·         thread and heap information
·         network traffic tracking
·         incoming call and SMS spoofing
·         simulating network state, speed, and latency
·         location data spoofing

17.  What is the difference between a fragment and an activity? Explain the relationship between the two.

An activity is typically a single, focused operation that a user can perform (such as dial a number, take a picture, send an email, view a map, etc.). Yet at the same time, there is nothing that precludes a developer from creating an activity that is arbitrarily complex.
Activity implementations can optionally make use of the Fragment class for purposes such as producing more modular code, building more sophisticated user interfaces for larger screens, helping scale applications between small and large screens, and so on. Multiple fragments can be combined within a single activity and, conversely, the same fragment can often be reused across multiple activities. This structure is largely intended to foster code reuse and facilitate economies of scale.
A fragment is essentially a modular section of an activity, with its own lifecycle and input events, and which can be added or removed at will. It is important to remember, though, that a fragment’s lifecycle is directly affected by its host activity’s lifecycle; i.e., when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all of its fragments.
18.  What is the difference between Service and IntentService? How is each used?
Service is the base class for Android services that can be extended to create any service. A class that directly extends Service runs on the main thread so it will block the UI (if there is one) and should therefore either be used only for short tasks or should make use of other threads for longer tasks.
IntentService is a subclass of Service that handles 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. Writing an IntentService can be quite simple; just extend the IntentService class and override the onHandleIntent(Intent intent) method where you can manage all incoming requests.
19.  What do ADT stands for?
ADT stands for Android development tool,This is useful to develop the applications and test the applications.

20.  What is a service in android?
The Service is like as an activity to do background functionalities without UI interaction.

21.  What is shared preferences in android?
Shared preferences are the simplest mechanism to store the data in XML documents.

22.  How is the use of web view in Android?
WebView is UI component that can display either remote web-pages or static HTML

23.  What are the different Storage Methods in android?

Android provides many options for storage of persistent data. It provides the solution according to your need. The storages which have been provided in Android are as follows:-

Shared Preferences: Store private primitive data in key-value pairs

Internal Storage: Store private data on the device memory.

External Storage: Store public data on the shared external storage.

SQLite Databases: Store structured data in a private database.

Network Connection: Store data on the web with your own network server

24.   How do you find any view element into your program?
Findviewbyid : Finds a view that was identified by the id attribute from the XML processed inActivity.OnCreate(Bundle).

25.  Singleton design pattern in java
Singleton patterns comes under creational design patterns. The Singleton's purpose is to control object creation, limiting the number of objects to one only.  Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.
Singleton ensures that a class has only one instance, and provide a global point of access to it.



Example:
public class Singleton {
  // Private constructor prevents instantiation from other classes
  private Singleton() {}

  /**
   * SingletonHolder is loaded on the first execution of Singleton.getInstance()
   * or the first access to SingletonHolder.INSTANCE, not before.
   */
  private static class SingletonHolder {
    private static final Singleton INSTANCE = new Singleton();
  }

  public static Singleton getInstance() {
    return SingletonHolder.INSTANCE;
  }
}

26.  How to check service is running in background or not?
Use below code to check whether service is running or not
private boolean isMyServiceRunning(Class serviceClass) {
  ActivityManager actManager = (ActivityManager)
                             getSystemService(Context.ACTIVITY_SERVICE);
  for (RunningServiceInfo service : actManager
    .getRunningServices(Integer.MAX_VALUE)) {
   if (serviceClass.getName().equals(service.service.getClassName())) {
    return true;
   }
  }
  return false;
 }

Other Android Qns

  • How does the Android notification system work?
  • How can two distinct Android apps interact? (several answers)
  • Describe Activities. 
  • What are the four states of the Activity Lifecycle? 
  • What are the seven callback methods of an Activity used to perform operations when the Activity transitions between states? 
  • What is the difference between a fragment and an activity? Explain the relationship between the two.
  • What is the difference between Serializable and Parcelable? Which is the best approach in Android?
  • What are "launch modes"?
  • What are Intents? 
  • What is an Implicit Intent? 
  • What is an Explicit Intent? 
  • Describe three common use cases for using an Intent.
  • What is a Service? 
  • What is a ContentProvider and what is it typically used for? 
  • What is a Fragment?   
  • What is AndroidManifest.xml used for? Give examples of what kind of data you would add to it. 
  • Describe how broadcasts and intents work to be able to pass messages around your app.
  • What is the Dalvik Virtual Machine?
  • What are different ways to store data in your Android app? 
  • Android appplication components 
  • What is the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?
  • What is the difference between Service and IntentService? How is each used?
  • What is a Sticky Intent?
  • What is AIDL? 
  • What is dependency injection?
  • What are the different protection levels in permission? 

Android Design and XML

  • Explain the differences and similarities of Relative Layout and Linear Layout.
  • Explain the differences and similarities of List Views and Grid Views.
  • Describe how to implement XML namespaces.
  • Explain how to present different styles/drawables for a button depending on the state of the button (pressed, selected, etc.) using XML (no Java) 
  • for layout_width and layout_height, what's the difference between match_parent and wrap_content?
  • How do you implement Google's new Material Design in an Android application? 
  • Difference between View.GONE and View.INVISIBLE?

Android Networking

  • Have you use an HTTP Library, which, why, did you like it?
  • Describe how REST APIs work.
  • What are some typical methods of HTTP request/responses? 

Databases

  • Why does Android use SQLite?
  • What libraries have you used for interacting with databases and why did you choose them?
  • What are contract classes? 
  • How do you use the BaseColumns interface to describe your data schema?