Friday, January 23, 2015

Experienced Android Interview Questions - 1

Company : Beeonics

1. If you have multiple Broadcast receivers how do you identify for which application is it coming?
Ans:  if the question is how does Android sends broadcasted message to different application, then it is based on the action, data, and category present in the intent.
Android will perform action test, data & type test, and category test. If all the test are passing with any of the intent filters of receivers, then those receivers will be triggered.
If multiple receivers intent-filters are matching with the intent then all the receivers will be triggered. But there is no specific order if priority is not set in the intent filter.

2. How do you check memory leaks?
go to ddms, get the heap profiling dump.
generally .hprof files will be in /data/misc folder.
save the heap dump profile to desktop.
Now use eclipse memory analyzer to analyze the memory of your app.

source: http://developer.android.com/tools/debugging/ddms.html
source: http://kohlerm.blogspot.in/2009/04/analyzing-memory-usage-off-your-android.html


3. Do you require any key for development?
If you are planning to release any application into the market, then it is mandatory to sign your application with private key.
But at the initial stages, you can use debug key which is located in c:\users\<username>\.android folder.
But remember you can't release an application which is signed with your debug key. You have to sign with a private key obtained with keytool
command. keytool is a .bat file available in java\jdk\bin folder.

You can use keytool to create your own private key with password.
Note: Important to remember that don't ever forget your key and password, else it would become impossible to upgrade your application.
To upgrade your application you have to sign with same application.

More about how to create private key and how to use keytool, please refer to below official android link.
http://developer.android.com/tools/publishing/app-signing.html

4. How do you upload app into playstore?
If you want to upload an app into google play store, then follow below steps.
1. Create a private key to sign your application.
   Note: to create private key, use keytool command.
2. sign your application with private key.
   Go to file->export-> and select your private keystore to sign.
3. Go to https://play.google.com/apps/publish/signup/
4. If you are uploading your first app, then you have to pay one time reg
   fee of $25.
5. Go to developer console
6. Click on add new application +
7. Add apk and title , and follow with rest of the process.

5. How do you start push Notifications?
If this question is about sending a push notification to the client mobiles from the server, then Server side program has to be configured to use GCM (Google Cloud Messaging). Once the server is configured to use GCM and client android app also configured with GCM client framework, then GCM will automatically push the notifications from server to the client mobile by using GCM framework.

More about using GCM framework to send push notifications from server to client (android phones), follow this official android link.
http://developer.android.com/google/gcm/index.html
http://android-developers.blogspot.in/2013_06_01_archive.html

6. What is debugging mode?
You can build your application in 2 modes.
1. Debug Mode : Generally we use debug mode when we develop and test our application.
2. Release Mode : We use release mode, when we are about to release our application into play store.

Company : digipay
1. Can we create .apk file without eclipse?If yes how?
1. use javac to compile your java files
2. use dx tool to convert all .class files to single dex file
3. use aapt tool or apkbuilder tool to generate .apk file, which contains    manifest, .dex, and res.
   .apk is equivalent to .jar in java.
   aapt tool and dx tools are in android-sdk/platform-tools.
4. sign the apk file by using jarsigner tool of jdk/bin
5. zipalign your signed apk by using zipalign tool of android-sdk/tools

2. Can we show multiple activity at a time? If no why? Also asked what is activity?
In android activities are piled up on each other in a stack based architecture. System will display the activity which is on top of the activity stack, hence it is no possible to show 2 activities at a time.

But there is an option called ActivityGroup, which can contain and run multiple embedded activities in a screen. One such kind of ActivityGroup is TabActivity.
Note: TabActivity and ActivityGroup are deprecated, better to use Fragments to club multiple screens in a screen.

Fragment is the new technique added in 3.0 onwards, which are basically used to club different screens(panes) in a single wider screen sizes like Tablets.

Note: Don't use TabHost and TabActivity, as they are deprecated.
In place of them use ActionBar and fragments. ActionBar is something like TabHost in old versions. ActionBar also contains tabs, on clicking which it will display different fragments.

