Thread Concepts

1) What is a daemon thread?

Sol: A daemon thread is a thread whose execution state is not evaluated when the JVM decides if it should stop or not. The JVM stops when all user threads (in contrast to the daemon threads) are terminated. Hence daemon threads can be used to implement for example monitoring functionality as the thread is stopped by the JVM as soon as all user threads have stopped:

public class Example {

	private static class MyDaemonThread extends Thread {

		public MyDaemonThread() {

		public void run() {
			while (true) {
				try {
				} catch (InterruptedException e) {

	public static void main(String[] args) throws InterruptedException {
		Thread thread = new MyDaemonThread();

The example application above terminates even though the daemon thread is still running in its endless while loop.

CS General Concepts

1) What is POSIX?

The Portable Operating System Interface (POSIX)[1] is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines the application programming interface (API), along with command line shells and utility interfaces, for software compatibility with variants of Unix and other operating systems.

Android Basics


An activity is a class that represents a single screen. It is like a Frame in AWT.


A view is the UI element such as button, label, text field etc. Anything that you see is a view.


Intent is used to invoke components. It is mainly used to:

  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.


Service is a background process that can run for a long time.

There are two types of services local and remote.
Local service is accessed from within the application whereas remote service is accessed remotely from other applications running on the same device.

Content Provider

Content Providers are used to share data between the applications.

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.


Fragments are like parts of activity. An activity can display one or more fragments on the screen at the same time.


It contains informations about activities, content providers, permissions etc. It is like the web.xml file in Java EE.

Android Virtual Device (AVD)

It is used to test the android application without the need for mobile or tablet etc. It can be created in different configurations to emulate different types of real devices.

2) About Activity LifeCycle


Activity States:

State Description


Activity is visible and interacts with the user.


Activity is still visible but partially obscured, instance is running but might be killed by the system.


Activity is not visible, instance is running but might be killed by the system.


Activity has been terminated by the system of by a call to its finish() method.

Activity Methods

Method Purpose


Called then the activity is created.

Used to initialize the activity, for example create the user interface.


Called if the activity get visible again and the user starts interacting with the activity again.

Used to initialize fields, register listeners, bind to services, etc.


Called once another activity gets into the foreground. Always called before the activity is not visible anymore.

Used to release resources or save application data. For example you unregister listeners, intent receivers, unbind from services or remove system service listeners.


Called once the activity is no longer visible.

Time or CPU intensive shut-down operations, such as writing information to a database should be down in the onStop() method.

This method is guaranteed to be called as of API 11.

3) Saving and restoring Activity Instance State

Instance state of an activity which is required to restore the activity to the state in which the user left it. This is non-persistent application data that needs to be passed between activities restarts during a configuration change to restore user selections. The application is responsible for restoring its instance state.

Assume for example the user scrolled through a ListView with thousands of items and the activity is recreated. Loosing the position in the list is annoying for the user, hence the position should be restored.

The onSaveInstanceState() can be used to store this instance state as a Bundle . A Bundle can contain primitive data types, arrays, String and objects which are of the Parcelable or Serialisable type.

The persisted Bundle data is passed at restart of the activity to the onCreate() method and onRestoreInstanceState() as parameter.

If you override onSaveInstanceState() and onRestoreInstanceState() you should call the super implementation of it, because the default views of Android store their data via a call to View.onSaveInstanceState from the onSaveInstanceState() method of the activity. For example EditText stores its content via the default call of this method.

The onRestoreInstanceState() or the onCreate() methods can be used to recreate the instance scope of an activity if it is restarted.

Prefer using the onRestoreInstanceState() method for restoring the instance state. This approach separates the initial setup from restoring the state.

If the user interacts with an activity and presses the Back button or if the finish() method of an activity is called, the activity is removed from the current activity stack and recycled. In this case there is no instance state to save and the onSaveInstanceState() method is not called.

If the user interacts with an activity and presses the Home button, the activity instance state must be saved. The onSaveInstanceState() method is called. If the user restarts the application it will resume or restart the last running activity. If it restarts the activity it provides the bundle with the save data to the onRestoreInstanceState() and onCreate() methods.

The onSaveInstanceState() method is not called if the user presses the back button. Don’t use this approach to save the data which needs to get persisted.

Android Tools


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 (Take heap memory dump in hprof file )
  • network traffic tracking
  • incoming call and SMS spoofing
  • simulating network state, speed, and latency
  • location data spoofing

2) Eclipse Memory Analyzer

To analyze the heap memory dump in the hprof file.

3) Hierarchy Viewer

Hierarchy Viewer to view the time taken to draw the individual parts of the layout (it gives measure, layout and draw time of layouts).

We can use it to find the parts of layout which are taking long time to draw and which can be further optimized.
There is also a color code along side the time which tells us whether if time is optimal or not.

Android Concepts

1) In android for background tasks

If short –> can use AsyncTask
If long –> Use a Service and propagate the changes to the main activity with a BroadcastReceiver

2) Favor static inner classes over nonstatic. Each instance of a nonstatic inner class will have an extraneous reference to its outer Activity instance. Storing this reference can result in the Activity being retained when it would otherwise be eligible for garbage collection. If your static inner class requires a reference to the underlying Activity in order to function properly, make sure you wrap the object in a WeakReference to ensure that you don’t accidentally leak the Activity.


Android To Read

1) Activity, Memory Leaks, Long running task

Retained Fragments to handle background tasks.
The difference between static and non-static inner classes is subtle, but is something every Android developer should understand. What’s the bottom line? Avoid using non-static inner classes, anonymous 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.




2) Handling Configuration Changes with Fragments

Calling Fragment#setRetainInstance(true) allows us to bypass this destroy-and-recreate cycle, signaling the system to retain the current instance of the fragment when the activity is recreated.

It’s not a good practice to use configChanges to prevent destruction and recreation of activity on configuration change.


To handle configuration changes you can implement function onConfigurationChanged().

When you declare your activity to handle a configuration change, you are responsible for resetting any elements for which you provide alternatives. If you declare your activity to handle the orientation change and have images that should change between landscape and portrait, you must re-assign each resource to each element during onConfigurationChanged().

3) Infinite View Pager

4) Caching – How to use DiskLruCache


Interview Standard Algorithm Questions

1) Checking for valid parenthesis and longest valid parenthesis?

Longest Valid Parenthesis