1

Android supports

  • MultiThreading
  • Multi threading and multi tasking
  • single threading with multi tasking
  • single task with multi threading

Answer: B Multi threading and multi tasking
Description: Android supports both multi threading and multi tasking.

2

What are the mandatory functions to be implemented in AsyncTask class?

  • i. only doInBackground().
  • ii. onPreExecute()
  • iii. onPostExecute()
  • iv. onProgressUpdate.
  • i
  • i & ii
  • i, ii, & iii
  • i, ii, iii, & iv

Answer: A i
Description: doInBackGround() is the only mandatory function that has to be implemented in AsyncTask class.

3

How to stop a thread in android?

  • Use stop() method of the Thread class.
  • Use a volatile boolean flag, based on which return from the run() method of the Thread.
  • use exit() method in run() method to exit and stop the thread.
  • none

Answer: B Use a volatile boolean flag, based on which return from the run() method of the Thread.

Description:

4

What is a thread?Is service with Thread possible?

  • A thread, in the context of Java, is the path followed when executing a program. All Java programs have at least one thread, known as the main thread.
    Service with thread is not possible.
  • Thread is a dispatch able unit to the CPU.All Java programs have at least one thread, known as the main thread.
    Service with thread is not possible.
  • Thread is a dispatch able unit to the CPU.All Java programs have at least one thread, known as the main thread.
    Yes Service with thread is possible. But by default service will run in main thread.
  • None are correct

Answer: B Thread is a dispatch able unit to the CPU.All Java programs have at least one thread, known as the main thread.
Yes Service with thread is possible. But by default service will run in main thread.
Description: Yes Service with thread is possible. But by default service will run in main thread.

5

Define AsyncTask?How many methods are there and how parameters work in it?

  • The AsyncTask class provides a thread that will always run in background. It also supports reporting progress of the running tasks.
    There are four methods in Async Task:
    1. onCreateTask()
    2. doResumeTask()
    3. onStopTask()
    4. onDestroy()

    The three types of parameters in AsyncTask:
    1. Params: the type of the parameters sent to the task upon execution.
    2. Progress: the type of the progress units published during the background computation.
    3. Result: the type of the result of the background computation.
  • The AsyncTask class is a way to achieve multithreading in android application, where multiple tasks can be run at a time. It synchronizes with the main thread. It also supports reporting progress of the running tasks.

    There are four methods in Async Task:
    1. onPreExecute()
    2. doInBackground()
    3. onUpdateProgress()
    4. onPostExecute()
    The three types of parameters in AsyncTask:
    1. Params: the type of the parameters sent to the task upon execution.
    2. Progress: the type of the progress units published during the background computation.
    3. Result: the type of the result of the background computation.
  • AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.

    There are four methods in Async Task:
    1. onCreate()
    2. onResume()
    3. onStop()
    4. onDestroy()
    The three types of parameters in AsyncTask:

    1. Params: the type of the parameters sent to the task upon execution.
    2. Progress: the type of the progress units published during the background computation.
    3. Result: the type of the result of the background computation.
  • None are correct

Answer: B The AsyncTask class is a way to achieve multithreading in android application, where multiple tasks can be run at a time. It synchronizes with the main thread. It also supports reporting progress of the running tasks.

There are four methods in Async Task:
1. onPreExecute()
2. doInBackground()
3. onUpdateProgress()
4. onPostExecute()
The three types of parameters in AsyncTask:
1. Params: the type of the parameters sent to the task upon execution.
2. Progress: the type of the progress units published during the background computation.
3. Result: the type of the result of the background computation.
Description:

6

How to kill a thread?

  • <theradobject>.stop();
  • Thread.stop();
  • return from run() function
  • <threadobject>.kill()

Answer: C return from run() function
Description: <threadObject>.stop() function is used to stop or kill a thread. But due to some issues with that function, stop method has been deprecated and removed. Now we can't use stop method to kill a thread. Thread will be killed automatically once we return from the run() function. In case of Handler threads it will be in dormant state unless some new message comes to its message queue.

7

Why android follows single threaded UI model?

  • Because other threads should not manipulate UI.
  • Because synchronization is costly compared to single threaded model.
  • Because synchronization is not possible on UI.
  • Because UI thread only can access UI.

Answer: B Because synchronization is costly compared to single threaded model.
Description: using synchronization also it is possible to manipulate UI from other threads. But Android doesn't follow that design, because it is very costly in terms of CPU time over head if we use synchronization. So all UI updates has to go through Main Thread (or UI thread) only.

8

