Dan Chisholm's
Java Programmer Certification Mock Exam

Please Help Save a Tree!

The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.

Today, you can find my book at amazon.com.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1b  c  h  notify  notifyAll  wait   
2e  g  sleep  yield   
3d  h  i  resume  stop  suspend  For the purposes of the exam, you don't need to memorize the deprecated methods of the Thread class. Even though a question such as this will not be on the exam, every Java programmer should know that the deprecated methods should not be used in new programs.  
4a  e  h  join  sleep  wait   
5a  e  h  join  sleep  wait   
6b  c  f  notify  notifyAll  wait   
7InterruptedException  The methods Object.wait, Thread.join and Thread.sleep name InterruptedException in their throws clauses.  
8A member variable that is an object reference  Primitives don't have locks; therefore, they can not be used to synchronize threads. A method local variable that is a reference to an instance that is created within the method should not be used to synchronize threads, because each thread has its own instance of the object and lock. Synchronization on an instance that is created locally makes about as much sense as placing on your doorstep a box full of keys to the door. Each person that comes to your door would have their own copy of the key; so the lock would provide no security.  
9b  e  f  g  h  i  A compile-time error occurs if the expression produces a value of any primitive type  If execution of the block completes normally, then the lock is released  If execution of the block completes abruptly, then the lock is released  A thread can hold more than one lock at a time  Synchronized statements can be nested  Synchronized statements with identical expressions can be nested   
10The process of executing a synchronized method requires the thread to acquire a lock  The synchronized modifier can not be applied to a class. A method that overrides a synchronized method does not have to be synchronized. If a thread invokes a synchronized instance method on an instance of class A, then the thread must acquire the lock of that instance of class A. The same is not true for synchronized static methods. A synchronized static method is synchronized on the lock for the Class object that represents the class for which the method is a member.  
11c  d  e  f  The Ready state to the Running state  The Running state to the Not-Runnable state  The Running state to the Ready state  The Not-Runnable state to the Ready state  A dead thread can not be restarted.  
12b  c  d  f  The Thread.yield method might cause the thread to move to the Ready state  The same thread might continue to run after calling the Thread.yield method  The Thread.yield method is a static method  The Thread.sleep method causes the thread to move to the Not-Runnable state  The Thread.yield method is intended to cause the currently executing thread to move from the Running state to the Ready state and offer the thread scheduler an opportunity to allow a different thread to execute based on the discretion of the thread scheduler. The thread scheduler may select the same thread to run immediately, or it may allow a different thread to run. The Thread.yield method is a native method; so the behavior is not guaranteed to be the same on every platform. However, at least some implementations of the yield method will not yield to a thread that has a lower priority.  
13 Thread.yield method  The Thread.yield method may cause a thread to move into the Ready state, but that state transition is not guaranteed. The JLS states that the Thread.yield method provides a hint to the thread scheduler, but the scheduler is free to interpret--or ignore--the hint as it sees fit. Nothing in the JLS suggests that the thread might move to the Not-Runnable state.  
14None of the above  A dead thread can not be restarted.  
15Ready   
16b  d  e  h  The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler  The Runnable interface declares the run method  The Thread class implements the Runnable interface  Some implementations of the Thread.yield method will not yield to a thread of lower priority  The Object.notify method can only be called by the thread that holds the lock of the object on which the method is invoked. Suppose that thread T1 enters a block that is synchronized on an object, A. Within the block, thread T1 holds the lock of A. Even if thread T1 calls the notify method immediately after entering the synchronized block, no other thread can grab the lock of object A until T1 leaves the synchronized block. For that reason, the transfer of control from thread T1 to any waiting thread can not be accelerated by moving the notify method to an earlier point in the synchronized block. The behavior of Thread.yield is platform specific. However, at least some implementations of the yield method will not yield to a thread that has a lower priority. Invoking the Thread.yield method is like offering a suggestion to the JVM to allow another thread to run. The response to the suggestion is platform specific.  
17a  c  f  Thread.MAX_PRIORITY == 10  Thread.NORM_PRIORITY == 5  Thread.MIN_PRIORITY == 1   
18b  d  A program will terminate only when all user threads stop running  A thread inherits its daemon status from the thread that created it   
19Prints: A  If a Runnable target object is passed to the constructor of the Thread class, then the Thread.run method will invoke the run method of the Runnable target. In this case, the Thread.run method is overridden by A.run. The A.run method does nothing more than print the letter A. The invocation of the A.start method inside the main method results in the invocation of A.run, and the letter A is printed. The B.run method is never invoked.  
20Prints: T1T1T3  The Thread.currentThread method returns a reference to the currently executing thread. When the run method is invoked directly it does not start a new thread; so T1 is printed twice.  
21Compile-time error  The Runnable.run method does not have a throws clause; so any implementation of run can not throw a checked exception.  
22An IllegalThreadStateException is thrown at run-time  For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. The actual behavior of the method might be different. If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored, and no exception will be thrown. For the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification and ignores the actual behavior of the 1.4 version of the JVM.  
 
Ask a Question
Send an email to me.
 
Java Question and Answer Forums
JavaRanch Big Moose Saloon
Marcus Green's Discussion Forum
java.sun.com Forums, Chat and User Groups
 
Other Resources
Java Language Specification
Java Virtual Machine Specification
Java 2 Platform, Standard Edition, v 1.4.0 API Specification
 
Tutorials
Learning the Java Language
Operator Precedence Chart, Expressions, Statements, Blocks
Programming with Assertions
 

Copyright © 2002-2004, Dan Chisholm
All rights reserved.