Android Interview Questions and Answers

Android Interview Questions and Answers

Last updated on 25th Sep 2020, Blog, Interview Question

About author

Kernel (Sr Technical Project Manager )

He is a Proficient Technical Expert for Respective Industry & Serving 11+ Years. Also, Dedicated to Imparts the Informative Knowledge to Freshers. He Share's this Blogs for us.

(5.0) | 15638 Ratings 961

In this technological era, there is a huge demand for Android Application developers and it is one of the highly paid jobs too.

Hence many tech-savvy people are trying to make their way into this career. There are several tricky interview questions that are asked in many MNC’s and some small scale companies. And this blog includes a set of such Android interview questions and answers that will be helpful to millions of job seekers.This blog includes a set of most commonly asked android interview questions and answers covering almost all the important concepts in simple terms in order to enable any job seeker to clear the interview success

1.What is Android?

Ans:

 Android is an open-source operating system and is mainly popular for Smartphones and Tablets.

This operating system is Linux Kernel-based. Using the Android operating system, the developer develops the functions or programs which can perform basic as well as the advanced type of operations on the Smartphone.

2. What is the Android SDK?

Ans:

To develop a mobile application, Android developers require some tools and this requirement is satisfied by “Android SDK” which is a set of tools that are used for developing or writing apps.

It has a Graphical User Interface that emulates the Android environment. This emulator acts like an actual mobile device on which the developers write their code and then debug/test the same code to check if anything is wrong.

3. What are the different versions of Android OS that you remember?

Ans:

 Given below are the various versions of Android.

VersionName
Android 8.0Oreo
Android 7.0 – 7.1.2Nougat
Android 6 – 6.0.1Marshmallow
Android 5 – 5.1.1Lollipop
Android 4.4 – 4.4.4KitKat
Android 4.1 – 4.3Jelly Bean
Android 4.0-4.0.4Ice Cream Sandwich

4. What is the difference between Mobile Application Testing and Mobile Testing?

Ans:

Mobile app testing is the testing of applications on a device which mainly focuses on functions and features of the application.

And Mobile Testing is the testing of the actual mobile device and focuses on mobile features like Call, SMS, Contacts, Media Player, inbuilt browsers, etc.

5. Name the languages supported for Android development.

Ans:

 Java is the widely used language for Android development. It also supports C/C++ and when used with Android SDK, it improves the performance speed too.

6.What are the advantages of the Android Operating System?

Ans:

It is open-source and platform-independent. It supports various technologies like Bluetooth, Wi-Fi, etc

7. Explain Android Architecture briefly.

Ans:

 Android architecture is in the form of software stack components.

The below diagram describes the different layers in the Android architecture.

  • Linux Kernel: Linux Kernel is placed at the bottom of the software stack and is the foundation of the Android architecture. Using the Linux kernel, Android provides a connection between the other layers of the software. It helps to develop drivers like the keypad, display, audio for device manufacture, etc.
  • Hardware Abstraction Layer (HAL): HAL provides an interface between device drivers and API framework. It consists of library modules that are specific to the hardware component.
  • Android Runtime: Linux kernel provides a multi-tasking execution environment so that multiple processes can execute each process on its own instance of Android Runtime (ART). Android has core runtime libraries like Dalvik VM specific libraries, Java Interoperability Libraries, Android Libraries, and C/C++ libraries.
  • Application Framework (Java API Framework): The entire android functionalities are available through the API. It consists of multiple services like Activity Manager, Resource Manager, Notification Manager, etc., which form the environment in which the android application runs.
  • Applications: The Android application is a top layer and all types of in-built applications such as SMS, Browsers, Contact, etc are included in this top layer. It also includes third-party applications that are installed by the user such as Games, etc.

8. Define and explain the Android Framework.

Ans:

Android framework is a set of API’s using which the Android developers write code for the mobile apps. It contains the methods and classes to write the programming code.

Android framework includes a different set of tools to create image pane, text field, buttons, etc. It also includes “Activities” with which the user interacts and “Services”, which are the programs that run in the background. It is a package of different components like Intents, Broadcast Receivers, Content Providers, etc.

9. Which components are necessary for a New Android project?

Ans:

Whenever a new Android project is created, the below components are required:

  • manifest: It contains an XML file.
  • build/: It contains build output.
  • src/: It contains the code and resource files.
  • res/: It contains bitmap images, UI Strings and XML Layout i.e. all non-code resources.
  • assets/: It contains a file that should be compiled into a .apk file.

