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 BookSurge.com.

Are you a university student studying Java programming? Do you agree that my book would serve as a helpful workbook and companion to be used along with the Java fundamentals textbook that is currently being used in your class? If so, then please ask your professor to consider using my book in future classes.

If you have any questions or comments concerning my mock exams or my book, then please send an e-mail to me at scjpexam2000@yahoo.com.

I would also like to read your response to the following questions.

Answers: Java Programmer Certification Mock Exam
No.AnswerRemark
1b  e  g  h  k  goto  new  finally  const  do   
2None of the above  A tightly encapsulated class may have public mutator methods.  
3b  c  f  notify  notifyAll  wait   
4valueOf  The valueOf method is static. It returns a String representation of the argument. The StringBuffer methods append, delete and insert are not members of the java.lang.String class. A typical trick question will attempt to invoke StringBuffer methods on a String instance.  
5Prints: 10.0  The Math class declares four versions of the abs method; each declares one parameter. The parameter can be of type int, long, float or double. The return type is the same as the argument type. At run-time, the argument might not match the parameter type; so the argument might require an implicit conversion to an acceptable type. If the argument is of type byte, short or char, then it will be promoted to type int.  
6Prints: ABCAB  Instances of type StringBuffer are not immutable. In method m1, the method invocation expression s1.append("B") appends the String literal "B" to the StringBuffer instance referenced by the parameter variable s1. The new value, AB, is printed in method m1. The reference variable s1 declared in the main method refers to the same modified StringBuffer instance. In method m2, the class instance creation expression new StringBuffer("C") creates a new instance of type StringBuffer containing the value C. The assignment expression s1 = new StringBuffer("C") assigns a reference to the new StringBuffer instance to the method parameter variable s1. The value C is printed in method m1. The method local reference variable s1 in the main method remains unchanged by the assignment expression contained in method m2. In the main method, a copy of the reference value contained by the reference variable s1 is passed as an argument to methods m1 and m2. Since StringBuffer instances are not immutable, method m1 is able to change the original instance of the StringBuffer declared in the main method. Since references are passed by value, method m2 can not change the reference variable declared in the main method. Regardless of anything that happens in method m2, the reference variable s1 that is declared in the main method will continue to reference the original StringBuffer instance. Since the content of the original instance was modified by method m1, the new value, AB, is printed in the main method.  
7Prints: -128,127   
8Prints: true,true  An int is a 32-bit signed integral value that is stored in two's complement format. The left most bit is the sign bit. The sign bit is set to one for negative numbers and is set to zero for positive numbers.  
9Prints: true,false  Long.equals overrides Object.equals. The Long.equals method compares the data values contained in the Long instances. The StringBuffer.equals method does not override Object.equals. The StringBuffer.equals method compares the reference values of the instances. Two distinct instances of StringBuffer will not have the same reference values; so the equals method returns false. The StringBuffer.hashCode method typically returns a value that is based on the internal address of the StringBuffer instance. Two instances of StringBuffer will not have the same hash code.  
10a  b  c  d  e  doubleValue  floatValue  intValue  longValue  parseDouble   
11Prints: BbB  The Boolean.valueOf method returns a Boolean instance. The Boolean.booleanValue method returns a primitive. The Boolean.TRUE field is a reference to an instance of type Boolean that wraps the primitive value true.  
12Prints: ffF   
13Set  The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries.  
14Prints: true,false,false  HashSet is a subclass of AbstractSet and AbstractCollection; therefore, it implements the Collection interface. HashMap and Hashtable do not implement the Collection interface. Instead, HashMap extends AbstractMap and implements the Map interface. Hashtable extends Dictionary and implements the Map interface.  
15a  b  c  d  java.lang.Byte  java.lang.Integer  java.util.Vector  java.lang.String  The wrapper classes and the collection classes override the equals and hashCode methods. The String class overrides the equals and hashCode methods, but StringBuffer does not.  
16c  g  The abstract modifier can be applied to a class and a method but not a field. A field is sometimes shared between threads. The volatile modifier is used to force threads to reconcile their own working copy of a field with the master copy in main memory. If a field is declared final then its value does not change and there is no need for threads to reconcile their own working copies of the variable with the master copy in main memory.  
17a  c  e  If an accessible superclass method is static, then any method with the same signature in a subclass must also be staticIf a superclass method is public, then the overriding method must also be publicIf a superclass method is protected, then the overriding method must be protected or public The signature of a method is the name of the method and the number and types of the method parameters. The return type is not part of the method signature. An instance method declared in a subclass overrides an accessible superclass method with the same signature. A static method of a subclass hides--but does not override--an accessible superclass method with the same signature. A compile-time error is generated if a subclass contains a declaration of an instance method that shares the same signature as an accessible static method of the superclass. The modifiers, synchronized, native and strictfp, specify implementation details that the programmer is free to change in a subclass. Similarly, a subclass can override a concrete implementation of a method with an abstract method declaration. A subclass method may not have weaker access than the overridden superclass method. For example, a public method may not be overridden by a private method. However, a subclass method can provide greater access than a superclass method. For example, a protected method can be overridden by a public method.  
18 The modifier, protected, can not be applied to a top level class, but can be applied to a nested class. The synchronized modifier can not be applied to any class, because it is a method modifier. The modifier, volatile, can not be applied to any class; because it is a field modifier.  
19 Please note that this question asks which class declaration does NOT result in a compile-time error. The following modifiers can be applied to a member class: abstract, private, protected, public, static and final. The synchronized modifier can not be applied to any class, because it is a method modifier. The modifiers, transient and volatile, can not be applied to any class, because they are field modifiers.  
20Prints: 1,0,1,0,0,1  The switch statement does not throw an exception; so the switch completes normally. The subsequent statement increments the variable, a; and the try block completes normally. Both of the finally blocks are then executed.  
21Compile-time error  The two-parameter constructor of U does not explicitly invoke the two-parameter constructor of T; therefore, the constructor of U will try to invoke a no-parameter constructor of T, but none exists.  
22Prints: Z.m1 Y.m1  The private fields and methods of the member classes are available to the enclosing class. The private fields and methods of the member classes are also available to other member classes.  
23Prints: AAA  The reference c2 is of the superclass type, A; so it can be used to invoke only the method, m1, declared in class A. The methods that overload the method name m1 in the subclasses, B and C, can not be invoked using the reference c2.  
24Prints: 1433  On the first pass through the loop, the switch expression, x, has the value, -5. None of the case constants are matched, so the statement following the default label is executed causing the value of x to be set to -3. On the second pass, the default case of the switch statement is executed again, and two is again added to the value of x. The new value is -1. On the third pass, the value of x is again incremented, and the new value is +1. After that, the value of x is printed on each pass through the loop. For the last two passes, the value of x is 3.  
25d  e  In this code example a throw statement must be used in place of the assert statement.  Compile-time error  If the default label of a switch statement should not be reached under normal operating circumstances, then the default case becomes a good candidate for the use of an assert statement. If a method is declared with a non-void return type and if no return statement appears after the switch statement, then each case of the switch must have a return statement or a throw statement. The throw statement is used rather than an assert, because the compiler knows that the assert statement is not functional when assertions are disabled.  
26 Please note that this question asks which objects are NOT eligible for garbage collection after method m1 returns. After method m1 returns, the array a1 created on line 1 is not eligible for garbage collection. Method m1 sets all elements of the array to null; so the objects created on lines 2, 3 and 4 are eligible for garbage collection when method m1 returns.  
27Prints: A11  The declaration A11[] a1 = new A11[1] declares a variable a1 that references an array that contains one component of type A11. The declaration A11[][] a2 = new A11[2][] declares a variable a2 that references an array that contains two components of type A11[]. In other words, the array referenced by a2 contains two reference variables, and each is able to reference a subarray. The initial value of the subarray references is null. The size of the subarrays has not been specified. The declaration A11[][][] a3 = new A11[3][][] declares a variable a3 that references an array that contains three components of type A11[][]. In other words, the array referenced by a3 contains three reference variables, and each is able to reference a subarray. The initial value of each subarray reference is null. The dimensions of the subarrays have not been specified. At line 5, a reference to the array referenced by a1 is assigned to each of the two components of the array referenced by a2, a2[0] = a2[1] = a1. At line 6, a reference to the array referenced by a2 is assigned to each of the three components of the array referenced by a3, a3[0] = a3[1] = a3[2] = a2. In other words, after line 6, each component of the array referenced by a3 is a reference to the array referenced by a2, and each element of the array referenced by a2 is a reference to the array referenced by a1, and the array referenced by a1 contains a reference to an instance of class A11. Every element of the multi-dimensional array referenced by a3 contains a reference to a single instance of class A11. The print method invokes the toString method on the instance, and produces the output, A11
28Prints: 3122  The arguments that appear in the class instance creation expression of an anonymous class are passed to a constructor of the superclass.  
 
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.