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
1d  e  All of the escape sequences used in this question are defined for the C programming language. Those that are not also Java escape sequences result in a compile-time error. Java does not accept the hexadecimal escape sequences of the C programming language. However, Java does accept Unicode escapes (JLS 3.3).  
2c  g  -esa  -enablesystemassertions  Two command-line switches used to enable assertions in system classes are -esa and -enablesystemassertions. Remember that all of the letters are lower case.  
3Prints: DDDLDD  The round method does not return either of the two floating point primitive types, float or double. The ceil, sin and sqrt methods return only a value of type double. The abs and max methods are able to return an int, long, float or double depending on the argument type.  
4Prints: false,true,true  ListIterator extends Iterator.  
5a  b  d  e  f  By default assertions are disabled at run-time.  Assertions can be selectively enabled for any named class.  Assertions can be selectively enabled for any named package.  If assertions are selectively enabled for any named package then assertions are automatically enabled for the subpackages.  Assertions can be selectively enable for the unnamed package in the current working directory.   
6c  d  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
7e  f  The overloaded contructors for the String class accept a parameter of type String or StringBuffer or an array of byte or char. There is no constructor that will accept a primitive byte or char. Please note that if you want to convert a primitive to a String then you can use the static String.valueOf method.  
8Prints: DDDD  The floor and ceil methods are not overloaded. There is only one version of each method. The parameter type is double, and the return type is double.  
9AssertionError  An AssertionError is thrown to indicate that an assertion has failed. Don't be fooled by the name AssertionException.  
10a  b  c  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
11Prints: FDDD  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 sqrt method is not overloaded. The parameter type is double and the return type is double.  
12f  g  LinkedHashMap  LinkedHashSet  The HashMap, HashSet and Hashtable classes are all implemented with an internal hashtable that organizes the elements in buckets according to the hashcode and not according to the order of insertion. The LinkedHashMap and LinkedHashSet classes also use an internal hashtable, and both also maintain a linked list through all of the elements. The order of the list is determined by the order of insertion. Optionally, the LinkedHashMap can maintain the order of the list based on the time of the most recent access of each element. The TreeMap and TreeSet classes are both implemented using a tree structure that is ordered based on a Comparator or the Comparable interface.  
13c  d  e  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration in an interface, the usage is redundant and is discouraged. Since all methods declared within an interface are implicitly public, a weaker access level can not be declared.  
14Prints: IIILFD  There are four overloaded versions of the Math.min method with versions that declare one parameter of type int, long, float or double. The return type is the same as the parameter type. Arguments of type byte, char and short are promoted to type int.  
15None of the above  All methods declared within an interface are implicitly abstract and public. Although the abstract and public modifiers can legally be applied to a method declaration within an interface, the usage is redundant and is discouraged. The modifiers, final, synchronized and native, can not appear in the declaration of an abstract method, but they can be added to an implementation of an abstract method.  
16The relationship between Cat and Dog is an example of an appropriate use of inheritance.  An appropriate inheritance relationship includes a subclass that "is-a" special kind of the superclass. The relationship between the Dog subclass and the Pet superclass is an example of an appropriate inheritance relationship, because a Dog "is-a" Pet. The relationship between the Cat subclass and the Dog superclass is not an example of an appropriate use of inheritance, because a Cat is not a special kind of a Dog. The goal of the OO paradigm is to develop software models that are accurate and reusable. If the software model is not accurate, then it probably is not reusable and the goals of the OO paradigm are not achieved. Code reuse and maintenance becomes increasingly difficult when inheritance is used to model inappropriate relationships. For example, suppose that somebody implements a herdSheep method in the Dog class. The Cat subclass would inherit the method and suddenly each instance of Cat would acquire the unwanted capability to make an attempt to herd sheep. It is difficult to imagine that a Cat would perform well in that role, so additional maintenance would be required to resolve the problem.  
17b  d  A program will terminate only when all user threads stop running  A thread inherits its daemon status from the thread that created it   
18Prints: 12,true  The valueOf method returns a String representation of the input parameter. The input parameter may be of type Object, char[], or any primitive type. The toString method returns a reference to the existing String instance. It does not create a new instance of the String.  
19LinkedHashMap  The iteration order of a Map is the order in which an iterator moves through the elements of the Map. The iteration order of a LinkedHashMap is determined by the order in which elements are inserted. When a reference to an existing Map is passed as an argument to the constructor of LinkedHashMap, the Collection.addAll method will add the elements of the existing Map to the new instance. Since the iteration order of the LinkedHashMap is determined by the order of insertion, the iteration order of the new LinkedHashMap must be the same as the iteration order of the old Map.  
20Prints: SA SB CA CB  The static initializer of the super class runs before the static initializer of the subclass. The body of the superclass constructor runs to completion before the body of the subclass constructor runs to completion.  
21Prints: 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.  
22LinkedHashMap  The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to allow null elements is not satisfied by a Hashtable. The LinkedHashMap offers the option to remove the least recently used (LRU) element when a new element is added. The LinkedHashSet does not offer the LRU option.  
23Prints: 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.  
24Compile-time error  The Runnable.run method does not have a throws clause; so any implementation of run can not throw a checked exception.  
25Prints: 6,6  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)6,6. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The loop terminates when i reaches six.  
26Prints: 9,4  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (1,8)(3,7)(5,6)(7,5)9,4. The variable, i, is incremented twice with each pass through the loop. The variable, j, is decremented once with each pass. The boolean expression of the while loop, i++ <= j--, is false when i reaches 8 and j reaches 5. The value of i is subsequently incremented and j is decremented yielding 9 and 4 respectively. Those values are printed.  
27Prints: 8,4  Suppose the print statement, System.out.print("(" + i + "," + j + ")");, is inserted at the top of the loop. The output of the program would be as follows: (0,9)(2,8)(4,7)(6,6)(7,5)8,4. The initial value of j is 9. With each pass through the loop, the expression, j-- < 7, decrements j. The initial value of variable i is 0. With each pass through the loop, the expression, i++ < 7, increments variable i. Inside the body of the loop, i is also incremented as long as j is greater than or equal to 7. When j is less than seven, variable i is no longer incremented inside the body of the loop, but it is still incremented by the expression, i++ < 7.  
28Prints: 3,1  Suppose the print statement, System.out.print("("+i+","+j+","+k+")");, is inserted before the switch statement. The output of the program would be as follows: (1,0,1)(2,0,2)(2,1,3)(3,1,4)3,1. On the first iteration, case 1 is processed. The "continue label1;" statement causes control to go directly to the top of the for-loop following the label, label1. The boolean expression of the do-loop is not processed. On the second iteration, case 2 is processed. The break statement causes the switch statement to complete, and control passes to the boolean expression of the do-loop. On the third iteration, case 3 is processed. The break with label statement, "break label2;", completes abruptly; and the do-loop completes abruptly without processing the loop expression, ++j<5, so j is not incremented. On the fourth iteration, case 4 is processed. The break with label statement, "break label1;", completes abruptly, and the for-loop completes abruptly.  
 
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.