The below image shows the Project View once an Android project is created:

Android project

10. Provide the important core components of Android.

Ans:

The core components of Android operating systems are:

  • Activity
  • Intents
  • Services
  • Content Provider
  • Fragment
Subscribe For Free Demo

Error: Contact form not found.

11.Explain briefly – what is meant by Activities?

Ans:

 Activities are the part of the mobile app which the user can see and interact with.

For Example, if you open an SMS app which has multiple activities like create new SMS, add a contact from the address book, write the content in the SMS body, send SMS to the selected contact, etc.

Activity keeps a track of the following:

  • Keeps track of what a user is currently looking for in an app.
  • Keeps a track of previously used processes, so that the user can switch between ongoing process and previous process.
  • It helps to kill the processes so that the user can return to their previous state

An activity is implemented as a subclass of Activity class as shown below:

  • Public class MyActivity extends Activity
  • {
  • }

12.What is meant by Services?

Ans:

Service is an Android component that runs in the background and acts independently. It does not provide any user interface.

Though the services are running behind the scene, a user can continue their work on different apps. Most of the time, the users are not aware of the services which are running in the background. These services allow the system to kill the process without interrupting the user’s ongoing work.

A service is implemented as a subclass of Service class:

  • Public class MainService extends Service
  • {
  • }

13. Explain Activity Lifecycle briefly.

Ans:

When a user interacts with the app and moves here and there, out of the app, returns to the app, etc. During all this process “Activity” instances also move in the different stages in their lifecycle.

There are seven different states  like – onCreate(), onStart(), onRestart(), onResume(), onPause(), onStop(), and onDestroy(). These are termed as a ‘callback’. Android system invokes these callbacks to know that the state has been changed.

The below-given diagram describes the Activity Lifecycle:

Activity-cycle

When a user is working on an app, then there are many activities involved in it like Open, Close, Save, Delete, Send, etc.

Based on the user action these activities are partially disconnected from the UI but these activities always reside in the memory so that when the user calls back the same activity, the user will be in the same state where he has left off.

14.What is an Intent?

Ans:

Android has an Intent class when the user has to navigate from one activity to another. Intent displays notifications from the device to the user and then the user can respond to the notification if required.

Given below are the two types:

  • Implicit Intents
  • Explicit Intents

15.Explain Implicit and Explicit Intents.

Ans:

Implicit Intent calls the system components while Explicit Intents invoke the Activity class.

16. What is the importance of setting up permission in app development?

Ans:

Once the permissions are set for the app development, then the data and code are restricted to the authorized users only.

If the code is kept without any restriction or if it is accessible to anyone then there are chances of compromise of code which results in defect leakage.

17. What is .apk extension in Android?

Ans:

It is a default file format that is used by the Android Operating System. Application Package Kit (APK) is used for the installation of mobile apps. The .apk contains resource file, certificate, manifest file, and other code.

APK files are archive files in the zip format with .apk extension.

18.What is the database used for the Android platform?

Ans:

SQLite is the database that is used for the Android platform. It is an open-source, serverless database.

19.What is ANR in Android?

Ans:

ANR stands for Application Not Responding. It is a notification or pop-up displayed by the Android platform whenever the application is performing too many functions at a time and if it is suddenly not responding for a long time to the user action.

20. Which are the dialog boxes supported by the Android platform?

Ans:

 Android supports four types of dialog boxes:

  • AlertDialog: It has a maximum of 3 buttons and sometimes AlertDialog includes checkboxes and Radio buttons to select the element.
  • ProgressDialog: It displays the progress bar or wheels.
  • TimePickerDialog: Using this dialog box, a user selects the Time.
  • DatePickerDialog: Using this dialog box, a user selects the Date

21. What is ADB?

Ans:

Android Debug Bridge (ADB) is a command-line tool that performs shell commands.

ADB is used for direct communication between the emulator ports. It gives direct control of the communication between the emulator instances to the developer.

22.What is ActivityCreator?

Ans:

ActivityCreator is a batch file and shell script which was used to create a new Android project. It is now replaced by the “Create New Project” in Android SDK.

23.What is Orientation?

Ans:

Orientation is the key feature in Smartphones nowadays. It has the ability to rotate the screen between Horizontal or Vertical mode.