3. What is ADB? Any commands?
4. What is android? Where we can use android OS other than mobiles, tabs etc?
Android is O.S and framework to develope applicatins for mobiles and tablets.
Android is based on Linux O.S.
Android can be used in tvs, automobiles, and wears like watches, glasses etc.
Android is also used in kitchen utensils which is also called Google's smart kitchen.
Android can also be used in robots, washing machines, etc.

4. How to support text views for multiple screens?Suppose one mobile 3 inch another 7 inch?
When we are using a textview there is a possibility that our app may be downloaded into various screen size devices.
Based on the screen sizes we have to make sure our textview should grow and shrink, similarly the text size also should look compatible with the screen size. Below is the solution for this problem.

various screen sizes have various folders for dimensions.
eg:
res/values/dimens.xml will contain screen properties for normal screens.
res/values-sw600dp/dimens.xml will contain screen properties for screen width category 600 dp.
So try to declare different text sizes for different screen widths.
This will work perfectly with all screen sizes.

Note: Using sp or dp will not fix your issue.
Note: If you are not specifiying sp or dp, then giving wrap content or match_parent would be the best suggestion to solve the problem

5. what is aidl?
6. what is adapter?
7. What is min and max sdk version?
Manifest file has an option to tell what is the minium sdk version , and maximum sdk version your app can be downloded.
It simply says what all the various phone ranges your app supports or can be downloadable.

There is one more option target sdk, which says I have compiled and cross checked with this version and I am targeting for this version.
So that android system won't add any compatible library when your app is downloaded into a phone which is same as your target sdk version.


8. give one example for anr?

company: ivy mobility
1. Difference between Set and Array List? (java)
set interface: 1. Sets can't contain duplicate values (just like a mathematical set), 
                         2. Sets inherits collection and adds method to avoid duplications

There are 3 Set implementations:

1. hashset
   - uses hash table/ hashmap internally to store values, 
   - it is very fast compared to other two set implementations, 
   - it will not have any order while reading, 
   - it will not contain any duplicate elements, 
   - it is not synchronized by default
   - will not guarantee any order over time
   - iteration order/ reading order is not predictable.
     the order in which u entered may differ while reading.

2. treeset
   - internally uses red-black tree to store the values, 
   - it will maintain ascending order
   - it is slowest of all other 2 set implementations.
   - rest of the properties remain same

3. linkedhash set
   - internally uses hash table with linked list to store values.
   - same as hashset but order in which user entered will be persisted.
   - it is neither fast nor slow comparatively.
   - rest of the properties remain same.


list
----
 - it is ordered (maintains the insertion order) & may contain duplicate values
 - inherits from collection, and add functions for indexof, search functionalities.

List implementations
1. arraylist  (better performance) - same as vector .. 
2. linked list (better performace in some circumstances)


2. What is an Interface? Explain with realtime use case? (java)
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.
                   
Use cases of Interfaces:

1. It can be used as APIs for third party libraries or server side libraries when using with client application. This is the main use case.
    Eg: If you are selling some libraries to some clients, then you will not expose the code. Rather you give in terms of .lib format.
          But since lib is unreadable format, you have to create an interface file telling what all the functions available in that library.
    Eg: Similarly if you want to access some server side functionalities in your client application, then also we use interfaces with some libraries.

2. You can use interfaces to implement multiple inheritance. 

3. What are the methods in Async Task?
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.

4. What is a Handler & what is the function of the Handler in Android?
Handler : is used to communicate between two threads.

Use of Handler: If you want to send or pass some message from one thread to other thread, then we can use Handlers.
                             1. A thread can have n number of handlers allowing other threads to communicate with it.
                             2. If we have Thread-A and Thread-B, and if Thread-B wants to send a message to Thread-A, then We have to create a handler
                                  for Thread-A and send message to that handler. Via handler Thread-B can send messages to Thread-A.


5. What is Anchor View in a Relative layout?
Anchor view tells about the relative position of view with respect to a given anchor view.
For eg: android:layout_above property will make sure that the current view's base line will be above the anchor view id given in this property.

More about anchor:
----------------------------
android:layout_above Positions the bottom edge of this view above the given anchor view ID. 
android:layout_alignBaseline Positions the baseline of this view on the baseline of the given anchor view ID. 
android:layout_alignBottom Makes the bottom edge of this view match the bottom edge of the given anchor view ID. 
android:layout_alignEnd Makes the end edge of this view match the end edge of the given anchor view ID. 
android:layout_alignLeft Makes the left edge of this view match the left edge of the given anchor view ID. 

6. How do you make your Application available to others for example you are using browser app if someone wants to use your app how you will make it available to others.
7. How to pass the objects to all activities?
If you want to pass objects between activities, there are multiple options for this.
1. You can implement Serializable for that class, and then pass serializable object in putExtra of intent.
2. Since serializables are heavy compared to parcels, second and better option can be passing parcels in extras.

1st way:
-----------
class ABC implements Serializable{
 ....
}

ABC a = new ABC();
Intent in = new Intent();
in.putExtra("class",a); //this should take serializable objects

2nd way:
------------
Though parcels are fast compared to serializables, but building a parcel is very tough and complicated.
It may affect the readability of the program. Parcels are heavily used only in IPCs (Inter Process Communications) and Binder Services by android
Otherwise to make it simple for reading, better to go with serializables.


8. If an activity is in foreground what all the lifecycle methods to be called?
If an activity is in foreground, then android will call onCreate(), onStart(), and onResume() sequentially before making it as foreground.

9. In life cycle in general it will be on Pause() ->on Stop but tell me example for on Pause()->on Resume()
eg 1. When your activity is in foreground or running state, turn off the LCD (will call onPause on your activity), and again turn on LCD (will call onResume)
eg 2. When your activity is in foreground or running state, display an activity in theme dialog  (will call onPause of your old activity), once dialog themed activity gets destroyed android will call onResume of your old activity.
eg 3. When your activity is in foreground or running state, display an activity which is transparent (will call onPause of your old activity), once transparent activity gets destroyed android will call onResume of your old activity.


10. Random Nos Program code? i.e How will you generate random numbers? (java)
Below code will generate a random number between 0 to 100 and stores it in variable "i".
int i = (int) (Math.random()*100); 

company: mindtree
1. Have u worked on AIDL?
Only if one is working on binder services, then only there is a fair chance of working on AIDL. IPC (Inter Process Communication) in android happens through kernel level Binder driver.
AIDL (Android Interface Definition Language) is a mechanism which internally uses Binders to access or to bind services of other applications.
Binder is an efficient way to do IPC when compared to serialization, thats why AIDL adopts Binder.

When a client application binds a remote service (which is in other application), that service will return an object using which client can call remote methods.
Since both client and service are in different applications, client would not know the type of object which is going to be returned. This is where AIDL
will be useful. 

Steps to implement AIDL in service side (server side):
1. create aidl file, which contains function declarations which you want to expose to the client.
2. implement interface stub method in your service file.
3. create an object for stub and return it as binder object to client

steps to implement AIDL in client side:
1. implement ServiceConnection object, and get the binder object in 
onServiceConnected() method, using aidl file interface name which was exposed by service. 
2. Start calling remote methods by using that object.

2. Why we don’t  give  min sdk version as 1?
There are very negligible number of devices who runs version 1 of android. It is almost deprecated completely. Setting min sdk is to make sure that an application is reaching wide range of users. As of today it will be sufficient to reach major number of users if we set min sdk version as 2.2.

3. What are the basic uses of manifest file?
Every application will contain one manifest file which contains important information about the application which system must know before it can run the application.  1. It contains package name of your application. 2. it contains various components used in the application like how many activities, services, receivers and content providers available in your application. 3. It contains all permissions required to run the application properly. 4. It contains minimum level of api required to run this application. 5. It lists all the libraries required to run the application. etc.

4. What is the use of registering broadcast receiver in manifest file?
Registering a receiver in manifest file makes sure that our receiver will be triggered even if our application is not running currently. For example when a new incoming SMS comes it should be handled by Messaging application even if it is not running currently. In this case programmer has to register the receiver in messaging application's manifest file to respond to incoming sms.

