Filters
Question type

Study Flashcards

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

E) None of the above
F) A) and B)

Correct Answer

verifed

verified

Insert the statement that would start the following thread. Thread firstThread = new Thread(myRunnable) ; ____________________


A) firstThread.run() ;
B) firstThread.start() ;
C) run() ;
D) start() ;

E) B) and D)
F) B) and C)

Correct Answer

verifed

verified

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


A) ThreadInterruptedException
B) ThreadException
C) ThreadSleepException
D) InterruptedException

E) All of the above
F) A) and D)

Correct Answer

verifed

verified

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

E) A) and C)
F) None of the above

Correct Answer

verifed

verified

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

E) A) and C)
F) B) and C)

Correct Answer

verifed

verified

B

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

E) A) and B)
F) None of the above

Correct Answer

verifed

verified

B

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.

E) A) and B)
F) B) and C)

Correct Answer

verifed

verified

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


A) InterruptedException
B) SleepException
C) lock
D) SignalException

E) None of the above
F) B) and D)

Correct Answer

verifed

verified

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

E) C) and D)
F) A) and B)

Correct Answer

verifed

verified

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

E) B) and D)
F) A) and B)

Correct Answer

verifed

verified

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.

E) C) and D)
F) B) and C)

Correct Answer

verifed

verified

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

E) A) and D)
F) B) and D)

Correct Answer

verifed

verified

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


A) IOException
B) IllegalStateMonitorException
C) InterruptedException
D) SleepException

E) All of the above
F) C) and D)

Correct Answer

verifed

verified

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.

E) A) and B)
F) A) and C)

Correct Answer

verifed

verified

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.

E) A) and B)
F) B) and D)

Correct Answer

verifed

verified

B

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

E) A) and B)
F) A) and C)

Correct Answer

verifed

verified

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


A) InterruptException
B) InterruptedException
C) ThreadException
D) ThreadTerminatedException

E) All of the above
F) A) and B)

Correct Answer

verifed

verified

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.

E) B) and C)
F) A) and D)

Correct Answer

verifed

verified

____ 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

E) C) and D)
F) A) and C)

Correct Answer

verifed

verified

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

E) B) and D)
F) B) and C)

Correct Answer

verifed

verified

Showing 1 - 20 of 82

Related Exams

Show Answer