Android supports two types of screen Orientations as mentioned below:

  • Portrait: When your device is vertically aligned.
  • Landscape: When your device is horizontally aligned.

setOrientation() is a method using which you can set a screen alignments. HORIZONTAL and VERTICAL are two values that can be set in the setOrientation() method. Whenever there is a change in the display orientation i.e. from Horizontal to Vertical or vice versa then onCreate() method of the Activity gets fired.

Basically, when the orientation of the Android mobile device gets changed then the current activity gets destroyed and then the same activity is recreated in the new display orientation. Android developers define the orientation in the AndroidManifest.xml file.

24.What is AIDL?

Ans:

In the Android platform, there are remote methods that facilitate the use of methods from one program to another. To create and implement the remote methods the first step is to define the communication interface in AIDL.

AIDL stands for Android Interface Definition Language. It facilitates communication between the client and the service. It also communicates the information through inter-process communication.

For communication between processes, the data is broken down into chunks which are easily understandable by the Android platform.

25.What are the data types supported by AIDL?

Ans:

Data Types supported by AIDL are as follows:

  • String
  • List
  • Map
  • charSequence
  • Java data types such as INT, Long, Char, Boolean, etc

26.Explain the AndroidManifest.xml file and why do you need this?

Ans:

Every application must have an AndroidManifest.xml file in the root directory. It contains information about your app and provides the same to the Android system.

The information includes the package name, Android components such as Activity, Services, Broadcast Receivers, Content Providers, etc. Every Android system must have this information before running any app code.

AndroidManifest.xml file performs the following tasks:

  • It provides a name to the Java package and this name is a unique identifier for the application.
  • It describes the various components of the application which include Activity, Services, Content Providers, etc. Also, it defines the classes which implement these components.
  • It is responsible to protect the application and it declares the permission for accessing the protected part of the app.
  • It also declares the Android API which is going to be used by the application.
  • It contains the library file details which are used and linked to the application.

27.What all devices have you worked on?

Ans:

There are many mobile devices available in the market with different operating systems.

Specifically, I have worked on Android, Windows, Symbian, iPhone, etc

28.Which tools are used for debugging on the Android platform?

Ans:

To understand the cause of the failure or cause of any issue, debugging is important. On the Android platform Android Monitor.bat utility is used while on the iOS platform, iPhone Configuration utility is used for debugging purposes.

There are different tools for debugging which include: Android DDMS, Android Debug Bridge, iOS simulator, Debugging from Eclipse with ADT, Remote debugging on Android with Chrome, etc.

29.Which scenario can test only on real devices but not on an emulator?

Ans:

Emulators are used for performing similar kinds of testing which is performed on the real devices. Basically, emulators are used as a replacement for real devices as sometimes real devices are not available for testing, the use of real mobile devices for testing purposes is costlier at times.

But there are few scenarios that cannot be tested using emulator, these can be tested only using real devices. These scenarios are interrupted scenarios i.e. message, phone call interruption while using the app, low battery, Bluetooth, memory card mount and unmount, etc.

30.Name the mobile automation tools that are available in the market.

Ans:

There are quite a few mobile automation testing tools that are available in the market but these are used only if the project requires it and if the application supports the automation.

These tools are paid as well as free tools, hence analysis needs to be done within the project team and then the appropriate mobile automation tool needs to be selected. Silk Mobile, SeeTest, Ranorex are the paid mobile automation tool while Appium, KIF, Robotium, Calabash are few free tools.

Course Curriculum

Best Practical Oriented Android Training By Certified Experts

  • Instructor-led Sessions
  • Real-life Case Studies
  • Assignments
Explore Curriculum

31.How do you troubleshoot the android application which is crashing frequently?

Ans:

Given below are the few steps that we need to follow while troubleshooting the crashing issue:

Free up memory space: There is only limited space available on mobile devices for mobile apps. To avoid crashing issues or memory-related issues, you need to first check the memory space.

Clear app data usage: You can clear the app data using the Application Manager under “Settings”. This will clear the cache memory and allow some free space to install another app or it will boost up your current app.

Memory Management: Some apps run perfectly on one type of mobile device but the same app may not work on another type of device as for such devices the processing power, memory management, and CPU speed is different. For any app to run properly on any type of mobile device, you should manage the memory on the device.

Compatibility issue: It is always not possible to test mobile apps on all mobile devices, browsers, operating systems, etc. So you need to test your mobile app on as many mobile devices as you can in order to avoid any compatibility issue.

32.How do you find memory leaks in the mobile app on the Android platform?

Ans:

Android Studio is using Android Device Manager (ADM), this ADM is used to detect the memory leaks in the Android platform.

When you open ADM in the Android Studio then on the left-hand side of the ADM, you will find your device or emulator in which a heap sign will be displayed. When you are running any mobile app then you will see the heap size, memory analysis and other statistics displayed on it.

33.What is DDMS?

Ans:

Android Studio has debugging tools known as DDMS i.e. Dalvik Debug Monitor Server.

It has wide debugging features which include:

  • Port forwarding services.
  • Screen capture on the device.
  • Thread and Heap information.
  • Incoming call and SMS spoofing.
  • Logcat
  • Radio state information.
  • Location data spoofing.

DDMS is integrated with the Android studio. To launch the DDMS, you need to open the Android Device Monitor (ADM) first and then click on the DDMS menu button. Once DDMS is launched, then on the left-hand side the list of connected devices is displayed along with the processes which are running on each device.

With the help of DDMS, you can debug both on real devices and emulators.

34.What are the different data storage options available on the Android platform?

Ans:

Android platform provides a wide range of data storage options. These options must be used based on the need such as data is secure and used with permission only or can be accessed publicly.

Below is the list of data storage options on the Android platform:

  • SharedPreference: It stores data in XML files. It is the simplest way to store private data in the key-value pair.
  • SQLite: It stores structured data in the private database.
  • Internal Storage: It stores data in the device file system and any other app cannot read this data.
  • External Storage: Data is stored in the file system but it is accessible to all apps in the device

35.Explain Sensors in Android.

Ans:

Android-enabled devices have built-in sensors that measure orientation, motion, and other conditions.

These sensors provide data with high accuracy, which will help to monitor the positioning and movement of the device. Some of the sensors are hardware-based and few are software-based.

There are three categories of sensors as mentioned below:

  • Motion Sensors: These sensors measure the rotational & acceleration forces and it includes gravity sensors, rotational vector sensors, accelerometers, etc.
  • Environmental Sensors: It measures air temperature, pressure, humidity, etc.
  • Position Sensors: It measures the physical position of the device and includes orientation sensors and magnetometers.

There are four types of Java Classes as shown below:

  • Sensor Manager
  • Sensor
  • SensorEvent
  • SensorEventListener

36.Explain different launch modes in Android

Ans:

Below are the different launch modes in Android

  • Standard: This launch mode generates a new instance of the activity in the task from which it originated. It is possible to create multiple instances of the same activity, which can be added to the same or different tasks.
  • SingleTop: This launch mode is similar to the Standard launch mode, except if there exists a previous instance of the activity on the top of the stack, then a new instance will not be created, but the intent will be sent to the existing instance of the activity.
  • SingleTask: This launch mode will always create a new task and push a new instance to the task as the root one.
  • SingleInstance: This launch mode is the same as the SingleTask launch mode but the system doesn’t launch any new activities in the same task. In a scenario where the new activity is launched, it is launched in a separate task.

37.State the components which are necessary for a New Android Project

Ans:

When a new Android project is created, the following components are required:

  • Manifest: Contains the xml file
  • Build/: Contains build output
  • Res/: Contains non-code resources such as bitmap images, UI strings and more
  • Src/: Contains the code and resources
  • Assets/: Contains a file which can be converted into an .apk file

38.What is the importance of setting up permission in Android application development?

Ans:

If the code is accessible to anyone and without restrictions, there may be a scenario where the code is compromised, resulting in defect leakage. Once the permissions are set, the code becomes available to authorized users only.

39.Explain the term ANR in Android

Ans:

The term ANR is short for Application Not Responding. It is displayed as a notification by the Android OS every time the application stops responding to the user action for a considerable amount of time.

40.Name the different data storage options available on the Android platform.

Ans:

Android platform provides a variety of data storage options which can be used depending upon the need of the user. The storage options are:

  • SharedPreference: This option stores data in XML files.
  • SQLite: This stores structured data in the private database
  • Internal Storage: This stores data in the device file system where it cannot be read by other applications
  • External Storage: This stores data in the file system but it can be accessed to all apps in the device

41.Define the architecture of Android?

Ans:

The Android architecture consists of four components. These are:

  • Linux Kernel.
  • Libraries.
  • Android applications.
  • Android Framework.
android-architecture

42.What is the full form of AAPT? Explain AAPT?

Ans:

The full form of AAPT is Android Asset Packaging Tool. This tool is used by developers that offer them the ability to deal with the zip-compatible archives. Hence the process of creation, extraction, and viewing of the contents easier with this tool in application.

43.What is an “Emulator” in Android?

Ans:

An “Emulator” in android helps the developers to play around an interface that acts as a real mobile device. This way, it becomes easier for the developers to write and test different codes for the application. The process of debugging also becomes possible through emulators. Hence, emulators provide a safe platform for testing codes in their early phases as well as in the later stage when the bugs need to be dealt with.

44.Is there any difference between activities and services?

Ans:

Yes, there are a lot of differences between activities and services. These differences are stated as under:

ActivitiesServices
These are closedThese are open
These can be terminated at any timeThese cannot be terminated at any time
They are designed to run before the scenesThese are designed to run behind the scenes
They are dependentThey act independently
They are not continuousThey are not continuous

45.What is the use of XML based layouts?

Ans:

The XML based layouts help inconsistent and standard format for setting up of GUI definition format. The layout details are placed in XML files, and the other items are put in source files.

46.Define containers in the Android system?

Ans:

The containers in the Android system helps in holding objects and widgets together so that the specific items and arrangements can be fulfilled. These containers include labels, buttons, fields, etc.

47.Do we need Android in the mobile market?

Ans:

Yes, we need Android in the mobile market because it helps the users of smartphones to download applications that can provide a variety of services. The developers make various applications year after year. The Android system gives them a platform from which they can display these applications to the smartphone users and find potential customers out of them. Android system offers paid and free applications for the users that could secure their data, act as a platform for information, and provide a variety of services as per the needs of the customers.

48.Is there any disadvantage to the Android System? List them all.

Ans:

Yes, there are several disadvantages to the Android system. These are listed below.

  • There is no policy on how the application can be adapted with the operating system versions and upgrades on smartphones.
  • Not all applications can run on every kind of operating system of smartphones. Thus the download could cause loss of data, time, and download facility.
  • Some of the applications in the Android system may not fit the screen of all kinds of smartphones.
  • The Android system offers such applications as well, which tends to collect user data. This may cause leaking of vital personal information data from smartphone users.
  • Android systems could offer download applications which may not be backed by credible sources and could cause harm to the mobile device.

49.What is the full form of ADB? Define ADB?

Ans:

The full form of ADB is Android Debug Bridge. ADB helps the developers in creating remote shell commands. The main function of ADB is to allow and control the communication process towards the emulator port and later getting a response from it.

50.What are the states in an activity?

Ans:

There are four states in an activity. These include the following.

  • Active state: In this state, the activity is in the foreground.
  • Paused state: In this state, the activity is in the background and visible.
  • Stopped state: in this state, the activity is in the background but no visible or even hidden or obscure other activities.
  • Destroyed state: In this state, the activity is completely terminated or killed or removed.
Course Curriculum

Get Hand-on Experience from Android Training Course By IT Experts

Weekday / Weekend BatchesSee Batch Details

51.What is the full form of ANR? Define ANR?

Ans:

The full form of ANR is Application Not Responding. ANR is a dialogue that appears on the screen of the application user on its electronic device. It seems when the application is unresponsive for a more extended period.

52.What are the permissions in the application development process?

Ans:

The permissions in the application development process include restrictions that are imposed to protect the data and code. These permissions are applied to protect the application from getting compromised, attacked by viruses, steal away users’ information, and to cause bug defects.

53.What are the intent filters?

Ans:

The intent filters are the vital components of the Android system because it helps in responding, filtering, and receiving the right information inputs.

54.Are there any critical loops while monitoring an activity?

Ans:

Yes, three key loops monitoring an activity. These include the following.

  • Loop 1: Entire Lifetime: In this key loop, the activity happens between the onCreate and onDestroy.
  • Loop 2: Visible Lifetime: In this key loop, the activity happens between onStart and onStop.
  • Loop 3: Foreground Lifetime: In this key loop, the activity happens between onResume and onPause.

55.What are the possible states in which a process is based?

Ans:

The possible states in which a process is based include the following.

  • State 1: Foreground activity
  • State 2: Visible activity
  • State 3: Background activity
  • State 4: Empty activity

