Java MCQ – Home
Java Basic MCQ
➤ Java Hello World MCQ
➤ Find Java Keywords
➤ Java Identifier Quiz
➤ Java Data Types Quiz
➤ If-Else MCQ in Java-1
➤ If-Else MCQ in Java-2
Object class MCQ
➤ Object Class Quiz
➤ equals() Method Quiz
➤ Hashcode Value Quiz
➤ toString() Method Quiz
➤ Clone() Method Quiz
Multithreading MCQ
➤ Define a Thread-1
➤ Define a Thread-2
➤ Get/set ThreadName
➤ Thread State MCQ
➤ Thread Priority MCQ
➤ Yield(), join() & sleep()
➤ Synchronization MCQ
➤ Interthread Comms
➤ Deadlock, Daemon
Exception Handling
➤ Exception Handling-1
➤ Exception Handling-2
➤ Exception Handling-3
➤ Java try-catch MCQ-1
➤ Java try-catch MCQ-2
➤ Java try-catch MCQ-3
➤ Nested try-catch MCQ
➤ throw Keyword MCQ
➤ finally Block MCQ-1
➤ finally Block MCQ-2
➤ throws Keyword MCQ
Generics MCQ
➤ Java Generics Quiz-1
➤ Java Generics Quiz-2
➤ Java Generics Quiz-3
Collection Framework
➤ Collections Quiz-1
➤ Collections Quiz-2
➤ ArrayList MCQ-1
➤ ArrayList MCQ-2
➤ LinkedList MCQ
➤ Vector Stack MCQ
➤ Java Cursors MCQ
➤ Java TreeSet MCQ
➤ TreeSet & Comparator
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 BlocksSynchronized 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) MultipleA 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 theseQ4) 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 threadQ7) 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 timeSince 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 errorThe 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) OneAn 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!