Friday, January 23, 2015

Experienced Android Interview Questions - 2

company - mportal

1. What is an Interface and What is an Abstract class? (java)
Abstract class: is an incomplete class. This is a kind of a house under construction which is not yet completed.
                            Since abstract classes are incomplete, you can't create any object for that class. It doesn't make any sense to create object.
                            But any one can inherit abstract classes to give extra definition for that class to make it more concrete.
                            Abstract class can contain 0 or more number of abstract methods. If it is having at least one abstract method, then
                            we have to make that class as abstract class.
                             A class will become abstract class, if we know only partial implementation of that class.
                             Abstract class can contain method definitions.

Interface: as name suggests, it is an interface between your class and out side world.
                  interfaces can contain only constants, method declarations, and nested types. It can't contain method definitions.
                  Just like an abstract class, you can't create object for an interface.
                  You can imagine interface just like a header file in c program. It tells what all the functionalities supported by your interface.
                 

2. Explain about Async Task with one use case
Async Task: is a way to achieve multi threading in android. It has some advantages over normal java threads.
                       1. You don't need to use any Thread keyword, every thing will be taken care by AsyncTask class internally.
                            It creates threads internally.
                        2. Since android follows single threaded UI model, where other threads can't touch UI components directly.
                             But from async task you can directly touch UI components from all the functions except from doInBackGround().

AsyncTask class will have 4 functions.
a. onPreExecute() - this is the first function to be called before calling doInBackGround(). This runs in UI thread. You can touch ui from this.
b. doInBackGround() - this runs in background thread created by AsyncTask class. Don't touch ui from this method.
                                         Write background heavy logic in this function, as it runs in background thread.
c. onProgressUpdate() - this function is used to update UI while performing background functionality. to execute this call publishProgress()
                                             this also runs in UI thread.
d. onPostExecute() - once doInBackGround() is finished this function will be called. If you want to update any UI after doInBackGround() use this                                
                                     function. This also runs in main ui thread.

Async task use case: If you want to upload some images to facebook server and want to update UI (progress bar) while uploading images using progress bar, then you can use AsyncTask.

3. What is Mutable and Imutable explain with examples? (java)
There are many concepts related to mutability and immutability.

General meaning:
Mutable : means changeable;   Immutable : means unchangeable.

Some of the Immutable concepts:
----------------------------------------------
Immutable classes : are also known as final classes. A class which you can't inherit (can't change the definition by extending it) is called as
immutable class. In some languages it is also called as shield classes. Eg: String class in java is immutable class, you can't inherit it.
use case: If you don't want any one to inherit your class and change the basic functionality of your class, then make it as immutable class.
This can be achieved by making your class as final class by using "final" keyword before your class.

Immutable variables (also know as final variables): also known as constants
Any variable that is declared as final, you can't modify its value.
eg: public static final double pi = 3.14; // we know that pi value will always remain same 3.14, no body can change its value. So we are declaring it as final , ie constant. So that no one can change its value.

Immutable methods (final methods): says, no body can modify or change my functionality.
Inherited classes (derived classes) can't override immutable methods.
use case: Framework generally gives lot of basic functionality where derived classes can't change. such kind of methods are made as final methods by using "final" keyword before that method name.

Immutable objects: (Note: don't misunderstand this with immutable variables)
an object for which we can't change the states(data) of it once it is set. If we can achieve this , then that object is called as immutable object.
For eg: String str = "hello";
In the above example hello is immutable, no body can change it. If you try to change it it will create a new string object.
String objects are examples of immutable objects.

4. Can class be immutable? (java)
Yes, a class can be immutable. You can make a class as final to make it immutable

Immutable classes : are also known as final classes. A class which you can't inherit (can't change the definition by extending it) is called as
immutable class. In some languages it is also called as shield classes. Eg: String class in java is immutable class, you can't inherit it.
use case: If you don't want any one to inherit your class and change the basic functionality of your class, then make it as immutable class.
This can be achieved by making your class as final class by using "final" keyword before your class.


