Test Bank Answers Multithreading Ch.21 - Big Java Early Objects 5e Complete Test Bank by Cay S. Horstmann. DOCX document preview.

Test Bank Answers Multithreading Ch.21

Course Title: Big Java, Early Objects

Chapter Number: 21 Multithreading

Question type: Multiple Choice

1) Given a single CPU and four threads, how many of the threads can execute in parallel?

a) Only 1 can execute at a time.

b) 2 can execute in parallel.

c) 3 can execute in parallel.

d) All 4 can execute in parallel.

Title: Given a single CPU, how many threads can execute in parallel?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

2) Given a two-CPU machine and four threads, how many of the threads can execute in parallel?

a) Only 1 can execute at a time.

b) 2 can execute in parallel.

c) 3 can execute in parallel.

d) All 4 can execute in parallel.

Title: Given a two-CPU machine, how many threads can execute in parallel?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

3) The Runnable interface includes which method(s)?

I public void run(Runnable runnable)

II public void run()

III public void start()

a) I

b) II

c) I and II

d) II and III

Title: The Runnable interface includes which method(s)?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

4) Which method(s) are part of the Thread class?

I public void run(Runnable runnable)

II public void start(Runnable runnable)

III public void start()

a) I

b) II

c) III

d) II and III

Title: The Runnable interface includes which method(s)?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

5) Which of the following class declarations could run in a thread?

I public interface MyRunnable extends Runnable { . . . }

II public class MyRunnable extends Runnable { . . . }

III public class MyRunnable implements Runnable { . . . }

a) II

b) III

c) I and II

d) II and III

Title: Which of the following class declarations could run in a thread?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

6) Which of the following does not create an object that can run in a thread, assuming the following MyRunnable class declaration?

public class MyRunnable implements Runnable { . . . }

I Runnable runnable = new Runnable();

II Runnable runnable = new MyRunnable();

III MyRunnable runnable = new MyRunnable();

a) I

b) III

c) I and II

d) II and III

Title: Which of the following does not create an object that can run in a thread?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

7) In which method are the tasks that are performed by a thread coded?

a) start

b) main

c) run

d) init

Title: In which method are the tasks that are performed by a thread coded?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

8) Which method do you call to make a thread ineligible to run on the CPU for a set number of milliseconds?

a) start

b) wait

c) await

d) sleep

Title: Which method makes a thread ineligible to run for a number of milliseconds?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

9) Which exception must be caught or declared when calling the sleep method?

a) IOException

b) IllegalStateMonitorException

c) InterruptedException

d) SleepException

Title: Which exception must be caught or declared when calling sleep?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

10) Which constructor can be used to create a new thread associated with a Runnable object?

a) public Thread(Runnable r)

b) public Thread()

c) public Runnable(Thread t)

d) public Thread(String s)

Title: Which constructor can be used to create a new thread?

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

11) For threads of equal priority, which is guaranteed by the thread scheduler that is part of the Java Virtual Machine?

I All will get time on the CPU

II All will get exactly equal time on the CPU

III The order threads run in the CPU will always be the same

a) I

b) II

c) I and III

d) II and III

Title: Which is guaranteed by the thread scheduler part of the Java Virtual Machine?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

12) If you do not use the Runnable interface, then what is necessary to create a new thread?

I Implement the Threadable interface

II Extend the Thread class and add a run() method to it

III Add a run method to any class

a) I

b) II

c) III

d) II and III

Title: If you do not use the Runnable interface, what is needed to create a new thread?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

13) Suppose run1 and run2 are objects of the class MyRunnable, which implements the Runnable interface. What is the result of the following calls?

run1.run();

run2.run();

a) run1 and run2 execute as independent threads.

b) Syntax error

c) Only run1 executes as an independent thread.

d) run1 and run2 run sequentially, not as independent threads.

Title: Given two instances of MyRunnable, what is the result of the following calls?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

14) When a sleeping thread is interrupted, an InterruptedException is generated. Where do you catch that exception?