5. Commands to write or create .apk file?
1. use javac to compile your java files
2. use dx tool to convert all .class files to single dex file
3. use aapt tool or apkbuilder tool to generate .apk file, which contains    manifest, .dex, and res.
   .apk is equivalent to .jar in java. 
   aapt tool and dx tools are in android-sdk/platform-tools.
4. sign the apk file by using jarsigner tool of jdk/bin
5. zipalign your signed apk by using zipalign tool of android-sdk/tools 

6. Can we create all ui coding  in activity without using xml?
Ui can be completely created through programming. But that is not recommended. There should be clear separation between design and the logic. That is the reason why android has given res folder to create all the designs like layouts, menus, icons, and styles. If project demands some dynamic changes to the UI, which can't be done in the xml files, then programmer can use any View or ViewGroup classes to design the UI through programming.


lLayout = new LinearLayout(this);
        lLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
        tView = new TextView(this);
        tView.setText("Hello, This is a view created programmatically!");
        tView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
        lLayout.addView(tView);
        setContentView(lLayout);

7. Life cycle of activity and Fragments?what is Fragment?
8. When I run any application where fragments are there which method will be called first?
Just like Activities has life cycle methods, similarly fragments also have their life cycle methods. When we load a fragment first method to be called is onAttach(), followed by onCreate(), which is followed by onCreateView(). Generally programmers will not implement onAttach() method. onCreate() is where we will initialize all the variables which are used in the fragment. onCreateView() is where we will inflate or load the xml file of our fragment and return it to the parent (activity).

9. Can we have fragments without activity?
It is not possible to have fragment with out an activity. Fragments are part of activity. Fragments always lies with in the activity. An activity can exist without a fragment where as fragment with out an activity is not possible. You can assume fragment like a thread and activity is like a process, where threads are always part of a process. Every fragment will contribute its own UI to the parent activity.

10. Tell me different IDEs other than eclipse?
Android can be developed by using below IDEs:

Eclipse
NetBeans
IntelliJ
Re-Sharper
Android Studio which is based on IntelliJ

As of now plugins are up to date for Eclipse.
But soon Android Studio is going to be the official IDE for android development.

11. Asked about memory leakage?How to handle it
To fix memory leaks, first we should know if our application is leaking any memory. For that use Logcat and check for GC messages and observe if free memory is going down. To understand this you can rotate your phone for couple of times and observe gc messages in logcat if free memory is going down. If you see that free memory is going down, that is an indication of memory leak. To fix memory leaks you can use MAT(memory analyzer tool) of eclipse. Before doing that you should create a hprof file which contains the memory status of your application. Open up hprof file in the memory analyzer tool to check for dominator tree and find out what is causing for your memory leak. Note: 70-80% of the memory will leaked by bitmap images, so be careful while using them, and try to avoid un necessery static pointers to them to avoid memory leaks. For more on this, checki https://www.youtube.com/watch?v=_CruQY55HOk



6 comments:

  1. Excellent Question bank for the Android interview Question and Answers. Good collection of author. Appreciable.

    ReplyDelete
  2. Thanks for sharing the valuable information with us.These are really great and useful in all aspects,appreciated!
    Android Interview Questions
    Android Basic Interview Questions

    ReplyDelete
  3. I am so happy after reading your blog. It’s very useful blog for us.

    Python Corporate training in Uganda

    ReplyDelete
  4. Please continue this great work and I look forward to more of your awesome posts.

    in-house training program in Nigeria

    ReplyDelete
  5. Thanks you for sharing this unique useful information content with us. Really awesome work. keep on blogging
    Artificial Intelligence Course
    Java Course
    AWS Course
    Machine Learning Course
    Data Science Course
    DevOps Course

    ReplyDelete
  6. I read your blog this is a informative blog Impressive CV format! For top-notch opportunities, consider AMCAT.
    Read more: https://www.myamcat.com/resume-format

    ReplyDelete