56.Can we prevent ANR in the Android system?

Ans:

Yes, we can prevent ANR in the Android system. ANR hinders the Android system from concluding a code that has been responsive for a long time. The prevention can be made by creating a child thread wherein most of the actual workings of the codes can be applied, and the minimum period unresponsive times can be revealed.

57.What is Dalvik? How is it useful to the Android System?

Ans:

Dalvik is a virtual machine that helps the Android system run its applications. It helps the smartphones and other electronic devices to download the applications from the virtual machines which have every application stored in its memory. The new applications, their removal, and updates are all made in the Dalvik platform.

58.What are the Default Resources? How are they useful?

Ans:

The Default resources include the default strings and files. Their absence will result in creating errors on the screen and could also hinder the running of the downloaded application. They are useful as they are placed as subdirectories under the project directory, which supports the running of the downloaded application.

59.Define AIDL?

Ans:

AIDL stands for Android Interface Definition Language. AIDL is used for handling the interface requirements that exist between the client and the service provider. It acts as a platform to communicate at the same level through the interprocess communication or IPC method. The steps followed in AIDL includes breaking down the objectives into primitives so that the Android system can understand them well. AIDL also helps in limiting the applications from fetching information from other devices.

60.What is a Fragment in the Android system?

Ans:

A Fragment in the Android system is a part of the activity, which is modular in a sense. They can move around and could even combine with other fragments to form a single activity. Fragments are reusable.

61.Explain the dialog boxes supported on Android.

Ans:

Android supports four dialog boxes

  • AlertDialog: The most suggested dialog box, the AlertDialog supports 0-3 buttons, along with a list of selectable items, such as radio buttons and checkboxes.
  • DatePickerDialog: Used for the selection of date by the user
  • TimePickerDIalog: Used for the selection of time by the user
  • ProgressDialog: Used to display a progress bar and is an extension of the AlertDIalog. It also supports the addition of buttons.

62.What is Context?

Ans:

The Context in Android, as its name suggests, in the context of the current state of your application or object. The context comes with services such as giving access to databases and preferences, resolving resources and much more. There are two types of context:

  • Activity Context: This context is attached to the lifecycle of an activity. It should be used when you are passing the context in the scope of an activity or you need the context whose lifecycle is attached to the current context.
  • Application Context: This context is attached to the lifecycle of an application. The application context can be used where you need a context whose lifecycle is separate from the current context or when you are passing a context beyond the scope of activity.

63.How do you find memory leaks in an application on the Android platform?

Ans:

To find memory leaks in an application on Android, the Android Device Manager (ADM) is used by the Android Studio. When you open ADM in Android Studio, you can see parameters such as heap size and memory analysis along with many others while you run an application.

64.If an application is crashing frequently, how will you troubleshoot it?

Ans:

If an Android application is crashing frequently, you can do the following:

  • Free Memory: As there is a limited amount of space on mobile devices, you can try by freeing up memory space for the application to function properly.
  • Compatibility Check: It may not be a hardware problem, but more of a software issue. It is not always possible to test an application for all devices and OS systems. There might be a chance that the application is not compatible with your OS. Check the compatibility on the application’s Google Play Store page.
  • Memory Management: Some applications run perfectly on one mobile device but may crash on other devices. This is where processing power, memory management and CPU speed comes into play. Check the application memory requirements if the application is constantly crashing.
  • App Data Usage: If an application is crashing frequently, you can delete the application’s data, which will clear its cache memory and allow some free space on your device and might boost the app’s performance.

65.There are four Java classes related to the use of sensors on the Android platform. List them and explain the purpose of each.

Ans:

The four Java classes related to the use of sensors on the Android platform areL

  • Sensor: Provides methods to identify which capabilities are available for a specific sensor.
  • SensorManager: Provides methods for registering sensor event listeners and calibrating sensors.
  • SensorEvent: Provides raw sensor data, including information regarding accuracy.
  • SensorEventListener: Interface that defines callback methods that will receive sensor event notifications.

66.What is a ContentProvider and what is it typically used for?

Ans:

A ContentProvider manages access to a structured set of data. It encapsulates the data and provides mechanisms for defining data security. ContentProvider is the standard interface that connects data in one process with code running in another process.