5. Difference between Final ,Finally and Finalize (java)
final - means immutable. eg: final classes, final methods, final variables.
           once you say final, it is the final definition of that component, no one can change it.

finally - is used with try-catch block to make sure that you are cleaning all the system resources properly when an exception occurs.
             what ever the code you write, will be executed with out fail once try block exits.

finalize - is the method that will be executed(called) before that object is being garbage collected.
                Garbage collector will make sure that it will finalize() method of an object, before it cleans that object.
                Garbage collector will clean only java resources and java memory. In your program if you are using any non java resources like files,  
                sockets, graphic contexts, or allocating memory using a c program then garbage collector will not clean all those resources.
                It is the responsibility of the programmer to clean them properly, For that we use finalize method.
                Note: Don't depend too much on finalize() method to clean non java resources.

6. What is thread? Why we use Thread? (java)
Thread: is independent dispatch-able unit to cpu. each thread can execute an independent module in parallel with CPU.
               Thread is also known as light weight process. Each process can contain multiple threads.
               Threads will share the resources of its process.

Why to use a thread: Generally your CPU might have the capability of running multiple tasks at a time by using its threads.
                                      But programmer has to support this by creating multiple threads for his program where ever it is possible.
                                      If your program is having two modules, and there is no dependency between those two modules, then
                                      your program can run faster if you run both the modules at a time (in parallel). This can be done only
                                      if you are running those two modules in two different threads.

7. Tell me about Main Components in Android?
Android main components are Activity, service, Broadcast Receiver, and Content Provider.
For details information about these components please refer to developer.android.com

8. What are the layouts in Android?
Layouts are viewgroups, which can contain child views in it.
There are 4 basic layouts in android.

1. linear layout: displays child views in linear fashion (either horizontally or vertically).
2. frame layout: displays child views in stack architecture (one on top of each other). It is also used as place holder for the elements.
3. relative layout: displays child views in relative position to other child views. This is heavily used layout in android.
4. absolute layout: displays child views with absolute position. This is deprecated because of its absolute positioning.

9. What is a Broadcast Receiver?
Broadcast Receiver: is the component of android which gets triggered when ever android systems sends a broadcast announcement.
                                      Generally, when ever an important event happens in the phone (like battery low), android system will send a broad
                                      cast message to all the applications. Who ever is interested to catch that message can use Broadcast Receiver to
                                      handle that message.

Note: Broadcast receiver has a time limitation of 10 seconds. What ever you want to do in a receiver, you have to finish it off in 10 seconds.
           Else android may force close your receiver.

10. What is an Intent?
Intent: is a message passing mechanism between 2 components of android.
There are 2 use cases of intents.

1. You can start other component using intent, except content provider.
    eg: you can start one activity from other activity by using intent. 
           You can also start a service from an activity.

2. You can use intent to pass some data from one component to other component.
    Eg: you can pass username and password from login screen to home screen of your application.

There are two types of intents are there in android. a. Explicit intent b. Implicit intent.

11. How do you know whether GPS is on or Off in your moble?
//declare location manager
LocationManager lm;
//define location listner
private LocationListener ll = new LocationListener() {
@Override
public void onProviderEnabled(String provider) {
//called when user enables gps or wifi in the phone
}
@Override
public void onProviderDisabled(String provider) {
// called when user disables gps or wifi in the phone
}
};

//create location manager object
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
//request for location updates
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 
0, 0, ll);


12. If GPS is not on is there any other way to find the location?
If GPS is not available, then you can use NETWORK_PROVIDER which uses wi-fi signals or cell tower information to find out your mobile location.

13. What is a ViewGroup?
ViewGroup : is an invisible container of other child views or other view groups. Eg: LinearLayout is a viewgroup, which can contain other views in it.

14. How do we solve Resolution Problem
There are many solutions for this resolution problem:

1. Always use dps, don't use pixels
2. Use nine patch (.9.png) images in places of normal images. Normal png images won't stretch properly.
3. Use relative layouts where ever it is possible.
4. Don't give hard coded width and height, always mention height and width relative to parent view or layout.



No comments:

Post a Comment