These are made up questions.
1. What will the output of the following code be?
1 Compilation fails
2 A runtime exception is thrown
3 goodbyegoodbyegoodbyehellohellohello
4 hellohellohellogoodbyegoodbyegoodbye
5 A mixture of hello, goodbye of which the exact order can not be known
2 Which two statements are false about using a Runnable interface?
1 You can pass a Runnable target into a
Thread constructor
2 You call the start() method on the Runnable target to initiate the thread
3 The method to override in the Runnable interface is public void run()
4 A thread class can be defined without using the Runnable interface
5 If you extend Thread and implement Runnable in the same class compilation
will succeed
6 You can utilize a Runnable target as a separate thread without a Thread
instance
7 An abstract class which extends Runnable does not need to override any
methods from java.lang.Runnable
3 Which of the following three statements are true?
1 It is possible for a created thread to never enter the runnable state
2 More than two threads waiting on a single object lock indicates deadlock
3 Sleeping, waiting, and blocking are all ways a thread can be not eligible to be run
4 A thread is guaranteed to eventually receive an objects lock
5 A thread obtains the default priority of Thread.NORM_PRIORITY
6 A call to join on another thread instance halts execution of the current
thread
7 A dead thread is always eligible for garbage collection
4 Which three methods are complete method definitions for java.lang.Thread?
1 public static void sleep(long millis) throws InterruptedException
2 private final void wait()
3 public final void setPriority(int newPriority)
4 public final void join()
5 public final void notifyAll()
6 public void start()
5 How many of the following calls guarantee that a thread will give up its
object locks?
wait()
notifyAll()
join()
sleep()
yield()
notify()
1 - none of the above methods
2 - 1 of the above methods
3 - 2 of the above methods
4 - 3 of the above methods
5 - 4 of the above methods
6 - All of the above methods
6 Which of the followingmethods contains a incorrect syntax for synchronizing on an object lock?
1 method1 contains an error
2 method2 contains an error
3 method3 contains an error
4 method4 contains an error
5 none of the methods contain an error
7 Which statement is true about the following code?
1 The notify() call will allow any thread with a wait on Executor to run
immediately
2 There is not an exception thrown by sleep
3 The notify() call suspends execution of the thread
4 The notify() call will do nothing
5 The notify() call will allow any thread with a wait on Executor to run on
completion of the method
6 The code will not compile
8 Which two of the following statements are true?
1 wait() and notify() is a mechanism to communicate between threads
2 A thread can wait on an object outside of a synchronized context
3 A thread can wait interminably for a notify() to be called elsewhere
4 notifyAll() is a faster method than notify() when dealing with lots of
threads
5 wait() and the synchronized keyword accomplish the same thing
6 While a thread is waiting it will hold a lock on an object
1 Objective 7.1
3 The call on the extended class of Thread is run, not start, so the execution is not started in a new thread
2 Objective 7.1
2,6 are false
1 - True, you tell the Thread about the Runnable target through the constructor
2 - False, you call the start() method on the Thread to initiate the thread
which calls run on the Runnable target
3 - True, public void run() is the method to override
4 - True A thread class can extend Thread instead of implementing Runnable
5 - True You can extend Thread and also implement Runnable, although this is
poor design
6 - False, you need a Thread instance to utilize a Runnable target
7 - True, an abstract class does not need to override methods in an interface- nonabstract classes do
3 Objective 7.2
1, 3 and 6 are true statements
1 - True. A thread created with new and never having start() called on it will not enter the runnable state
2 - False. Many threads can wait on a single object and not be in deadlock.
3 - True, these are the common ways a thread can be in an ineligible state to be run
4 - False. A thread is not guaranteed to receive an objects lock, and will not in a case of deadlock.
5 - False. A thread obtains by default the priority of the thread of execution which creates the thread.
6- True. A call to join on another thread instance does cease execution of the current thread until that other thread reaches the dead state
7 - False. A dead thread has completed its run, but still may be a viable
object which is not eligible for garbage collection.
4 Objective 7.2
1, 3, 6
1 Correct
2 wait is a method of java.lang.Object
3 Correct
4 Incorrect- the full definition for join without any arguments is: public
final void join() throws InterruptedException
5 Incorrect notifyAll is a method of java.lang.object
6 correct
5 Objective 7.3
2 - 1 of the above methods
The only method which certainly gives up locks is wait(). It is common for
notify and notifyAll to shortly exit the synchronized area they are within, it is not necessary or guaranteed.
6 Objective 7.3
3 method3 contains an error- you do not synchronize variables
7 Objective 7.4
4 The notify() call will do nothing
For notify to work, it must be within synchronized code. It is not within
synchronized code with this example
8 Objective 7.4
1,3 are true
Section 7: Threads
1 Write code to define, instantiate and start new threads using both
java.lang.Thread and java.lang.Runnable.
2 Recognize conditions that might prevent a thread from executing.
3 Write code using synchronized wait, notify and notifyAll to protect against concurrent access problems and to communicate between threads.
4 Define the interaction among threads and object locks when executing
synchronized wait, notify or notifyAll.
[ September 18, 2004: Message edited by: Tom Tolman ]