67.Under what condition could the sample below crash your application?How would you modify the code to avoid this potential problem?Explain your answer.

  •  Intent sendIntent = new Intent();
  •     sendIntent.setAction(Intent.ACTION_SEND);
  •     sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);
  •     sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // “text/plain” MIME type
  •     startActivity(sendIntent);

Ans:

An implicit intent specifies an action that can invoke any app on the device able to perform the action. Using an implicit intent is useful when your app cannot perform the action, but other apps probably can. If there is more than one application registered that can handle this request, the user will be prompted to select which one to use.

However, it is possible that there are no applications that can handle your intent. In this case, your application will crash when you invoke startActivity(). To avoid this, before calling startActivity() you should first verify that there is at least one application registered in the system that can handle the intent. To do this use resolveActivity() on your intent object:

   // Verify that there are applications registered to handle this intent

    // (resolveActivity returns null if none are registered)

  •     if (sendIntent.resolveActivity(getPackageManager()) != null) {
  •         startActivity(sendIntent);
  •     } 

68.The last callback in the lifecycle of an activity is onDestroy().The system calls this method on your activity as the final signal that your final activity instance is being completely removed from the system memory. Usually, the system will call onPause() and onStop() before calling on destroy(). Describe a scenario, though, where onPause() and onStop() would not be involved.

Ans:

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.

Although onDestroy() is the last callback in the lifecycle of an activity, it is worth mentioning that this callback may not always be called and should not be relied upon to destroy resources. It is better to have the resources created in onStart() and onResume(), and have them destroyed in onStop() and onPause(), respectively.

Android Sample Resumes! Download & Edit, Get Noticed by Top Employers! Download

69.Which of the code snippets below is the correct way to check if a Compass sensor is present on the system? Explain your answer.

1.

  • PackageManager m = getPackageManager();
  •     if (!m.hasSystemFeature(PackageManager.FEATURE_SENSOR_COMPASS)) {
  •         // This device does not have a compass, turn off the compass feature
  •     }

2.

  •   SensorManager m = getSensorManager();
  •     if (!m.hasSystemFeature(SensorManager.FEATURE_SENSOR_COMPASS)) {
  •         // This device does not have a compass, turn off the compass feature
  •     }

3.

  •   Sensor s = getSensor();
  •     if (!s.hasSystemFeature(Sensor.FEATURE_SENSOR_COMPASS)) {
  •         // This device does not have a compass, turn off the compass feature
  •     }

Ans:

The correct answer is Answer 1, the version that uses PackageManager.SensorManager and Sensor are part of Android Sensor Framework and are used for direct access and acquisition of raw sensor data. These classes do not provide any method like hasSystemFeature() which is used for evaluation of system capabilities.

Android defines feature IDs, in the form of ENUMs, for any hardware or software feature that may be available on a device. For instance, the feature ID for the compass sensor is FEATURE_SENSOR_COMPASS.

If your application cannot work without a specific feature being available on the system, you can prevent users from installing your app with a <uses-feature> element in your app’s manifest file to specify a non-negotiable dependency.

However, if you just want to disable specific elements of your application when a feature is missing, you can use the PackageManager class. PackageManager is used for retrieving various kinds of information related to the application packages that are currently installed on the device.

70.Describe three common use cases for using an Intent.

Ans:

Common use cases for using an Intent include:

  • To start an activity: You can start a new instance of an Activity by passing an Intent to startActivity() method.
  • To start a service: You can start a service to perform a one-time operation (such as downloading a file) by passing an Intent to startService().
  • To deliver a broadcast: You can deliver a broadcast to other apps by passing an Intent to sendBroadcast(), sendOrderedBroadcast(), or sendStickyBroadcast().

71.Suppose that you are starting a service in an Activity as follows.

  • Intent service = new Intent(context, MyService.class);             
  • startService(service);

Where Myservice accesses a remote service via an Internet connection.

If the activity is showing an animation that indicates some kind of progress,what issue might you encounter and how could you address it?

Ans:

Responses from a remote service via the Internet can often take some time, either due to networking latencies, or load on the remote server, or the amount of time it takes for the remote service to process and respond to the request.

As a result, if such a delay occurs, the animation in the activity (and even worse, the entire UI thread) could be blocked and could appear to the user to be “frozen” while the client waits for a response from the service. This is because the service is started on the main application thread (or UI thread) in the Activity.

The problem can (and should) be avoided by relegating any such remote requests to a background thread or, when feasible, using an asynchronous response mechanism.

