Java Synchronization MCQ

Java Multithreading MCQ Part-7 | Java Synchronization MCQ | Synchronization contains lots of theoretical concepts therefore you can get more questions here:- Interview Question on Java Synchronization.

Q1) The synchronized keyword is applicable for whom?

a) Variables and Methods
b) Methods and Classes
c) Variables and Classes
d) Methods and Blocks

View Answer Answer:- d) Methods and Blocks

Synchronized is a modifier applicable only for methods, and blocks but not for the variables and classes.

Q2) A method can contain how many synchronized blocks?

a) One
b) Two
c) Ten
d) Multiple

View Answer Answer:- d) Multiple

A method can contain multiple synchronized blocks.

Q3) In which of the following areas the synchronization concept can be applied?

a) Ticket booking system
b) Web Application
c) Bank Application
d) All of these

View Answer Answer:- d) All of these

Q4) Synchronization concept is required when?

a) Multiple threads operating simultaneously on the same object.
b) Multiple threads are operating on multiple objects.
c) Both (a) and (b)
d) None of these

View Answer Answer:- a) Multiple threads operating simultaneously on the same object.

If multiple threads operate simultaneously on the same object then synchronization is required but if multiple threads are operating on multiple objects then synchronization is not required.

Q5) What is the valid syntax for synchronized blocks to get the lock of the current object?

a) synchronized(this)
b) synchronized(super)
c) synchronized(Test.java)
d) None of these

View Answer Answer:- a) synchronized(this)

Q6) At a time, an object can be locked by how many threads?

a) Multiple threads
b) Only one thread
c) Only by those threads which are created by the Main thread
d) Depends on the hashcode value of the object

View Answer Answer:- b) Only one thread

Q7) If two threads are trying to execute one synchronized method with two different objects, then their execution order is?

a) One by One
b) Both at time
c) Thread which gets lock first will execute first
d) None of these

View Answer Answer:- b) Both at time

Since they are using two different objects therefore the synchronization concept will not be applied.

Q8) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      int x = 10;
      synchronized(x) {
        // code
      }
   }
}

a) Compiled Successfully, but no output
b) Compile time error
c) NullPointerException
d) None of these

View Answer Answer:- b) Compile time error

The lock concept is applicable only for class types and object types but not for primitives. Hence we can’t pass primitive type arguments to synchronized block otherwise we will get compile time error:- unexpected type.

Q9) An object can be locked by how many threads at a time?

a) One
b) Two
c) Zero
d) Multiple

View Answer Answer:- a) One

An object can be locked by only one thread at a time.

Q10) How to declare a synchronized block to get the “Test” class lock?

a) synchronized(this)
b) synchronized(super)
c) synchronized(Test.java)
d) None of these

View Answer Answer:- c) synchronized(Test.java)

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *