Java How to Program, 10/e Multiple Choice Test Bank 5 of 11
d. None of the above.
23.4.1 Immutable Data
23.4.1 Q1: Which of the following statements is false?
a. Thread synchronization is necessary only for shared mutable data, i.e., data that may change during its lifetime.
b. With shared immutable data that will not change, it’s not possible for a thread to see old or incorrect values as a
result of another thread’s manipulation of that data.
c. When you share immutable data across threads, declare the corresponding data fields final to indicate that the
values of the variables will not change after they’re initialized. This prevents accidental modification of the shared
data, which could compromise thread safety.
d. Labeling object references as final indicates that the referenced object is immutable.
23.4.2 Monitors
23.4.2 Q1: Which of the following statements is false?
a. A common way to perform synchronization is to use Java’s built-in monitors.
b. Every object has a monitor and a monitor lock (or intrinsic lock).
c. A monitor ensures that its object’s monitor lock is held by a maximum of two threads at a time.
d. Monitors and monitor locks can be used to enforce mutual exclusion.
23.4.2 Q2: Which of the following statements is false?
a. If an operation requires the executing thread to hold a lock while the operation is performed, a thread must relase
the lock before proceeding with the operation. Other threads attempting to perform an operation that requires the
same lock will be blocked until the first thread releases the lock, at which point the blocked threads may attempt to
acquire the lock and proceed with the operation.
b. To specify that a thread must hold a monitor lock to execute a block of code, the code should be placed in a
synchronized statement.
c. Code in a synchronized statement is said to be guarded by the monitor lock; a thread must acquire the lock to
execute the guarded statements.
d. The monitor allows only one thread at a time to execute statements within synchronized statements that lock on
the same object, as only one thread at a time can hold the monitor lock. The synchronized statements are declared
using the synchronized keyword.
23.4.2 Q3: Which of the following statements is false?
a. If several synchronized statements in different threads are trying to execute on an object at the same time, only
one of them may be active on the object—all the other threads attempting to enter a synchronized statement on the
same object are placed in the blocked state.
b. When a synchronized statement finishes executing, the object’s monitor lock is released and one of the blocked
threads attempting to enter a synchronized statement can be allowed to acquire the lock to proceed.
c. Java also allows synchronized methods. Before executing, a synchronized instance method must acquire the lock
on the object that’s used to call the method.