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
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1None of the above  All of the declarations are legal. The first three ( 061, '\61', '\061' ) are declared in octal format. The fourth (0x0031) is declared as a hexadecimal literal. The fifth ('\u0031') is a Unicode escape sequence.  
2Prints: false,true  The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The reference variable s2 is initialized with a reference to an instance of type String containing the value "EFGHIJ". The expression s3 = s1.substring(4,7) initializes the reference variable s3 with a reference to a unique instance of type String containing the value "EFG". The expression s4 = s2.substring(0,3) initializes the reference variable s4 with a reference to a unique instance of type String containing the value "EFG". The expression s3 == s4 compares two unique reference values and produces the value false even though s3 and s4 reference two String instances that contain the same value, "EFG". The expression s3 + s4 produces a unique instance of type String containing the value "EFGEFG". Similarly, the expression s4 + s3 produces a unique instance of type String containing the value "EFGEFG". The expression (s3 + s4).equals(s4 + s3) compares the contents of two unique instances of type String that contain the value "EFGEFG". The result of the comparison is true.  
3a  g  h  i  b1  b7  b8  b9  The Math.random method returns a value that is equal to or greater than zero and less than 1.0.  
4Compile-time error  A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block.  
5a  e  -da  -disableassertions  Two command-line switches used to disable assertions are -da and -disableassertions. Remember that all of the letters are lower case.  
6c  d  e  Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error.  
7boolean b1 = true;  There are two primitive boolean values: true and false. Both must be written with lower case letters. Although the C programming language accepts zero as a boolean value, the Java programming language does not.  
8Prints: "ABCDEF"  The reference variable s1 is initialized with a reference to an instance of type String containing the value "ABCDEFG". The expression s2 = s1.substring(0,3) initializes the reference variable s2 with a reference to a unique instance of type String containing the value "ABC". The expression s3 = s1.substring(4,6) creates a unique instance of type String containing the value "EF". The expression c1 = s1.charAt(3) initializes the primitive variable c1 with the value 'D'. The expression String.valueOf(c1) invokes the static valueOf method with an argument of type primitive char and value 'D'. The valueOf method creates a new instance of type String. The value contained by the new instance is "D". The expression s2.concat(String.valueOf(c1)) invokes the concat method on the instance of type String referenced by the variable s2. The instance referenced by s2 contains the value "ABC". The value contained by the argument is "D". The result of the concatenation operation is a new instance of type String containing the value "ABCD". The expression s2.concat(String.valueOf(c1)).concat(s3) invokes the concat method on the previously created instance containing the value "ABCD". The instance referenced by the argument s3 contains the value "EF". The result of the concatenation operation is a new instance of type String containing the value "ABCDEF".  
9None of the above  All methods of the java.lang.Math class are static.  
10Prints: true,false,false  The Vector.elements method returns an Enumeration over the elements of the Vector. The Vector class implements the List interface and extends AbstractList; so it is also possible to obtain an Iterator over a Vector by invoking the iterator or listIterator method.  
11-disableassertions  Two command-line switches used to disable assertions in non-system classes are -da and -disableassertions. Remember that all of the letters are lower case.  
12Prints: fffffffe,fffffffe  For each of the three shift operators, <<, >> and >>>, the shift distance is specified by the right hand operand. If the left operand is of type int, then the shift distance is always within the range 0 to 31, inclusive; and the following expression is always true: (int1 << shift) == (int1 << (shift & 0x1f)). The hexadecimal representation of decimal 31 is 0x1f and the binary representation is 0001 1111. The hexadecimal representation of decimal 33 is 0x21 and the binary representation is 0010 0001. The expression i1 << (33 & 0x1f) is equivalent to (0xffffffff << (0x21 & 0x1f)). Evaluation of the right hand operand of the shift operator produces (0xffffffff << 1). The final result is 0xfffffffe. Similarly, if the left operand is of type long, then the shift distance is always within the range 0 to 63, inclusive; and the following expression is always true: (long1 << shift) == (long1 << (shift & 0x3f)).  
13b  c  The relationship between a class and its superclass is an example of an "is-a" relationship.  The relationship between a class and an object referenced by a field within the class is an example of a "has-a" relationship.  Inheritance is an example of an "is-a" relationship, because the subclass "is-a" specialized type of the superclass. The relationship between a class and an object referenced by a field declared within the class is an example of a "has-a" relationship, because the class "has-a" object.  
14a  d  e  abs  max  min   
15Prints: false,true,false  Both HashMap and TreeMap are subclasses of type AbstractMap; so both implement the Map interface. Neither implements the Collection interface. The TreeMap implements the SortedMap interface, but HashMap does not.  
16Ready   
17a  f  h  LinkedHashMap  HashMap  Hashtable  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 Hashtable, HashMap and LinkedHashMap classes store elements in a hashtable. Elements are accessed using a hashcode that identifies the bucket that contains the element. Access time is therefore not dependent on the number of buckets. As long as the hashcode methods of the elements are properly implemented, the time required to access an element in a hashtable remains constant as the number of buckets in the hashtable grows. In contrast, the TreeMap and TreeSet classes store elements in a sorted order in a tree structure. Access to any element requires walking the tree; so access time depends on the size of the tree.  
18Prints: 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.  
19b  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.  
20LinkedHashSet  The elements of a Map are key/value pairs; so a Map is not a good choice. A List generally accepts duplicate elements. A Set stores a collection of unique elements. Any attempt to store a duplicate element in a Set is rejected. TreeSet stores elements in a sorted order based on the key. HashSet does not sort the elements based on the key. The iteration order of LinkedHashMap and LinkedHashSet is clearly defined. By default, the iteration order of LinkedHashMap and LinkedHashSet is based on the order in which elements were inserted. While a LinkedHashSet rejects duplicate entries, the LinkedHashMap allows duplicate entries to replace old entries.  
21 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. Methods declared within an interface are implicitly public even if the modifier, public, is omitted from the declaration. Within the body of a class declaration, an attempt to implement the method using a weaker access privilege, private, protected or package access, results in a compile-time error. An abstract class that implements an interface is free to override any of the inherited method declarations with another abstract method declaration.  
22a  c  f  Thread.MAX_PRIORITY == 10  Thread.NORM_PRIORITY == 5  Thread.MIN_PRIORITY == 1   
23Prints: AB C D EF  The trim method creates a new String instance with the leading and trailing white space removed.  
24LinkedHashSet  The iteration order of a Set is the order in which an iterator moves through the elements of the Set. The iteration order of a LinkedHashSet is determined by the order in which elements are inserted. When a reference to an existing Set is passed as an argument to the constructor of LinkedHashSet, the Collection.addAll method will add the elements of the existing Set to the new instance. Since the iteration order of the LinkedHashSet is determined by the order of insertion, the iteration order of the new LinkedHashSet must be the same as the iteration order of the old Set.  
25Prints: i1k1i2j1i3j2  A while loop is nested inside of a do loop. The while loop iterates once during the first iteration of the do loop. The body of the while loop does not execute during the final iteration of the do loop.  
26Prints: Blue,Blue  The expression used to assign variable b1 is equivalent to the expression used to assign variable b2. The results demonstrate that the conditional operator (?:) groups from right-to-left.  
27Compile-time error at 2  The method invocation expression a1.m2() generates a compile-time error, because the named method, m2, is declared in class B, but the reference is of the superclass type, A. The reference a1 is of type A; so a1 is able to access only those methods that are declared in class A and subclass methods that override those of class A. Only one method, m1, is declared in A; so a reference of type A can be used to invoke A.m1 or an overriding implementation of m1 that is declared in a subclass of A. Class B extends A and overrides method m1. A reference of type A can be used to invoke method m1 on an instance of type B. Class B declares an additional method, m2, that does not override a method of class A; so a reference of type A can not invoke B.m2.  
28Prints: A0B0J1C1B1J2C2B2  The initialization statement, labeled A, is processed first. The boolean expression, labeled B, is processed before each iteration of the loop. The body of the loop is processed next followed by the update expression, labeled C.  

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