Programming Languages Chapter 23 Other Threads Attempting Perform An operation That 

subject Type Homework Help
subject Pages 9
subject Words 4089
subject Authors Harvey Deitel, Paul Deitel

Unlock document.

This document is partially blurred.
Unlock all pages and 1 million more documents.
Get Access
page-pf1
Java How to Program, 10/e Multiple Choice Test Bank 1 of 11
Chapter 23, Concurrency
23.1 Introduction
23.1 Q1: Two tasks that are operating ________ are both making progress at once.
a. sequentially
b. concurrently
c. iteratively
d. recursively
23.1 Q2: Two tasks that are operating ________ are executing simultaneously.
a. concurrently
b. in parallel
c. sequentially
d. iteratively
23.1 Q3: Which of the following statements is false?
a. Java makes concurrency available to you through the language and APIs.
b. Concurrency is a subset of parallelism.
c. Today’s multi-core computers have multiple processors that can perform tasks in parallel.
d. Java programs can have multiple threads of execution, where each thread has its own method-call stack and
program counter, allowing it to execute concurrently with other threads. This capability is called multithreading.
23.1 Q4: Which of the following statements is false?
a. In a multithreaded application, threads can be distributed across multiple processors (if available) so that multiple
tasks execute in parallel and the application can operate more efficiently.
b. The JVM creates threads to run a program and for housekeeping tasks such as garbage collection.
c. Multithreading can increase performance only on multi-core systems.
d. The vast majority of programmers should use existing collection classes and interfaces from the concurrency APIs
that manage synchronization for you.
23.2 Thread States: Life Cycle of a Thread
23.2.1 New and Runnable States
23.2.1 Q1: A new thread begins its life cycle by transitioning to the __________ state.
a. runnable.
b. waiting.
c. terminated.
d. new.
23.2.2 Waiting State
23.2.2 Q1. A waiting thread transitions back to the ________ state only when another thread notifies it to continue
executing.
a. runnable
b. terminated
c. new
d. blocked
page-pf2
Java How to Program, 10/e Multiple Choice Test Bank 2 of 11
23.2.3 Timed Waiting State
23.2.3 Q1: Which of the following statements is false?
a. A runnable thread can enter the timed waiting state for a specified interval of time. It transitions back to the
runnable state when that time interval expires or when the event it’s waiting for occurs.
b. Timed waiting threads and waiting threads can use a processor, if one is available.
c. A runnable thread can transition to the timed waiting state if it provides an optional wait interval when it’s waiting
for another thread to perform a task. Such a thread returns to the runnable state when it’s notified by another thread
or when the timed interval expireswhichever comes first.
d. Another way to place a thread in the timed waiting state is to put a runnable thread to sleepa sleeping thread
remains in the timed waiting state for a designated period of time (called a sleep interval), after which it returns to
the runnable state.
23.2.4 Blocked State
23.2.4 Q1: Which of the following statements is false?
a. A runnable thread transitions to the blocked state when it attempts to perform a task that cannot be completed
immediately and it must temporarily wait until that task completes.
b. When a thread issues an input/output request, the operating system blocks the thread from executing until that I/O
request completesat that point, the blocked thread transitions to the runnable state, so it can resume execution.
c. A blocked thread cannot use a processor, even if one is available.
d. Each of the above statements is true.
23.2.5 Terminated State
23.2.5 Q1: A runnable thread enters the ________ state (sometimes called the dead state) when it successfully
completes its task or otherwise terminates (perhaps due to an error).
a. extinct
b. defunct
c. terminated
d. aborted
23.2.6 Operating-System View of the Runnable State
23.2.6 Q1: With timeslicing, each thread is given a limited amount of time, called a __________, to execute on a
processor.
a. quantum
b. burst
c. time allocation
d. scheduling interval
23.2.7 Thread Priorities and Thread Scheduling
23.2.7 Q1: Which of the following statements is false?
a. Every Java thread has a thread priority that helps determine the order in which threads are scheduled.
b. Each new thread inherits the priority of the thread that created it.
c. Informally, higher-priority threads are more important to a program and should be allocated processor time before
lower-priority threads.
page-pf3
Java How to Program, 10/e Multiple Choice Test Bank 3 of 11
d. Thread priorities guarantee the order in which threads execute.
23.2.7 Q2: Its recommended that you do not explicitly create and use Thread objects to implement concurrency, but
rather use the ________ interface.
a. ExecutorService
b. Runnable
c. Concurrent
d. Executor
23.2.7 Q3: Which of the following statements is false?
a. Most operating systems support timeslicing, which enables threads of equal priority to share a processor.
b. Without timeslicing, each thread in a set of equal-priority threads runs to completion before other threads of equal
priority get a chance to execute.
c. With timeslicing, even if a thread has not finished executing when its quantum expires, the processor is taken
away from the thread and given to the next thread of equal priority, if one is available.
d. An operating system’s thread scheduler determines which thread runs next.
23.2.7 Q4: Which of the following statements is false?
a. Thread scheduling is platform independent.
b. One simple thread-scheduler implementation keeps the highest-priority thread running at all times and, if there’s
more than one highest-priority thread, ensures that all such threads execute for a quantum each in round-robin
fashion.
c. The round-robin process for all highest-priority threads continues until all threads run to completion.
d. Most programmers who use Java multithreading will not be concerned with setting and adjusting thread priorities
23.2.8 Indefinite Postponement and Deadlock
23.2.8 Q1: a. When a higher-priority thread enters the ready state, the operating system generally preempts the
currently running thread (an operation known as preemptive scheduling). Depending on the operating system, a steady
influx of higher-priority threads could postponepossibly indefinitelythe execution of lower-priority threads. such
indefinite postponement is sometimes referred to more colorfully as________.
a. fasting
b. famine
c. starvation
d. malnourishment
23.2.8 Q2: Operating systems employ a technique called ________ to prevent indefinite postponementas a thread
waits in the ready state, the operating system gradually increases the thread’s priority to ensure that the thread will
eventually run.
a. incrementing
b. prioritizing
c. maturing
d. aging
page-pf4
23.2.8 Q3: Another problem related to indefinite postponement is called ________. This occurs when a waiting
thread (let’s call this thread1) cannot proceed because it’s waiting (either directly or indirectly) for another thread
(let’s call this thread2) to proceed, while simultaneously thread2 cannot proceed because it’s waiting (either
directly or indirectly) for thread1 to proceed. The two threads are waiting for each other, so the actions that
would enable each thread to continue execution can never occur.
a. impass
b. standoff
c. deadlock
d. stalemate
23.3 Creating and Executing Threads with Executor Framework
23.3 Q1: The preferred means of creating multithreaded Java applications is by implementing the ________
interface. An object of a class that implements this interface represents a task to perform.
a. Thread
b. Runner
c. Runnable
d. None of the above.
23.3 Q2: The main method executes in the ________ thread of execution.
a. starting
b. main
c. local
d. None of the above.
23.3 Q3: An ExecutorService object is created by calling a static method of which class?
a. Executor.
b. Executors.
c. ExecutorService.
d. Thread.
23.3 Q4: Which of the following is not true of ExecutorService?
a. It is a subinterface of Executor.
b. It is an object that can be run in a separate thread.
c. It declares method shutdown.
d. It manages a group of threads.
23.4 Thread Synchronization
23.4 Q1: When a __________ method or block is running on an object, the object is locked so no other such method
can run on that object at the same time.
a. synchronized
b. shared
c. thread
d. writeable
23.4 Q2: When using Java's built-in monitors, every object has a(n) ________ or a(n) ________ that the monitor
ensures is held by a maximum of only one thread at any time.
a. monitor lock, intrinsic lock
b. built-in lock, free lock
c. mutual exlcusion lock, synchronization lock
page-pf5
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 objectall 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.
page-pf6
Java How to Program, 10/e Multiple Choice Test Bank 6 of 11
d. Using a synchronized block to enforce mutual exclusion is an example of the design pattern known as the Java
Exclusion Pattern.
23.4.3 Unsynchronized Mutable Data Sharing
23.4.3 Q1: Interface ExecutorService provides the ________ method, which returns control to its caller either
when all tasks executing in the ExecutorService complete or when the specified timeout elapses.
a. waitForTermination
b. wait
c. awaitTermination
d. None of the above.
23.4.4 Synchronized Mutable Data SharingMaking Operations Atomic
23.4.4 Q1: You can simulate atomicity by ensuring that ________.
a. at least one thread carries out its operations on an object at a time
b. two threads carry out their operations on an object in parallel
c. only one thread carries out its operations on an object at a time
d. None of the above.
23.5 Producer/Consumer Relationship without Synchronization
23.5 Q1: In a producer/consumer relationship, the ________ portion of an application generates data and stores it in
a shared object, and the ________ portion of an application reads data from the shared object.
a. consumer, producer
b. producer, consumer
c. outputter, inputter
d. None of the above.
23.6 Producer/Consumer Relationship: ArrayBlockingQueue
23.6 Q1: The BlockingQueue interface declares which two methods for blocked adding and blocked removing of
elements from a circular buffer?
a. put and take.
b. add and remove.
c. push and pop.
d. place and get.
23.6 Q2: The ArrayBlockingQueue method ________ returns the number of elements currently in the
ArrayBlockingQueue?
a. size.
b. length.
c. getSize.
d. getLength.
23.6 Q3: In a producer/consumer relationship with a single cell of shared memory, which of the following is true?
a. The consumer must run first.
b. The producer must run first.
c. The producer must run first or the consumer will have to wait.
d. The consumer must run first or the producer will have to wait.
page-pf7
Java How to Program, 10/e Multiple Choice Test Bank 7 of 11
23.7 Q1: When a thread obtains the monitor lock on an object, then determines that it cannot continue with its task
on that object until some condition is satisfied, the thread can call Object method ________; this releases the
monitor lock on the object, and transitions the thread waits to the waiting state.
a. waitForOtherThreads
b. stop
c. waitForCondition
d. wait
23.7 Q2: When a thread executing a synchronized statement (or method) completes or satisfies the condition on
which another thread may be waiting, it can call Object method ________ or ________ to allow a waiting thread or
all waiting threads to transition to the runnable state again.
a. notifyThread, notifyAllThreads
b. wakeUpThread, wakeUpAllThreads
c. notify, notifyAll
d. None of the above.
23.8 (Advanced) Producer/Consumer Relationship: Bounded Buffers
23.8 Q1: Which one of the following statements is true with regard to a producer/consumer relationship with one
producer and one consumer?
a. A producer can never produce faster than the consumer is consuming.
b. A consumer can never consume faster than the producer is producing.
c. A producer can produce more items than a consumer consumes.
d. A consumer can consume more items than a producer produces.
23.8 Q2: Which of the following is true for a correct producer/consumer relationship with one producer, one
consumer, and a 5-cell buffer?
a. The producer can produce when all cells are full.
b. The consumer can consume when all cells are full.
c. The consumer can consume when all cells are empty.
d. None of the above.
23.9 Q1: This class is used to help implement mutual exclusion.
a. MutEx.
b. Condition.
c. Lock.
d. Signal.
23.9 Q2: If a thread calls __________, then every thread waiting for the object becomes eligible to acquire the lock.
a. signalEach
b. signalAll
c. signal
d. signalMethods
page-pf8
23.9 Q3: In a producer/consumer relationship, when a consumer finds the buffer empty or finds that the previous
data has already been read, the consumer should call __________.
a. lock
b. signal
c. sleep
d. await.
23.10 Concurrent Collections
23.10 Q1: a. Synchronized versions of the collections in the Java Collections API allow ________ at a time to
access a collection that might be shared among several threads.
a. any number of threads
b. only one thread
c. a maximum of two threads
d. None of the above.
23.10 Q2: Which of the following statements is false?
a. The collections from the java.util.concurrent package are specifically designed and optimized for sharing
collections among multiple threads.
b. ConcurrentHashMap and ConcurrentLinkedQueue are by far the most frequently used concurrent collections.
c. The concurrent collections have been enhanced to support lambdas.
d. Rather than providing methods to support streams, the concurrent collections provide their own implementations
of various stream-like operationse.g., ConcurrentHashMap has methods forEach, reduce and searchthat are
designed and optimized for concurrent collections that are shared among threads.
23.11 Multithreading with GUI: SwingWorker
23.11 Q1: The ________ thread should be used to perform all tasks that manipulate GUI components, because they
are not thread safe.
a. event-handling
b. event-dispatch
c. GUI-handling
d. None of the above.
23.12 Q2: SwingWorker method ________ executes a long computation in a worker thread.
a. execute.
b. performCalculation.
c. doInBackground.
d. None of the above.
23.11.1 Performing Computations in a Worker Thread: Fibonacci Numbers
23.11.1 Q1: SwingWorker is a _______ class.
a. anonymous inner
b. composite
c. nested
d. generic
page-pf9
23.11.1 Q2: Any GUI components that will be manipulated by SwingWorker methods, such as components that will
be updated from methods process or done, should be passed to the SwingWorker subclass’s ________ and stored in
the subclass object. This gives these methods access to the GUI components they’ll manipulate.
a. constructor
b. methods
c. anonymous methods
d. thread
23.11.2 Processing Intermediate Results: Sieve of Eratosthenes
23.11.2 Q1: Which of the following statements is false?
a. SwingWorker method publish repeatedly sends intermediate results to method process, which displays the
results in a GUI component. Method setProgress updates the progress property.
b. Method process executes in the event dispatch thread and receives data from method publish. The passing of
values between publish in the worker thread and process in the event dispatch thread is synchronous; process is
not necessarily invoked for every call to publish.
c. PropertyChangeListener is an interface from package java.beans that defines a single method,
propertyChange.
d. Every time method setProgress is invoked, a PropertyChangeEvent is generated to indicate that the progress
property has changed.
23.12 sort/parallelSort Timings with the Java SE 8 Date/Time API
23.12 Q1: Class Instant’s static method ________ gets the current time.
a. currentTime
b. immediate
c. now
d. present
23.12 Q2: To determine the difference between two Instants, use class Duration’s static method ________,
which returns a Duration object containing the time difference.
a. difference
b. interval
c. between
d. span
23.12 Q3: Duration method ________ returns the Duration as a long value in milliseconds.
a. milliseconds
b. term
c. extent
d. toMillis
23.12 Q4: NumberFormat static method ________ returns a NumberFormat that’s used to format a number as a
percentage.
a. percent
b. getPercentInstance
c. percentage
d. toPercent
page-pfa
23.12 Q5: NumberFormat method ________ returns a String representation of its argument in the specified numeric
format.
a. number
b. toNumeric
c. format
d. numeric
23.12 Q6: Arrays static method ________ fills an array with values produced by a generator function that
receives an int and returns a value of type int, long or double.
a. parallelFill
b. parallelFillAll
c. generateAll
d. parallelSetAll
23.12 Q7: Arrays static method ________ applies a BinaryOperator to the current and previous array elements
and stores the result in the current element.
a. prefix
b. parallelSet
c. parallelApply
d. parallelPrefix
23.13 Java SE 8: Sequential vs. Parallel Streams
23.13 Q1: Streams are easy to parallelize, enabling programs to benefit from enhanced performance on ________
systems.
a. uni-core
b. serial
c. multi-core
d. sequential
23.13 Q2: To obtain a parallel stream, simply invoke method ________ on an existing stream.
a. toParallel
b. toStream
c. parallel
d. toParallelStream
23.14 (Advanced) Interfaces Callable and Future
23.14 Q1: The Callable interface (of package java.util.concurrent) declares a single method named call that
allows a task to return a value.
a. call
b. execute
c. invoke
d. None of the above.
23.14 Q2: ExecutorService method ________ executes a Callable passed in as its argument. Method submit
returns an object of type Future (of package java.util.concurrent) that represents the future result of the
executing Callable.
a. submit
b. execute
page-pfb
Java How to Program, 10/e Multiple Choice Test Bank 11 of 11
c. future
d. perform
23.14 Q3: Which of the following statements is false?
a. Java SE 8 introduces a new CompletableFuture class (package java.util.concurrent), which implements the
Future interface and enables you to asynchronously execute Runnables that perform tasks or Suppliers that return
values.
b. CompletableFuture static method supplyAsync asynchronously executes a Supplier task that returns a value.
c. CompletableFuture static method runAsync asynchronously executes a Runnable task that does not return a
result.
d. CompletableFuture method get is a blocking methodit causes the calling thread to run until the asynchronous
task completes and returns its results.
23.15 (Advanced) Fork/Join Framework
23.15 Q1: Java’s concurrency APIs include the fork/join framework, which helps programmers parallelize
algorithms. The fork/join framework particularly well suited to divide-and-conquer-style algorithms, like the
________ sort.
a. insertion sort
b. selection sort
c. merge sort
d. bubble sort

Trusted by Thousands of
Students

Here are what students say about us.

Copyright ©2022 All rights reserved. | CoursePaper is not sponsored or endorsed by any college or university.