a) in the start() method.

b) in the sleep() method.

c) in the run() method.

d) in the interrupt() method.

Title: Where do you catch an InterruptedException?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

15) The Runnable interface has a single method called ____________.

a) run

b) start

c) sleep

d) wait

Title: The Runnable interface has a single method called _____.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

16) The _____________ interface is designed to encapsulate the concept of a sequence of statements that can run in parallel with other tasks, without equating it with the concept of a thread, a potentially expensive resource that is managed by the operating system.

a) Thread

b) Runnable

c) MyRunnable

d) Threadable

Title: The _____ interface encapsulates the concept of statements that can run in parallel…

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

17) The ________ method stops the current thread for a given number of milliseconds.

a) run

b) await

c) sleep

d) delay

Title: The ___ method stops the current thread for a given number of milliseconds.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

18) When a thread is interrupted, the most common response is to terminate the ____________ method.

a) run

b) thread

c) sleep

d) await

Title: When a thread is interrupted, terminate the ____ method.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

19) When a sleeping thread is interrupted, a(n) ____________________ is generated.

a) ThreadInterruptedException

b) ThreadException

c) ThreadSleepException

d) InterruptedException

Title: Interrupting a sleeping thread generates a ________.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

20) To start a thread, you should first construct an object from a class that implements the ____________ interface.

a) Thread

b) Threadable

c) Run

d) Runnable

Title: To start a thread, construct an object from a class that implements the ____ interface.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

21) Insert the statement that would start the following thread.

Thread firstThread = new Thread(myRunnable);

____________________

a) firstThread.run();

b) firstThread.start();

c) run();

d) start();

Title: Insert the statement that starts this thread

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

22) Each thread runs for a short amount of time, called a ____________________.

a) thread allotment

b) thread time slot

c) time slice

d) run time slice

Title: Each thread runs for a short amount of time, called a _____.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

23) A(n) ____ uses a small number of threads to execute a large number of runnables.

a) embedded system

b) condition object

c) thread pool

d) race condition

Title: A ____ uses a small number of threads to execute a large number of runnables.

Difficulty: Easy

Section Reference 1: 21.1 Running Threads

Section Reference 2: Special Topic 21.1

24) Which argument(s) present(s) the best case(s) for using the Runnable interface rather than extending the Thread class?

I Thread sub-classes are harder to write

II Runnable objects can be passed into thread pools

III Less time is wasted in creating and destroying thread objects

a) I

b) II

c) II and III

d) I and II

Title: Why use the Runnable interface rather than extending the Thread class?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

25) Which argument(s) present(s) the best case(s) for extending the Thread class rather than using the Runnable interface in conjunction with a thread pool?

I Thread sub-classes will all execute independently

II Runnable objects will waste more system resources

III Thread sub-classes can execute faster on a single CPU than Runnable objects

a) I

b) II

c) III

d) II and III

Title: Why extend the Thread class rather than using Runnable interface with a thread pool?

Difficulty: Hard

Section Reference 1: 21.1 Running Threads

26) Which of the following definitely indicates that a thread has terminated?

I The run method has completed

II The method Thread.interrupted returns true

III The run method catches an InterruptedException

a) I

b) II

c) I and II

d) II and III

Title: Which of the following definitely indicates that a thread has terminated?

Difficulty: Medium

Section Reference 1: 21.2 Terminating Threads

27) Which of the following definitely indicates that a thread has been interrupted by another thread?

I The run method has completed

II The method Thread.interrupted returns true

III The run method catches an InterruptedException

a) I

b) II

c) I and II

d) II and III

Title: Which of the following indicates that a thread has been interrupted?

Difficulty: Medium

Section Reference 1: 21.2 Terminating Threads

28) What course of action should be followed when a thread has been interrupted?

a) the thread should terminate

b) the thread should do what a programmer thinks is appropriate

c) the thread should go to sleep

d) the thread should call the Thread.interrupted method

