Section 17: Synchronized Methods
Q1. What is the primary purpose of synchronized methods in Java?
• A) To allow multiple threads to access a method simultaneously
• B) To prevent multiple threads from accessing a method at the same time
• C) To improve the speed of thread execution
• D) To manage memory allocation
Q2. Which of the following is true for a synchronized method in Java?
• A) It locks the object for any other synchronized methods
• B) It allows only one thread per JVM to access it
• C) It can be accessed by multiple threads simultaneously
• D) It locks the method only for the calling thread
Q3. What happens if a non-synchronized method is called by multiple threads?
• A) Only one thread can access it at a time
• B) Multiple threads can access it simultaneously
• C) The method becomes synchronized automatically
• D) It throws a runtime exception
Q4. In a synchronized method, what does the 'this' keyword refer to?
• A) The method itself
• B) The class of the method
• C) The current instance of the class
• D) The main thread
Q5. Which statement about the synchronized keyword is correct?
• A) It can be applied to static methods
• B) It can only be applied to instance methods
• C) It causes compilation errors when used with methods
• D) It can only be applied to constructors
Q6. What will happen if two threads try to access synchronized methods in different
instances of a class?
• A) Both threads will be blocked
• B) Only one thread will be able to execute
• C) Both threads can execute simultaneously
• D) It causes a deadlock
Q7. Which of the following statements about synchronized blocks is correct?
• A) They lock a specific part of the method, not the whole method
• B) They can only be used in static methods
• C) They do not provide any thread-safety
• D) They prevent method-level synchronization
Q8. How is a static synchronized method different from a non-static synchronized method?
• A) It locks the class object rather than the instance
• B) It allows concurrent access
• C) It cannot be accessed by any thread
• D) It requires no memory lock
Q9. Which of the following is true about synchronized blocks?
• A) They must contain the entire method
• B) They provide fine-grained control over locking
• C) They prevent method-level access
• D) They are less efficient than synchronized methods
Q10. When a thread enters a synchronized method, which of the following happens?
• A) It locks the object so other threads cannot enter any synchronized methods of that
object
• B) It only locks the current method
• C) It allows other threads to enter synchronized blocks
• D) It throws a runtime exception if other threads are waiting