If I create a thread in my Activity and close my activity, will that thread run in the back ground or will be killed automatically on activity close? If it keeps running in the back ground, when will it be killed?

  • that thread will run for ever, if there is no condition in run() method to return and close it.
  • that thread will killed immediately on activity closure by android.
  • either it may run for ever if there is no condition in run() method to return, or it may be killed by android in case of low memory scenario.
  • option 3 is right, and it is highly unpredictable how it behaves. this kind of design is wrong. if you want a thread in activity, make sure that either you kill before leaving activity, or put a condition to close it in run method, or better to start a service with thread.

Answer: D option 3 is right, and it is highly unpredictable how it behaves. this kind of design is wrong. if you want a thread in activity, make sure that either you kill before leaving activity, or put a condition to close it in run method, or better to start a service with thread.
Description: either it may run for ever if there is no condition in run() method to return, or it may be killed by android in case of low memory scenario. it is highly unpredictable how it behaves. this kind of design is wrong. if you want a thread in activity, make sure that either you kill before leaving activity, or put a condition to close it in run method, or better to start a service with thread.

9

Is it possible to start asynctask from background thread?

  • Yes we can start asynctask from any where.
  • it is not mandatory to start asynctask from worker thread, but it will not give any problem even if we do so.
  • it is mandatory that one has to call asynctask only from main thread, else it may crash at run time when we try to touch UI from onPreExecute [or] onProgressUpdate [or] onPostExecute functions.
  • no, we should not start from other threads, it gives compile time error.

Answer: C it is mandatory that one has to call asynctask only from main thread, else it may crash at run time when we try to touch UI from onPreExecute [or] onProgressUpdate [or] onPostExecute functions.
Description: it is mandatory that one has to call asynctask only from main thread, else it may crash at run time when we try to touch UI from onPreExecute [or] onProgressUpdate [or] onPostExecute functions.

10

what will happen if execute() function of asynctask is called more than once, for a give asynctask object?

  • it will create one more thread / asynctask object.
  • there is no effect on it because already it has created an asynctask on first execution.
  • we should not call execute more than once on one object, it will throw run time exception.
  • it gives compile time error if we try to do so.

Answer: C we should not call execute more than once on one object, it will throw run time exception.
Description: we should not call execute() function more than once on a given asynctask object, it will throw run time exception.

11

how many threads will be created with asynctask?

  • asynctask is used to do multi threaded programming, so it creates multiple threads.
  • it creates only single thread, no matter how many objects you created for asynctask class.
  • till donut - it is used to create single thread, from 1.6 to 2.3 - it is used to create multi threads, from 3.0 on wards - it is again used to create single thread.
  • it won't create any thread.

Answer: C till donut - it is used to create single thread, from 1.6 to 2.3 - it is used to create multi threads, from 3.0 on wards - it is again used to create single thread.
Description: till donut - it is used to create single thread, from 1.6 to 2.3 - it is used to create multi threads, from 3.0 on wards - it is again used to create single thread. If you want to create multiple threads with asynctask from 3.0 on wards, instead of using execute(), use executeOnExecutor(Executor e, variable). They have reverted back the basic functionality if asynctask.execute() to behave as a single threaded, because multi threded programming will lead to lot of complications in terms of synchronization.

12

How to create a service with single thread? should I use IntentService or AsyncTask?

  • Use IntentService, if you don't want to interact with UI.
  • Use AsyncTask if you want to interact with UI.
  • both option 1 and option 2 are correct.
  • none

Answer: C both option 1 and option 2 are correct.
Description: It actually depends on the requirement. Use IntentService, if you don't want to interact with UI. Use AsyncTask if you want to interact with UI from background thread. It doesn't mean that you can't touch UI from IntentService, but you have to either use post() or runOnUiThread() or Handler concepts, which are little bit complicated to understand for novice android developers who are not well versed with threads.

13

In which thread asynctask functions will execute?

  • all four functions executes in worker thread context.
  • all four functions executes in main thread context.
  • onPreExecute, onProgressUpdate, onPostExecute - runs in background thread, doInBackGround - runs in MainThread.
  • onPreExecute, onProgressUpdate, onPostExecute - runs in Main thread, doInBackGround - runs in background Thread.

Answer: D onPreExecute, onProgressUpdate, onPostExecute - runs in Main thread, doInBackGround - runs in background Thread.
Description:

14

Do all components of an android application run in same thread?

  • it is based on number of components in that application.
  • option 1 is true, because activity, service, receiver, and content provider will create a thread by default internally.
  • each application will have one process and one main thread created by system, by default.
  • process or application by default will not have any thread.

Answer: C each application will have one process and one main thread created by system, by default.
Description: each application will have one process and one main thread created by system, by default. So by default all the components of an android application runs in Main thread (UI thread)