Android NDK Interview Questions

General


 

1. What is Android NDK?

Native development Kit as it it called gives you ace

2.  When can the callback implementations to NativeActivity cause an ANR?

Native activity contains all the callback information and data structures.  The main thread handles the callbacks.  So, your callback implementations should not be blocking.  If they block, you may receive an ANR because your main thread is unresponsive until the callback returns.

3.

Advertisements

Android Interview questions

Threads


 1.  During Android unit testing, what is the difficulty in using ‘static’ variables?

Static variables are cleared and set to ‘null’ each time a set of data is called.  This is because your app is reset/killed each time

Solution:  Call super.tearDown() before each test.

2.   What is dependency injection in Android?

Dependency injection is a style of object configuration in which an objects fields and collaborators are set by an external entity.

In Android, Dagger & Guice are examples of dependency injectors:

RoboGuice 2 example:

    @ContentView(R.layout.main)
    class RoboWay extends RoboActivity { 
        @InjectView(R.id.name)             TextView name; 
        @InjectView(R.id.thumbnail)        ImageView thumbnail; 
        @InjectResource(R.drawable.icon)   Drawable icon; 
        @InjectResource(R.string.app_name) String myName; 
        @Inject                            LocationManager loc; 

        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            name.setText( "Hello, " + myName ); 
        } 
    }

 

3.  How is Serializable different from Parcel?

Parcels are designed for high performance IPC transport, used heavily in Binders.  For normal serialization concepts, use Serializable.  Serializable is much heavier and takes more time.  Therefore Parcels are used in embedded devices.

4.   What is a Handler, Thread & Loader?

Handler:  A class that can synchronize a worker thread to the main thread.  In the example of ASyncTask, the onProgressUpdate() and the doPostExecute() method are already synchronized.  They are examples of handlers.  You can create your custom handler methods for your custom thread classes.

Thread:  A line of execution.

Loader:

  • It provides asynchronous loading of data.
  • They are available to every Activity and Fragment.
  •  They monitor the data source and deliver new results when contents change.
  • They automatically reload to their last loader cursor when being recreated after a configuration change.  They don’t need to re-query data

5.  What is a looper, message queue and handler?

Looper – part of any Thread to loop through message queue.  This will be used to check if any incoming message has arrived to this thread. Only one looper exists for a given thread. Only for handler threads this looper will be activated, for other normal threads this will be in passive or inactive mode.

Message Queue – part of any thread, will store incoming messages to this thread. For any thread only one message Q is available. Handler – communication channel between two threads.

Handler is associated with Looper. for a given looper we can n number of handlers to communicate with it from out side world.

Memory Leaks


 1.  What are some of the causes for memory leaks in Android?

Leaking a context causes references to an Activity’s view group and all its resources inside it.

2.  What is the most common memory leak in Android?

Screen orientation change.  When this happens,  Android destroys the current activity and creates a new one  preserving its state.   This can cause a leak in the entire Activity and all its references resources.

3.  What is a leak?

Keeping a reference to any object that the garbage collector is unable to collect.

4.  How to avoid Context related leaks?

  • Avoid using non-static inner classes in an activity if instances of the inner class could outlive the activity’s lifecycle. Instead, prefer static inner classes and hold a weak reference to the activity inside.

5. How can you leak a Context?

In Java, non-static inner and anonymous classes hold an implicit reference to their outer class. Static inner classes, on the other hand, do not.

Views


 1.  How do you create a View programmatically without using a View  Inflater service?

LinearLayout myLayout = findViewById(R.id.main);

Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                          LinearLayout.LayoutParams.FILL_PARENT,
                          LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

Context


 1. What are contexts used for?

Various reasons but mostly to load and access resources.  This is why all widgets have a context parameter.

2.  What are some types of Contexts?

Application Context

An instance of Context that is tied to the lifecycle of the application.

Example:  Use when you need something that is tied to a global scope.  Such as a WakefulIntentService.  To get a static WakeLock you would need a context to PowerManager.  In this case it is safest to use getApplicationContext().

Activity context

An instance of Context that is tied to the lifecycle of an activity.

Usually passed to classes and methods that require a context.   Holds a reference to a Viewgroup and all the resources inside it.   If a context leaks, the entire activity can be leaked since a reference is always held causing a lot of memory to be held up.

Services


1.  What is a started Service?

When this service is started, it has a lifecycle that’s independent of the component that started it and the service can run in the background indefinitely, even if the component that started it is destroyed. As such, the service should stop itself when its job is done by calling stopSelf() or another component can stop it by calling stopService()

2.  What is a bound Service?

You should create a bound service when you want to interact with the service from activities and other components in your application or to expose some of your application’s functionality to other applications, through interprocess communication (IPC).

Application Questions


 1. Photo Sharing App / notification manager attach to a notification object, what would you use

Pending Intent

2. Data from one app to another and if you want some fields hidden

Content Provider

3. If your app has to have messaging as an option, what would you use to navigate to the messaging app.

 

4.  If you want an animation to appear .5 sec after a button click, what would you use?

 

5. Android Service – Does it run on UI thread?

Yes

6.  How do you schedule a recurring task at periodic intervals

Alarm Manager

Java Interview Questions

1.  What is the purpose of ‘final’,  ‘finalize’ and ‘finally?

final

final can be used to mark a variable “unchangeable”

private final String name ="foo";//the reference name can never change

final can also make a method not “overrideable”

public final String toString(){return"NULL";}

final can also make a class not “inheritable”. i.e. the class can not be subclassed.

public final class finalClass {...}
public class classNotAllowed extends finalClass {...}// Not allowed

finally

finally is used in a try/catch statement to execute code “always”

lock.lock();
try{
//do stuff
}catch(SomeException se){//handle se}
finally{
  lock.unlock();//always executed, even if Exception or Error or se}

finalize

finalize is called when an object is garbage collected. You rarely need to override it. An example:

public void finalize(){
     //free resources (e.g. unallocated memory)
     super.finalize();
}