Title: What course of action should be followed when a thread has been interrupted?

Difficulty: Medium

Section Reference 1: 21.2 Terminating Threads

29) What should be done to get the attention of a thread?

a) call the thread’s stop method

b) call the thread’s run method

c) call the thread’s interrupt method

d) call the thread’s sleep method

Title: What should be done to get the attention of a thread?

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

30) What is likely to be true when thread one is downloading a 1MB file while thread two is downloading another 1MB file?

a) The files download twice as fast as they would if one thread downloaded both files.

b) The files download half as fast as they would if one thread downloaded both files.

c) When thread one is half done, thread two is about half done.

d) The faster thread will interrupt the other thread.

Title: What is likely to be true when two threads are downloading 1MB files?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

31) Suppose thread one is downloading a 800KB file while another thread is processing the same file on a single CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. Approximately how does the time to complete the entire job compare to having a single thread do the work?

a) The threaded way will be twice as fast.

b) The threaded way will be twice as slow.

c) The time depends heavily on the thread scheduler.

d) The ways will take about the same time.

Title: How does the time to complete a job compare to having a single thread do the work?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

32) Suppose thread one is downloading a 800KB file while another thread is processing the same file on a single CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. How many total time slices will it take to see the first 10KB of the processed file?

a) one

b) two

c) eight

d) ten

Title: How many total time slices will it take to see the first increment of the processed file?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

33) Suppose thread one is downloading a 800KB file while another thread is processing the same file on a single CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. How many total time slices will it take to process half the file?

a) thirty

b) forty

c) eighty

d) hundred

Title: How many total time slices will it take to process half the file?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

34) Suppose thread one is downloading a large file while another thread is processing the same file on a single CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. What is the main benefit of using two threads rather than using a single thread to do both parts of the job?

a) accuracy of the results

b) speed to compete entire job

c) speed of availability of partial results

d) less memory is used

Title: What is the main benefit of using two threads instead of one to do both parts of the job?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

35) Suppose thread one is downloading a 800KB file while another thread is processing the same file on a multi-CPU machine. Suppose further that one time slice allows the first thread to download about 10KB and that the second thread can process 10KB of the file in one time slice. What is the minimum number of CPUs that will allow the job to be completed in roughly half the time of a single-CPU machine?

a) 1

b) 2

c) 3

d) 4

Title: What is the minimum number of CPUs that will allow the job to be completed in roughly half the time?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

36) The sleep method is terminated with a(n) __________ whenever a sleeping thread is interrupted.

a) InterruptedException

b) SleepException

c) lock

d) SignalException

Title: The sleep method is terminated with a _____ when a sleeping thread is interrupted.

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

37) In the initial release of the Java library, the Thread class had a stop method to terminate a thread. However, that method is now _______________

a) forbidden

b) not recommended

c) unwise

d) deprecated

Title: The Thread.stop method is now ______.

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

38) The ___________ method does not actually cause threads to terminate; it merely sets a Boolean field in the thread data structure.

a) stop

b) await

c) sleep

d) interrupt

Title: The ___ method does not terminate threads; it sets a boolean field.

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

39) The ____________ occurs when a thread that is not running is interrupted.

a) InterruptException

b) InterruptedException

c) ThreadException

d) ThreadTerminatedException

Title: The _____ occurs when a thread not running is interrupted.

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

40) ____ occur(s) if the effect of multiple threads on shared data depends on the order in which the threads are scheduled.

a) Pooling

b) Interrupted exceptions

c) Deadlocks

d) Race conditions

Title: ____ occur if the effect on shared data depends on the order in which the threads are scheduled.

Difficulty: Easy

Section Reference 1: 21.3 Race Conditions

41) Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData eight times with values 1...8 respectively, sleeping for 30 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 30 milliseconds between calls. What is one reason why thread two may not receive 7 and 8 on its two final calls to getSharedData?

public class SharedData

{

private int value;

public void setSharedData(int n)

{

value = n;

}

public int getSharedData()

{

return value;

}

}

a) Thread one produced two 8’s.

b) Thread two is getting longer time slices.

c) Thread one is getting longer time slices.

d) The race condition.

Title: What is one reason a thread does not receive 7 and 8 on its two final calls to getSharedData?

Difficulty: Medium

Section Reference 1: 21.3 Race Conditions

42) Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData eight times with values 1...8 respectively, sleeping for 30 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 30 milliseconds between calls. Suppose thread two receives values 1, 2, 3, 4, 5, 6, 7, 8 respectively on its calls. Should we expect the same values for each program run?

public class SharedData

{

private int value;

public void setSharedData(int n)

{

value = n;

}

public int getSharedData()

{

return value;

}

}

a) Yes, because the sleep times are equal in between calls.

b) Yes, because that’s the same order used by thread one to set the values.

c) No, because thread one may not set the data as 1,2,3,4,5,6,7,8 respectively.

d) No, because a race condition may result in a different order.

Title: Should we expect the same values for each program run?

Difficulty: Medium

Section Reference 1: 21.3 Race Conditions

43) Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData eight times with values 1...8 respectively, sleeping for 30 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 30 milliseconds between calls. Which of the following orders of values is not possible for thread two to receive?

public class SharedData

{

private int value;

public void setSharedData(int n)

{

value = n;

}

public int getSharedData()

{

return value;

}

}

a) 1,2,3,4,5,6,7,8

b) 1,2,2,4,5,6,7,8

c) 1,2,3,4,5,6,8,8

d) 1,2,3,4,5,6,8,7

Title: Which of the following orders of values is not possible for thread two to receive?

Difficulty: Medium

Section Reference 1: 21.3 Race Conditions

44) Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData eight times with values 1...8 respectively, sleeping for 100 milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for 100 milliseconds between calls. Which of the following could be the last two values received by thread two?

public class SharedData

{

private int value;

public void setSharedData(int n)

{

value = n;

}

public int getSharedData()

{

return value;

}

}

a) 9, 10

b) 10, 9

c) 1, 2

d) 8, 7

Title: Which of the following could be the last two values received by thread two?

Difficulty: Hard

Section Reference 1: 21.3 Race Conditions

45) Examine the SharedData class shown below. Suppose two threads are created so that each has access to the same SharedData object. Thread one calls setSharedData ten times with values 1...10 respectively, sleeping for a random number of milliseconds between calls. Thread two calls getSharedData eight times, also sleeping for a random number of milliseconds between calls. Which of the following could be the last two values received by thread two?

public class SharedData

{

private int value;

public void setSharedData(int n)

{

value = n;

}

public int getSharedData()

{

return value;

}

}

a) 8, 10

b) 10, 9

c) 10, 8

d) 8, 7

Title: Which of the following could be the last two values received by thread two?

Difficulty: Medium

Section Reference 1: 21.3 Race Conditions

46) Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods as shown below. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Suppose a race condition occurs, and the race is finished first by thread one. What would you expect balance to be after all thread calls?

public void deposit(int dollars)

{

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

}

public void withdraw(int dollars)

{

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

}

a) $10

b) $20

c) $0

d) a negative amount

Title: What would you expect balance to be after all thread calls?

Difficulty: Hard

Section Reference 1: 21.3 Race Conditions

47) Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods as shown below. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. If the balance after all thread calls is 0, which statement is definitely true?

public void deposit(int dollars)

{

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

}

public void withdraw(int dollars)

{

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

}

a) The calls were interleaved: thread one, thread two, thread one, thread two, …

b) The first call was to the deposit method.

c) The last call was to the withdraw method.

d) Each individual call to the deposit and withdraw methods ran to completion.

Title: If the balance after all thread calls is 0, which statement is definitely true?

Difficulty: Hard

Section Reference 1: 21.3 Race Conditions

48) Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods and has a ReentrantLock named myLock, as shown below. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Which statement regarding the balance after all thread calls is definitely true?

