Java Thread Safety & Synchronization Techniques: Intermediate Insights
Assess your understanding of thread safety and synchronization techniques in Java.
Questions in this quiz
12 questions on Java Thread Safety & Synchronization Techniques: Intermediate Insights. Work through them here, then start the quiz to answer them and see your score.
1.Which of the following is true about the synchronized keyword in Java?
- AIt can be used to synchronize a block of code or a method.
- BIt can only be applied to instance methods.
- CIt is used to make a method execute faster.
- DIt can only be used to synchronize static methods.
2.What is the primary purpose of the volatile keyword in Java?
- ATo prevent a class from being extended.
- BTo make a variable immutable.
- CTo ensure visibility of changes to variables across threads.
- DTo synchronize methods.
3.Consider the following code snippet: `synchronized(this) { /* code */ }`. What does this synchronization achieve?
- AIt locks the static methods of the class.
- BIt locks the current object instance.
- CIt locks the entire class.
- DIt locks the code block globally.
4.What is the main difference between synchronized methods and synchronized blocks?
- ASynchronized methods are faster than synchronized blocks.
- BSynchronized blocks allow more granular control over the lock.
- CSynchronized blocks can only synchronize instance variables.
- DSynchronized methods can only be static.
5.Which of the following is a potential drawback of using synchronized methods?
- AThey can cause deadlocks if not used carefully.
- BThey can lead to increased memory usage.
- CThey cannot be used in abstract classes.
- DThey make the code difficult to read.
6.What is the purpose of the java.util.concurrent package?
- ATo provide classes for file I/O operations.
- BTo handle exceptions in a multi-threaded environment.
- CTo offer a set of utilities for concurrent programming.
- DTo manage database connections.
7.Which of the following is NOT a class provided by java.util.concurrent?
- ACopyOnWriteArrayList
- BStringBuilder
- CThreadPoolExecutor
- DConcurrentHashMap
8.What is the main advantage of using a ReentrantLock over synchronized blocks?
- AReentrantLock can only be used with static methods.
- BReentrantLock provides more flexibility, such as timed lock waits.
- CReentrantLock is faster than synchronized blocks.
- DReentrantLock automatically prevents deadlocks.
9.Which synchronization technique allows a thread to wait until it is signaled by another thread?
- Avolatile
- Bwait/notify
- CThread.sleep()
- Dsynchronized
10.What is the role of the 'lock' in a synchronized block?
- AIt prevents the block from being executed.
- BIt ensures that only one thread can execute the block at a time.
- CIt allows multiple threads to execute the block simultaneously.
- DIt speeds up the execution of the block.
11.What happens if a thread holds a lock and another thread tries to acquire the same lock?
- AThe second thread will wait until the lock is released.
- BThe second thread will acquire the lock immediately.
- CThe second thread will throw an exception.
- DThe second thread will terminate.
12.Which of the following is a correct statement about deadlocks?
- ADeadlocks only occur in single-threaded applications.
- BDeadlocks occur when two or more threads are waiting for each other to release locks.
- CDeadlocks are easy to detect and resolve.
- DDeadlocks can be avoided by using more synchronized blocks.