Answer: B Multi threading and multi tasking
Description: Android supports both multi threading and multi tasking.
Answer: A i
Description: doInBackGround() is the only mandatory function that has to be implemented in AsyncTask class.
Answer: B Use a volatile boolean flag, based on which return from the run() method of the Thread.
Description:
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.
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:
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.
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.
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.
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.
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.
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.
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.
Answer: D onPreExecute, onProgressUpdate, onPostExecute - runs in Main thread, doInBackGround - runs in background Thread.
Description:
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)