public void deposit(int dollars)

{

myLock.lock()

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

myLock.unlock()

}

public void withdraw(int dollars)

{

myLock.lock()

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

myLock.unlock()

}

a) The balance could be zero or positive.

b) The balance is zero.

c) The balance could be zero or negative.

d) The balance is positive.

Title: Which statement regarding the balance after all thread calls is definitely true?

Difficulty: Medium

Section Reference 1: 21.4 Synchronizing Object Access

49) Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides deposit and withdraw methods and has a ReentrantLock named myLock, as shown below. Note that only the deposit method uses the lock. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Which statement regarding the balance after all thread calls is definitely true?

public void deposit(int dollars)

{

myLock.lock()

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

myLock.unlock()

}

public void withdraw(int dollars)

{

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

}

a) The balance could be zero or positive.

b) The balance is zero.

c) The balance could be zero or negative.

d) The balance is positive.

Title: Which statement regarding the balance after all thread calls is definitely true?

Difficulty: Hard

Section Reference 1: 21.4 Synchronizing Object Access

50) A(n) ____ object is used to control the threads that want to manipulate a shared resource.

a) condition

b) lock

c) interrupt

d) runnable

Title: What object is used to control threads that share data?

Difficulty: Easy

Section Reference 1: 21.4 Synchronizing Object Access

51) Consider the addFirst method of the LinkedList class in Chapter 16:

/**

Adds an element to the front of the linked list.

@param element the element to add

*/

public void addFirst(Object element)

{

Node newNode = new Node();

newNode.data = element;

newNode.next = first;

first = newNode;

}

Three implementations have been proposed to make the addFirst method thread safe where listLock is a variable of type ReentrantLock. Which of them will work?

I.

listLock.lock();

try

{

Node newNode = new Node();

newNode.data = element;

newNode.next = first;

}

finally

{

listLock.unlock();

}

first = newNode;

II.

Node newNode = new Node();

newNode.data = element;

newNode.next = first;

listLock.lock();

try

{

first = newNode;

}

finally

{

listLock.unlock();

}

III.

listLock.lock();

try

{

Node newNode = new Node();

newNode.data = element;

newNode.next = first;

first = newNode;

}

finally

{

listLock.unlock();

}

a) None of them

b) III only

c) II and III only

d) All of them

Title: Which of these makes LinkedList.addFirst thread safe?

Difficulty: Hard

Section Reference 1: 21.4 Synchronizing Object Access

52) If a thread sleeps after acquiring a _________, it blocks all other threads that want to acquire it.

a) condition

b) signal

c) lock

d) thread

Title: If a thread sleeps after acquiring a _____, it blocks all other threads that want to acquire it.

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

53) The ________ method is called by a thread that has just changed the state of some shared data in a way that may benefit waiting threads.

a) run

b) lock

c) unlock

d) signalAll

Title: ______ method is called by a thread that has changed the state of some shared data …

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

54) The thread that calls signalAll must own the lock that belongs to the condition object on which signalAll is called. Otherwise, a(n) _______ ________ is thrown.

a) InterruptException

b) IllegalMonitorStateException

c) ThreadException

d) ThreadTerminatedException

Title: What exception is thrown if a thread calls signalAll without a lock?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

55) The ____ method is useful only if you know that a waiting thread can actually proceed.

a) run

b) signal

c) await

d) interrupted

Title: Which method is useful only when you know a waiting thread can proceed?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

56) ____ allow a thread to temporarily release a lock, so that another thread can proceed, and to regain the lock at a later time.

a) Condition objects

b) Embedded systems

c) Exceptions

d) Race conditions

Title: What allows a thread to temporarily release a lock?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

57) A waiting thread is blocked until another thread calls ____ on the condition object for which the thread is waiting.

a) await

b) signalAll

c) interrupt

d) lock

Title: A waiting thread is blocked until another thread calls ____ on the condition object ...

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

