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 toner, and save money. If you prefer to work my exams from printed pages, then you can save a tree, save your printer toner, and save money if you buy my new book instead of attempting to print the pages of my web site.

Today, you can find my book on the retail web site of the company that prints it and distributes it.

Send an email to me.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1TreeSet  The elements are not key/value pairs; so a Map is not a good choice. A List generally accepts duplicate elements. A Set stores a collection of unique objects; so any attempt to store a duplicate object is rejected. TreeSet stores elements in an order that is determined either by a Comparator or by the Comparable interface.  
2Prints: false,true,true  HashSet implements the Set interface, but not the Map interface. HashMap extends AbstractMap and implements the Map interface. Hashtable extends Dictionary and implements the Map interface.  
3a  b  return 31;  return getI1();  A hashCode method that returns the constant value 31 is consistent with the hash code contract. Even so, a hashCode method that returns the same value regardless of the internal state of the object is not very good, because it will cause hashtables to place every instance of the class in the same bucket. If the equals method determines that two instances are equal, then the two instances must produce the same hash code. For that reason, the hash code must not be calculated using fields that are not used to determine equality. In this case, the equals method determines equality based on the value of i1. The value of i2 is not used to determine equality; therefore, i2 can not be used to calculate the hash code.  
4HashMap  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. TreeMap and TreeSet store elements in a sorted order based on the key.  
5Prints: true,true,true  All three implement Cloneable.  
6return 31 * getI1() + getI2();  All of the statements would produce a hashCode method that is consistent with the hash code contract. The expression 31 * getI1() + getI2() produces the most efficient hashCode method, because it is most likely to produce unique hashcodes for various combinations of i1 and i2. The expression getI1() + getI2() is less efficient, because it produces the same hash code when the values of i1 and i2 are swapped.  
7List  The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A List allows entries to be accessed using an index.  
8Prints: true,true,false  HashMap does not implement the Collection interface.  
9c  d  e  f  TreeMap  TreeSet  HashMap  HashSet  The Vector and Hashtable methods are synchronized and do not allow for simultaneous access by multiple threads. The concrete subclasses of the AbstractList, AbstractMap, and AbstractSet classes allow for unsynchronized read operations by multiple threads. Additionally, the sychronized wrapper methods of the Collections class allow for the instantiation of a Collection, List, Map, Set, SortedMap, or SortedSet with synchronized methods. If simultaneous read and write operations are necessary then a synchronized instance should be used.  
10SortedMap  The List and Set interfaces do not support key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A Map organizes the entries as key/value pairs. The SortedMap is similar to a Map except that the ordering of the elements is determined by a Comparator or the Comparable interface.  
11Prints: false,true,false  LinkedHashMap does not implement the Collection interface or the List interface.  
12SortedSet  The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. The SortedSet is similar to a Set except that the ordering of the elements is determined by a Comparator or the Comparable interface.  
13Prints: false,true,true  LinkedHashMap does not implement the Collection interface. LinkedHashMap extends HashMap and implements Map, Cloneable and Serializable.  
14Prints: true,true,false  LinkedHashSet does not implement the List interface. LinkedHashSet extends HashSet and implements Collection, Set, Cloneable and Serializable.  
15Prints: false,false,true  The Collections class is not the same as the Collection interface. The Collections class contains a variety of methods used to work with collections. For example, Collections.shuffle is used to randomly shuffle the elements of a Collection. Similarly, the Arrays class provides utility methods for working with arrays.  
16HashMap  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. TreeMap and TreeSet store elements in a sorted order based on the key. The iteration order of LinkedHashMap and LinkedHashSet is not unspecified. By default, the iteration order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted. Optionally, the iteration order of the LinkedHashMap can be set to the order in which the elements were last accessed.  
17Hashtable  The requirement to store key/value pairs is directly satisfied by a Hashtable or any concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. The requirement to NOT allow null elements is satisfied by Hashtable, but not by HashMap or any of the other Collection implementations that were introduced with Java 1.2 and later.  
 
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.