Note well: Accessing the network from the UI thread throws a runtime exception in newer Android versions which causes the app to crash.

72.Normally, in the  process of carrying out a screen reorientation, the Android platform tears down the foreground activity and recreates it, restoring each of the view values in the activity’s layout.

In an app you’re working on, you notice that a view’s value is not being restored after screen reorientation. What could be a likely cause of the problem that you should verify,at a minimum, about that particular view?

Ans:

You should verify that it has a valid id. In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

73.What’s the relationship between the life cycle of an AsyncTask and an Activity? What problems can this result in? How can these problems be avoided?

Ans:

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.

74.What is an Intent? Can it be used to provide data to a ContentProvider? Why or why not?

Ans:

The Intent object is a common mechanism for starting new activity and transferring data from one activity to another. However, you cannot start a ContentProvider using an Intent.

When you want to access data in a ContentProvider, you must instead use the ContentResolver object in your application’s Context to communicate with the provider as a client. The ContentResolver object communicates with the provider object, an instance of a class that implements ContentProvider. The provider object receives data requests from clients, performs the requested action, and returns the results.

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

Ans:

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.

76.What is the difference between Serializable and Parcelable ? Which is the best approach in Android ?

Ans:

Serializable is a standard Java interface. You simply mark a class Serializable by implementing the interface, and Java will automatically serialize it in certain situations.

Parcelable is an Android specific interface where you implement the serialization yourself. It was created to be far more efficient than Serializable, and to get around some problems with the default Java serialization scheme.

77.What are “launch modes”? What are the two mechanisms by which they can be defined? What specific types of launch modes are supported?

Ans:

A “launch mode” is the way in which a new instance of an activity is to be associated with the current task.

Launch modes may be defined using one of two mechanisms:

  • Manifest file. When declaring an activity in a manifest file, you can specify how the activity should associate with tasks when it starts. Supported values include:
    • standard (default). Multiple instances of the activity class can be instantiated and multiple instances can be added to the same task or different tasks. This is the common mode for most of the activities.
    • singleTop. The difference from standard is, if an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.
    • singleTask. A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.
    • singleInstance. Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.
  • Intent flags. Calls to startActivity() can include a flag in the Intent that declares if and how the new activity should be associated with the current task. Supported values include:
    • FLAG_ACTIVITY_NEW_TASK. Same as singleTask value in Manifest file (see above).
    • FLAG_ACTIVITY_SINGLE_TOP. Same as singleTop value in Manifest file (see above).
    • FLAG_ACTIVITY_CLEAR_TOP. If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent(). There is no corresponding value in the Manifest file that produces this behavior.

78.What is the difference between Service and IntentService? How is each used?

Ans:

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.

79.How do you supply construction arguments into a Fragment?

Ans:

Construction arguments for a Fragment are passed via Bundle using the Fragment#setArgument(Bundle) method. The passed-in Bundle can then be retrieved through the Fragment#getArguments() method in the appropriate Fragment lifecycle method.

It is a common mistake to pass in data through a custom constructor. Non-default constructors on a Fragment are not advisable because the Fragment may be destroyed and recreated due to a configuration change (e.g. orientation change). Using #setArguments()/getArguments() ensures that when the Fragment needs to be recreated, the Bundle will be appropriately serialized/deserialized so that construction data is restored.

80.What is ANR, and why does it happen?

Ans:

‘ANR’ in Android is ‘Application Not Responding.’ It means when the user is interacting with the activity, and the activity is in the onResume() method, a dialog appears displaying “application not responding.”

It happens because we start a heavy and long running task like downloading data in the main UI thread. The solution of the problem is to start your heavy tasks in the background using AsyncTask class.

81.Which method is called only once in a fragment lifecycle?

Ans:

onAttached()

82.Is it possible to create an activity in Android without a user interface ?

Ans:

Yes, an activity can be created without any user interface. These activities are treated as abstract activities.

83.What are the different data types supported by AIDL?

Ans:

AIDL or Android Interface Definition Language facilitates the communication between the client and service. Data Types supported by AIDL are as follows:

  • String
  • List
  • Map
  • charSequence
  • INT, Long, Char, Boolean (Java data types)

84.What is a broadcast receiver?

Ans:

The broadcast receiver communicates with the operating system messages such as “check whether an internet connection is available,” what the battery label should be, etc.

Are you looking training with Right Jobs?

Contact Us

Popular Courses