58) Which of the following statements is correct?

a) If a thread sleeps after acquiring a lock, it blocks all other threads that want to use the same lock.

b) When a thread calls await, it is simply deactivated in the same way as a thread that reaches the end of its time slice.

c) A thread pool is designed to encapsulate the concept of a sequence of statements that can run in parallel with other tasks, without equating it with the concept of a thread, a potentially expensive resource that is managed by the operating system.

d) Under no circumstances should you terminate a running thread.

Title: Which statement about threads and locks is correct?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

59) Consider the following change to the deposit method in Section 21.4:

public void deposit(double amount)

{

balanceChangeLock.lock();

try

{

double newBalance = balance + amount;

balance = newBalance;

}

finally

{

balanceChangeLock.unlock();

}

System.out.println("Depositing " + amount + ", new balance is " + balance);

}

What is the consequence of this change?

a) The bank account balances may no longer be correctly updated.

b) The printouts may become intermingled.

c) The printouts may no longer display the correct balances.

d) All of the above.

Title: What is the effect of changing the locking code in the deposit method?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

60) Calling the wait method in synchronized code is very similar to what action after locking with a ReentrantLock object?

a) Calling notify.

b) Locking a ReentrantLock object.

c) Calling await on a condition object.

d) Calling signalAll on a condition object.

Title: Calling the wait method in synchronized code is very similar to what action?

Difficulty: Hard

Section Reference 1: 21.5 Avoiding Deadlocks

61) Assume two threads share a BankAccount object with balance of zero (0), and that the BankAccount class provides synchronized deposit and withdraw methods. Thread one deposits $10 ten times and, concurrently, thread two withdraws $10 ten times. Which statement regarding the balance after all thread calls is definitely true?

public synchronized void deposit(int dollars)

{

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

}

public synchronized void withdraw(int dollars)

{

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

}

a) The balance could be zero or positive.

b) The balance is zero.

c) The balance could be zero or negative.

d) The balance is positive.

Title: Which statement regarding the balance after all thread calls is definitely true?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

62) What is the relationship between synchronized code and code that is locked using a ReentrantLock object?

a) synchronized code stops the race condition, locks do not.

b) synchronized is a single lock, but we may have many ReentrantLock objects.

c) The two are exactly the same.

d) The two concepts are not related.

Title: What is the relationship between synchronized code and code locked with a ReentrantLock?

Difficulty: Hard

Section Reference 1: 21.5 Avoiding Deadlocks

63) Class MyClass has a single ReentrantLock object, myLock. Suppose thread one calls myLock.lock() as it enters methodX and immediately completes its CPU time slice. What will happen as thread two calls methodY and attempts to call myLock.lock()?

a) Thread two will wait for the lock.

b) Thread two will acquire the lock.

c) Deadlock will occur.

d) Thread two causes the IllegalStateMonitorException.

Title: What will happen as thread two calls methodY and attempts to call myLock.lock()?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

64) Class MyClass has two ReentrantLock objects, myLock1 and myLock2. Suppose thread one acquires myLock1 as it enters methodX and immediately completes its CPU time slice. After thread two enters methodY, it acquires myLock2 and tries to acquire myLock1. When thread one resumes, it tries to acquire myLock2. What will happen next?

a) Thread two will acquire myLock1.

b) Thread one will acquire myLock2.

c) Deadlock will occur.

d) Thread two will release myLock2.

Title: When thread one resumes, it tries to acquire myLock2. What will happen next?

Difficulty: Hard

Section Reference 1: 21.5 Avoiding Deadlocks

65) Under what conditions are locks unnecessary for multi-threaded programs?

a) When the threads are all of the same class.

b) When the threads are different classes.

c) When the threads do not share data.

d) When the shared data only has a single method.

Title: Under what conditions are locks unnecessary for multi-threaded programs?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

66) Under what circumstances will a call to signalAll not release a blocked thread that has called await?

a) When the thread is sleeping.

b) When the thread called await on different condition object.

c) When two or more threads are waiting.

d) When the thread called await on the same condition object.

Title: When will a call to signalAll not release a blocked thread that has called await?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

67) Consider an old fashioned telephone booth that can be occupied by one person at a time. Suppose one person went in and dialed a part of her number, and had to leave the booth. A second person went in and dialed a part of his number, and before the number was fully dialed, a connection to some other phone was made. What Java threads analogy fits this scenario?

I The two people are the threads

II The shared data is the telephone

III The state of the object is corrupt

a) I

b) I and II

c) II and III

d) I, II and III

Title: What Java threads analogy fits this telephone booth scenario?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

68) Consider an old fashioned telephone booth that can be occupied by one person at a time. Suppose one person went in and dialed a part of her number, and had to leave the booth. A second person went in and dialed a part of his number, and before the number was fully dialed, a connection to some other phone was made. What Java threads analogy would prevent this undesirable scenario?

I Acquire the lock prior to entering the booth

II Dial a complete number when inside the booth

III Hang up the phone and release the lock upon exiting the booth

a) I

b) I and II

c) I and III

d) I, II and III

Title: What Java threads analogy would prevent this undesirable telephone booth scenario?

Difficulty: Easy

Section Reference 1: 21.5 Avoiding Deadlocks

69) Which phrase best describes the purpose of a lock used in an object in one or more of its methods?

a) increased sharing

b) mutual exclusion

c) speed increase

d) privacy protection

Title: Which phrase best describes the purpose of a lock used in an object's method?

Difficulty: Easy

Section Reference 1: 21.4 Synchronizing Object Access

70) “Livelock” occurs when one thread runs continuously, while another thread never does. Which of the following is sufficient to cause livelock?

I Thread one is in an infinite loop

II Thread one possesses a lock and does not unlock it, but thread two requires the lock

III Thread one requires a lock, but thread two possesses the lock

a) I

b) II

c) I and II

d) I and III

Title: Which of the following is sufficient to cause livelock?

Difficulty: Medium

Section Reference 1: 21.4 Synchronizing Object Access

71) Stale data occurs in multi-CPU machines when one thread modifies shared data and a second thread accesses that data later, but sees the data value before the change took place. What is required to guarantee that the second thread sees the updated data, not stale data?

a) The second thread should not try to acquire the lock.

b) The first thread should not try to acquire the lock.

c) Neither thread should use locks.

d) The first thread should release the lock after changing the data.

Title: What is required to guarantee that the second thread sees the updated data, not stale data?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

72) Which of the following scenarios may not always cause a deadlock among two threads?

I Thread one is in an infinite loop and has acquired a lock

II Both threads are in an infinite loop, and one thread has acquired a lock

III Both threads are in an infinite loop, and both threads have acquired locks

a) I

b) II

c) III

d) I, II or III

Title: Which of the following scenarios may not cause a deadlock among two threads?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

73) Stale data occurs in multi-CPU machines when one thread modifies shared data and a second thread accesses that data later, but sees the data value before the change took place. What is required to guarantee that the second thread sees the updated data, not stale data when access to the shared data occurs in two different methods?

a) One method needs to acquire then release the lock after modifying the data.

b) Both methods need to acquire then release the lock after modifying or reading the data.

c) Never change any shared object data in multi-CPU machines.

d) Never use locks for any shared data in multi-CPU machines.

Title: What is required to guarantee that a second thread sees updated data when access to the shared data occurs in two different methods?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

74) A GUI should be responsive to the user. If a GUI interaction starts a time-consuming task, the GUI may not be responsive to the user until the task completes. Which approach would make the GUI responsive under these circumstances?

a) As you run the task, break and check for events.

b) Ask the user if she is sure she wants to run the task.

c) Run the task in a separate thread.

d) Disable the GUI during the task.

Title: Which approach would make the GUI responsive during a time-consuming task?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

75) When is it a good idea to call notifyAll in a synchronized method?

