Core Java - Interview Questions and Answers for 'Thread' | Search Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com

Search Interview Questions


 More than 3000 questions in repository.
 There are more than 900 unanswered questions.
Click here and help us by providing the answer.
 Have a video suggestion.
Click Correct / Improve and please let us know.
Label / Company      Label / Company / Text

   



Interview Questions and Answers - Order By Newest

   
 Q31. What state does a thread enter when it terminates its processing?

a. Wait
b. Ready
c. Dead
d. Running
Ans. Dead

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   threads   multithreading   thread states


 Q32. Invoking start twice on same thread leads to ..

a. ClassCastException
b. NullPointerException
c. InterruptedException
d. IllegalStateException
Ans. IllegalStateException

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     java   exception   threads


 Q33. Print natural numbers sequentially with two threads.Core Java
Ans. http://stackoverflow.com/questions/18799591/print-natural-sequence-with-help-of-2-threads1-is-printing-even-and-2nd-is-pri

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     print natural numbers with two threads   multithreading   multi threading   threads   code   coding   makemytrip.com


 Q34. What is the use of Synchronized block ?Core Java
Ans. The goal of a synchronised block is to achieve mutual exclusion i.e at one time, the segment of the code should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     synchronized block   synchronized   synchronization   multithreading   threads   mutual exclusion   concurrency     Asked in 4 Companies      intermediate        frequent

Try 1 Question(s) Test


 Q35. Difference between Yielding and Sleeping ?
Ans. When a task invokes its yield method, it returns to the ready state. When a task invokes its sleep method, it returns to the waiting state.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads  yielding  sleeping  thread states


 Q36. In case of a synchronized method, Does the method only gets locked or the whole object ?Core Java
Ans. Just the method gets locked.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     synchronization  multithreading     Asked in 1 Companies


 Q37. What are the disadvantages of multithreading ?Core Java
Ans. 1. Switching Overheads - Even though multi threading aims at improving performance by reducing the wait time and hence improving overall throughput, there is a cost of switching resources between threads and sometime this cost can surpass the benefits if there isnt much wait for IO or external communication.

2. Debugging is hard with multi threaded code.

3. Deadlock - Execution of multi threaded code many a times lead to deadlock due to shared resources.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads     Asked in 4 Companies


 Q38. Can there be a Thread without a Process ?Core Java
Ans. No.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Thread  Process     Asked in 1 Companies      Basic


Very frequently asked.
 Q39. What is the use of synchronized keyword ?Core Java
Ans. Synchronize is used to achieve mutual exclusion i.e at one time, the segment of the code, method that has been declared synchronized should be executed by single thread only and hence the lock needs to be retrieved before executing the segment and then released.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads  multithreading  synchronized     Asked in 5 Companies      basic        frequent


 Q40. Can we start the same thread twice ?Core Java
Ans. No we cannot start the same thread twice. Each thread has a lifecycle.

But Yes, we can run the same code in parallel using different threads.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     thread  multithreading


 Q41. Who invokes a thread's run method ?Core Java
Ans. After a thread is started using a call to start method, JVM invokes the thread’s run method when the thread is initially executed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads  thread run method     Asked in 1 Companies


 Q42. How do threads share information ?Core Java
Ans. Through common memory area.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads     Asked in 1 Companies


 Q43. Can you give real time examples of thread or where thread are actually used ?Core Java
Ans. Games are very good examples of threading.You can use multiple objects in games like cars, motor bikes, animals, people etc. All these objects are nothing but just threads that run your game application.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads  multithreading     Asked in 1 Companies


 Q44. Can we synchronize the run() method in Java?Core Java
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     synchronization  multithreading  run method


 Q45. If a Map needs to be accessed simultaneously by multiple threads, which collection class should be used ? Core Java
Ans. ConcurrentHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     map  concurrenthashmap  multithreading


 Q46. Why do we use Thread Class as well as Runnable Interface for creating threads in Java ?Core Java
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Threads  Thread class  runnable interface


 Q47. Write code to read data from a file and write it to a file with 2 threads using the thread name contentCore Java
 This question was recently asked at 'verifone'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads  file  file handling     Asked in 1 Companies


 Q48. Write Java code that would cause deadlock ?Core Java
Ans. https://howtodoinjava.com/java/multi-threading/writing-a-deadlock-and-resolving-in-java/

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads  deadlock     Asked in 1 Companies      intermediate


 Q49. How would you design a Thread Pool ?Design
Ans. By using Executor Framework , we can create Thread pool

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Thread Pool     Asked in 1 Companies      expert


 Q50. How executor service is better than using primitive Threading mechanism using Thread class or runnable Interface ?Core Java
 This question was recently asked at 'Symantec'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads     Asked in 1 Companies


 Q51. Suppose we make an api call using multithreading, How would you design to make sure that we have received response for all threads before exiting the main thread ?Design
Ans. We can use HashMap for tracking response status for all threads. We can wait every n second by using Thread.sleep and exit the main thread only once we have received response for all threads.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     threads  multithreading     Asked in 1 Companies      basic        frequent


 Q52. How does multi threading improve performance ?Core Java
Ans. Every process in it's timeline require different resources. Utilization of resources can be optimized when they are shared among different processes or threads. When one thread is sleeping waiting for a peripheral to complete (e.g. a disk write, or a key press from the keyboard), other threads can continue using processor time and hence would lead to better usage of resources.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads      basic        frequent


 Q53. What is Time slicing with respect to Threads ?Operating System
Ans. Time Slicing is the process to divide the available CPU time to the available runnable threads.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Time Slicing  threads  thread scheduling   thread scheduler


 Q54. What is Thread scheduler ?Operating System
Ans. Thread Scheduler is the Operating System service that allocates the CPU time to the available runnable threads.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Thread scheduler  threads


 Q55. What are the different ways to avoid multi Threading related problems in Java ?Core Java
Ans. Synchronization,
Concurrent classes,
Volatile keyword,
Implementing concurrent Lock interface,
Immutable classes

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     multithreading  threads  ways to avoid thread related problems  synchronization  volatile  concurrent collections      Intermediate


 Q56. What is context switching wrt Threads in Java?Core Java
Ans. Context Switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Threads  multithreading  context switching


 Q57. Tell something about Sleep method.Core Java
 This question was recently asked at 'IBM'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     Thread  sleep  multithreading     Asked in 1 Companies      Basic


 Q58. Tell me about thread pool?Design
Ans. Java provides its own implementations of the thread pool pattern, through objects called executors. These can be used through executor interfaces or directly through thread pool implementations which does allow for finer-grained control. The java.util.concurrent package contains the following interfaces:

Executor : a simple interface for executing tasks.ExecutorService a more complex interface which contains additional methods for managing the tasks and the executor itself.

ScheduledExecutorService: extends ExecutorService with methods for scheduling the execution of a task.Alongside these interfaces, the package also provides the Executors helper class for obtaining executor instances, as well as implementations for these interfaces.

Generally, a Java thread pool is composed of:

The pool of worker threads, responsible for managing the threads.
A thread factory that is responsible for creating new threads.
A queue of tasks waiting to be executed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     thread pool     Asked in 1 Companies


 Q59. What resources are shared between threads ?Threads
 This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like         Discuss         Correct / Improve     


 Q60. notify(), notifyAll(), wait() are found in which classCore Java
a. java.lang.Object
b. java.lang.Thread
c. java.lang.Runnable
d. java.lang.Implement

Ans.a. java.lang.Object

previous 30   

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked: