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
1Adds 0.5 to the argument and takes the floor of the result.   
2None of the above  If the java.lang.Math.round method is invoked with an argument of the float type and the result exceeds the range of type int, then the result is Integer.MAX_VALUE.  
3It is not overloaded.  The argument and returned value are both of type double. If the argument is negative, then the returned value is NaN.  
4a  c  It is not overloaded.  The argument is an angle measured in radians.  The argument is of type double and represents an angle measured in radians. The returned value is of type double. The method does not throw any exceptions.  
5None of the above  The random method is not overloaded, does not declare any parameters and does not throw any exceptions. The returned value is greater than or equal to zero and is less than but not equal to one.  
6Returns the largest whole number that is less than or equal to the argument value.  The floor method is not overloaded, and declares only one parameter of type double. The return value is always of type double. The floor method does not declare any exceptions.  
7Returns the smallest whole number that is greater than or equal to the argument value.  The ceil method is not overloaded, and declares only one parameter of type double. The return value is always of type double. The ceil method does not declare any exceptions.  
8a  c  f  Four overloaded versions of abs exist.  The type of the return value depends on the argument type.  If the argument is positive, then the argument is returned.  The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type. No exceptions are declared. If the argument is a negative integral value, then the returned value is the two's complement of the argument. The magnitude of Integer.MIN_VALUE is one greater than the magnitude of Integer.MAX_VALUE; therefore, the absolute value of Integer.MIN_VALUE exceeds the range of Integer.MAX_VALUE. Due to the limited range of type int, the two's complement of Integer.MIN_VALUE is Integer.MIN_VALUE. For that reason, the Math.abs method returns Integer.MIN_VALUE when the argument is Integer.MIN_VALUE.  
9b  c  e  f  h  Positive infinity  NaN  Integer.MIN_VALUE  Long.MIN_VALUE  Positive zero  The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type. The result is positive infinity if the argument is negative infinity. The result is positive zero if the argument is negative zero. The result is NaN if the argument is NaN. If the argument is a negative integral value, then the returned value is the two's complement of the argument. The magnitude of Integer.MIN_VALUE is one greater than the magnitude of Integer.MAX_VALUE; therefore, the absolute value of Integer.MIN_VALUE exceeds the range of Integer.MAX_VALUE. Due to the limited range of type int, the two's complement of Integer.MIN_VALUE is Integer.MIN_VALUE. For that reason, the Math.abs method returns Integer.MIN_VALUE when the argument is Integer.MIN_VALUE. The negation of Short.MIN_VALUE is well within the range of type int; so Short.MIN_VALUE is never returned by the Math.abs method.  
10Prints: -4.0,-3.0,3.0,4.0  The Math.floor method returns a primitive of type double that is equal to the largest integral value that is smaller than or equal to the argument. For example, -4.0 is the floor of -3.6, because -4.0 is the largest floating-point value tthat is equal to an integral value that is closer to negative infinity than -3.6. Similarly, the Math.ceil method returns a primitive of type double that is equal to an integral value, and is smaller than any other such value that is larger than the argument. For example, -3.0 is the ceil of -3.6. The term "larger" refers to values that are closer to positive infinity. For example, -3 is larger than -4, because -3 is closer to positive infinity.  
11None of the above  The math.round method name is overloaded. If the argument is of type double, then the return value is of type long. If the argument is of type float, then the return value is of type int. Both return types are integral primitive types; so a decimal point should not be included in the output. The actual output is -4,-3,3,4.  
12Prints: 0,0  The math.round method adds 0.5 to the argument, and then takes the floor of the sum. The result of Math.round with an argument of -3.6 is -4. The result of Math.round with an argument of 3.6 is 4.0. The sum of the two results is zero.  
13volatile  A field might be shared between two or more threads. Each thread is allowed to maintain a working copy of the field. If the threads do not reconcile the working copies then each might be working with a different value. The volatile modifier is used to force each thread to reconcile its working copy of the field with the master copy in main memory.  
14a  c  The first number printed is greater than or equal to 0  The second number printed must always be greater than 5000  The notify method is never invoked on thread a1; so it will sleep for at least five seconds. The invocation of the join method forces the main thread to wait for the completion of thread a1. The argument of 6000 will allow the main thread to wait for six seconds if necessary, but we know that thread a1 will complete in only five seconds. The first number printed will be greater than or equal to zero, and the second number will be greater than or equal to 5000. The synchronized block is necessary, because it is necessary to hold the lock of an object when the wait method is invoked.  
15Prints: ABC  The block inside the main method is synchronized on the String array object sa. Inside the block, a new thread t1 is started and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa. Even if the thread scheduler moves thread t1 into the Running state, it will block while attempting to acquire the lock of the String array object sa. Thread t1 will continue to block until the synchronized block in the B.main method runs to completion. At that time, the contents of the String array object have all been updated.  
16Prints: ABC  Inside the main method, thread t1 is started and will move into the Running state at the discretion of the thread scheduler. The A.run method invokes the wait method on the String array object sa causing the thread to block until another thread invokes the sa.notify method. Before the B.main method invokes sa.notify, all of the elements of the String array object sa have already been updated.  
17b  c  d  e  h  The Thread.join method is always invoked on an instance of Thread  The Thread.join method causes the current thread to wait for the referenced thread to die  The Thread.join method declares an InterruptedException in the throws clause  The Thread.join method accepts a timeout value as an argument  A timeout of zero will allow Thread.join to wait forever if necessary  The Thread.join method is not static. Thread.join is always invoked on an instance of Thread. Thread.join causes the current thread to wait until the referenced thread has died. The maximum time limit to wait for the death of the referenced thread can be specified in milliseconds by an argument. Thread.join will throw an InterruptedException if the interrupt method is invoked on the current Thread.  
18b  d  By entering a synchronized instance method of the obj1  By entering the body of a block that is synchronized on obj1  Blocking on I/O or invoking the Thread.sleep or Object.wait method causes a thread to enter the Not-Runnable state. Invoking the notify method on an object wakes up a thread that is waiting on the object. The thread that invokes wait or notify on an object should already hold the lock of the object. Invoking the wait or notify method does not cause the thread to hold the lock. Static methods are synchronized on the lock of the Class object of the class. Instance methods are synchronized on the lock of the instance of the class.  
19a  b  d  f  Another thread invokes the notify method on the object, obj1, and T1 is selected to move out of the wait set  Another thread invokes the notifyAll method on the object  Another thread interrupts thread T1  A specified timeout period has elapsed   
20None of the above  All of the class instance creation expressions are legal. The String instance A is the name of the Thread. Yes, the exam requires you to memorize the Thread constructor signatures.  
21 The position of the arguments have been reversed in the constructor on line 3. The Runnable argument should appear before the thread name argument. Yes, the exam requires you to memorize the Thread constructor signatures.  
22The number printed is greater than or equal to 0  The main thread invokes the start method on thread a1. There is no way to predict when the new thread will start to run. At some point in time, the main thread will proceed to the print statement where the value of the startTime variable is subtracted from the current time. The value printed will be greater than or equal to one. At some point in time, thread a1 will begin to run and it will invoke the wait method. Since no other thread invokes the notify method on a1, it will wait forever, and the program will never run to completion.  
23The number printed is greater than or equal to 0  The a1 thread is a daemon thread; so the program can run to completion even if thread a1 is still running, waiting or sleeping. The notify method is never invoked on thread a1. If thread a1 were not a daemon thread, then the program would wait forever. However, the program will run to completion without waiting for a1.  
24b  c  Some or all of the numbers 0 through 9 could be printed  Nothing is printed  All of the threads started in method B.m1 are daemon threads; so the program can run to completion even if some or all of the daemon threads have not run.  
25Compile-time error  Remember that the Thread.start method is an instance method and can not be invoked from a static context.  
26Compile-time error  Remember that the Object.notify method is an instance method and can not be invoked from a static context. Also, the thread that invokes the notify method on an object must hold the lock of the object.  
27Compile-time error  Both the sleep and join methods declare an InterruptedException that must be caught or declared in the throws clause of A.main.  
28The priority assigned to thread T2 is greater than the priority assigned to T1  The Java Language Specification suggests that higher priority threads should be given preference over lower priority threads, but explicitly states that the preference is not a guarantee. It is very important to remember that no guarantee exists.  
 
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.