a) Whenever you exit it.

b) Upon exit, but only if threads before have called wait.

c) Upon exit, but only if the thread has replenished a needed resource.

d) Immediately upon entering the method.

Title: When is a good idea to call notifyAll in a synchronized method?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

76) Assume three threads share a BankAccount object with balance of zero (0), a ReentrantLock named myLock, and has a condition object on myLock named lowBalanceCondition, as shown below. Thread one calls withdraw(30), then thread two calls withdraw(20)and thread three calls deposit(45). If the starting balance is 0, what is the balance after the three calls and after the waiting threads have had a chance to run?

public void deposit(int dollars)

{

myLock.lock();

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

lowBalanceCondition.signalAll();

myLock.unlock();

}

public void withdraw(int dollars)

{

myLock.lock();

while (balance < dollars)

{

lowBalanceCondition.await();

}

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

myLock.unlock();

}

a) 0

b) 15 or 25

c) 45

d) -5

Title: What is the balance after the three calls and waiting threads have had a chance to run?

Difficulty: Hard

Section Reference 1: 21.5 Avoiding Deadlocks

77) Assume three threads share a BankAccount object with balance of zero (0), a ReentrantLock named myLock, and a condition object on myLock named lowBalanceCondition, as shown below. Thread one calls withdraw(30), then thread two calls withdraw(20)and thread three calls deposit(45). If the starting balance is 0, what is the balance after the three calls?

public void deposit(int dollars)

{

myLock.lock();

int newBalance = balance + dollars;

System.out.println("depositing");

balance = newBalance;

myLock.unlock();

}

public void withdraw(int dollars)

{

myLock.lock();

while (balance < dollars)

{

lowBalanceCondition.await();

}

int newBalance = balance - dollars;

System.out.println("withdrawing");

balance = newBalance;

myLock.unlock();

}

a) 0

b) 15 or 25

c) 45

d) -5

Title: If the starting balance = 0, what is the balance after the three calls?

Difficulty: Hard

Section Reference 1: 21.5 Avoiding Deadlocks

78) What happens if we try to start a thread with an instance of a Runnable class that did not override the run method?

a) A compiler error.

b) A checked exception is thrown.

c) The inherited method runs and does nothing.

d) The thread sleeps indefinitely.

Title: What happens if a runnable class does not override the run method?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

79) Exactly when does a thread finish in Java?

a) When its main method ends.

b) When its run method ends.

c) When it calls the method await.

d) When it is interrupted.

Title: Exactly when does a thread finish in Java?

Difficulty: Easy

Section Reference 1: 21.2 Terminating Threads

80) What happens when a thread calls the signalAll method of a Condition object connected to a lock, if no other thread had called await on that Condition object?

a) A compiler error.

b) A checked exception is thrown.

c) The unlock method call will block.

d) Nothing, the program executes normally.

Title: What happens when a thread calls signalAll when no other thread has called await?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

81) Which are ways that a thread can be blocked?

I when it is sleeping

II waiting for a lock to be available

III waiting after calling the await method

a) I

b) I and II

c) I and III

d) I, II and III

Title: How can a thread be blocked?

Difficulty: Medium

Section Reference 1: 21.5 Avoiding Deadlocks

82) Suppose that the class XYZ implements the interface Runnable. Which code creates a thread object and makes it available to the scheduler to be executed?

a)

Thread t = new Thread(new XYZ());

t.start();

b)

XYZ t = new Thread(new XYZ());

t.start();

c)

new XYZ().start();

d)

Thread t = new XYZ(new Thread());

t.start();

Title: Which code creates a thread object and makes it available to the scheduler?

Difficulty: Medium

Section Reference 1: 21.1 Running Threads

Document Information

Document Type:
DOCX
Chapter Number:
21
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 21 Multithreading
Author:
Cay S. Horstmann

Connected Book

Big Java Early Objects 5e Complete Test Bank

By Cay S. Horstmann

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party