Core Java - Interview Questions and Answers for 'Synchronization' | 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

   



Core Java - Interview Questions and Answers for 'Synchronization' - 24 question(s) found - Order By Newest

Advanced level question usually asked to senior developers , leads and architects.
 Q1. How does volatile affect code optimization by compiler?Core Java
Ans. Volatile is an instruction that the variables can be accessed by multiple threads and hence shouldn't be cached. As volatile variables are never cached and hence their retrieval cannot be optimized.

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

   Like         Discuss         Correct / Improve     java   java keywords   volatile   synchronization   compiler optimization   variable caching   architecture     Asked in 3 Companies      expert


 Q2. Can constructors be synchronized in Java ?Core Java
Ans. No. Java doesn't allow multi thread access to object constructors so synchronization is not even needed.

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

   Like         Discuss         Correct / Improve     synchronization   synchronize   constructor   java   multithreading   yes-no   advanced     Asked in 1 Companies      expert        rare


Frequently asked question for intermediate developers. Frequently asked in HCL Technologies and EPAM.
  Q3. What is Volatile keyword used for ?Core Java
Ans. Volatile is a declaration that a variable can be accessed by multiple threads and hence shouldnt be cached.

  Sample Code for volatile

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

   Like         Discuss         Correct / Improve     java   oops   synchronization   volatile   java keywords     Asked in 42 Companies      intermediate        frequent

Try 1 Question(s) Test


  Q4. Difference between Vector and ArrayList ?Core Java
Ans. Vectors are synchronized whereas Array lists are not.

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

   Like         Discuss         Correct / Improve     java   basic interview question   vector   arraylist   collections   synchronization   vector vs arraylist     Asked in 35 Companies      basic        frequent


 Q5. What is race condition ?Operating System
Ans. A source of possible errors in parallel programming, where one thread can cause an error in another thread by changing some aspect of the state of the program that the second thread is depending on (such as the value of variable).

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

   Like         Discuss         Correct / Improve     java.threads   multithreading   race condition   parallel programming   synchronization   ebay   concurrency     Asked in 10 Companies


 Q6. What is a Property class ?Core Java
Ans. The properties class is a subclass of Hashtable that can be read from or written to a stream.

  Sample Code for Load Properties using Property Class

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

   Like         Discuss         Correct / Improve     java   collections   hashtable   map   synchronization   synchronized


 Q7. What is the difference between these two approaches of creating singleton Class ?

//Double Checked Locking Code
public static Singleton createInstance() {
   if(singleton == null){
      synchronized(Singleton.class) {
         if(singleton == null) {
            singleton = new Singleton();
         }
      }
   }
   return singleton;
}

//Single checked locking code
public static Singleton createInstance() {
   synchronized(Singleton.class) {
      if(singleton == null) {
         singleton = new Singleton();
      }
   }
   return singleton;
}
Design
Ans. In First Case , Lock for the synchronized block will be received only if singleton == null whereas in second case every thread will acquire the lock before executing the code.
The problem of synchronization with singleton will only happen when the object has not be instantiated. Once instantiated , the check singleton == null will always generate true and the same object will be returned and hence no problem. First condition will make sure that synchronized access ( acquiring locks ) will only take place if the object has not been created so far.

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

   Like         Discuss         Correct / Improve     java   singleton   synchronization

Try 1 Question(s) Test


 Q8. What is meant by "Vector is synchronized and ArrayList isn't " ?Core Java
Ans. It means that only 1 thread can access have access to Vector at a time and no parallel access is allowed whereas Array List allows parallel access by multiple threads.

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

   Like         Discuss         Correct / Improve     vectors  arraylist  collections  list  synchronization  synchronized


 Q9. How can we ensure thread safety in static method ?Core Java
Ans. By using synchronized static method or synchronized block

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

   Like         Discuss         Correct / Improve     threads  synchronization  static methods     Asked in 2 Companies      intermediate


 Q10. What is suspend() method used for ?
Ans. suspend() method is used to suspend the execution of a thread for a period of time. We can then restart the thread by using resume() method.

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

   Like         Discuss         Correct / Improve     java   threads   multi threading   operating system   synchronization      basic        rare


Usually asked only to fresh graduates.
 Q11. Difference between suspend() and stop() ?
Ans. Suspend method is used to suspend thread which can be restarted by using resume() method. stop() is used to stop the thread, it cannot be restarted again.

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

   Like         Discuss         Correct / Improve     java   threads   multi threading   operating system   synchronization   suspend   stop      basic        rare


 Q12. When you will synchronize a piece of your code?
Ans. When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.

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

   Like         Discuss         Correct / Improve     java   threads   multi threading   synchronization   concurrency      basic        frequent


 Q13. 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


 Q14. Can a lock be acquired on a class? How is it different than the object level lock ?
Ans. Yes, a lock can be acquired on the class. Class level lock is applicable on the whole class and hence on all objects of the class whereas the object level lock is applicable on the respective object.

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

   Like         Discuss         Correct / Improve     synchronization  class level lock  object level lock


 Q15. 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


 Q16. What is an optimistic locking ?Database
Ans. It's the way for synchronization wherein we can store version information in the table , so that if the same entity is updated by two transactions, the last to commit changes is informed of the conflict, and does not override the other transaction's work.

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

   Like         Discuss         Correct / Improve     locking  optimistic locking  Synchronization


 Q17. Is synchronized a keyword , modifier, identifier? what is it ?Core Java
Ans. synchronized is a keyword and a modifier. The synchronized keyword is used to indicate that a method can be accessed exclusively by one thread at a time.

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

   Like         Discuss         Correct / Improve     synchronization  synchronized  modifier  identifier     Asked in 2 Companies


 Q18. What are the advantages and disadvantages of using lock classes from java.util.concurrent.locks vs. synchronized methods ?Core Java
Ans. http://www.devinline.com/2015/10/Lock-Vs-synchronized-in-java.html

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

   Like         Discuss         Correct / Improve     java.util.concurrent.locks  synchronization  synchronized methods


 Q19. Solve the producer and consumer problemOperating System
 This question was recently asked at 'Dell'.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     producer consumer problem  multi-process synchronization problem     Asked in 1 Companies


 Q20. Explain Java 8 concurrency mechanism.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     synchronization  concurrency  java 8  java8     Asked in 1 Companies


 Q21. 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


 Q22. Can you give a real life example of synchronizationCore Java
Ans. Intersection with traffic light

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

   Like         Discuss         Correct / Improve     synchronization


 Q23. 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


 Q24. Is HashMap Synchronized ?Core Java
Ans. No HashMap isn't synchronized. ConcurrentHashMap and Hashtable are synchronized.

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

   Like         Discuss         Correct / Improve     hashmap  synchronization



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: