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  d  f  Encapsulation enhances the maintainability of the code.  A tightly encapsulated class allows access to data only through accessor and mutator methods.  A tightly encapsulated class might have mutator methods that validate data before it is loaded into the internal data model.  The data members of a tightly encapsulated class are declared private; so changes to the data model are less likely to impact external code. Access to internal data can be provided by public accessor (i.e. get) and mutator (i.e. set) methods. The mutator methods can be used to validate the data before it is loaded into the internal data model. The use of accessor and mutator methods is likely to increase the size of the code and slow execution speed.  
2e  g  sleep  yield   
3Prints: 1,2  The Math.round method returns the floor of the argument value plus 0.5. If the argument is of type float, then the return type is int. If the argument is of type double, then the return type is long.  
4b  c  e  Compile-time error at 1  Compile-time error at 2  Compile-time error at 4  The Byte class has only two constructors: one accepts a primitive byte; the other accepts a String. The argument that appears in the class instance creation expression new Byte(1) is of type int, but the compiler will not implicitly narrow it to type byte. The argument that appears in the class instance creation expression new Byte('2') is of type char, but the compiler will not implicitly narrow it to type byte. At line 4, the result of the additive expression is of type int, but the variable is of type byte. The assignment expression generates a compile-time error.  
5Compile-time error  The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. The class instance creation expression new Integer(primitiveLong) generates a compile-time error, because the compiler will not apply an implicit narrowing conversion to the argument. For the same reason, the class instance creation expression new Integer(primitiveFloat) generates a compile-time error.  
6Compile-time error  Long has two constructors: one accepts an argument of type primitive long; the other accepts an argument of type String. The class instance creation expression new Long(i1) generates a compile-time error, because there is no constructor that accepts a reference to an instance of type Long.  
7Compile-time error  The Double constructor is overloaded: one accepts an argument of type primitive double; the other accepts an argument of type String. There is no constructor that accepts a reference to an instance of type Double.  
8Prints: false,true,true  Four instances of type Boolean containing the value true are created. The equality expression b1==b2 evaluates to false, because the unique instances of type Boolean have different reference values. The instance method Boolean.booleanValue returns a copy of the primitive boolean value that is contained by the instance. Since the boolean values of b1 and b2 are the same, the result of the equality expression b1.booleanValue()==b2.booleanValue() is true. The equals method compares the values of the primitives that are wrapped by the Boolean instances. Since the wrapped primitive values are the same, the result of the method invocation expression b3.equals(b4) is true.  
9Prints: 1.0,8.0,16.0,1.0,1.0,1.0  The Float constructor is overloaded: one version accepts a primitive of type float; one accepts a primitive of type double; one accepts a String representation of a floating-point literal. All numeric values can be promoted to type double; so all numeric values are accepted by the float constructor.  
10None of the above  None of the String arguments would generate a compile-time error, but two would generate a run-time error. The argument 1.0 would generate a run-time error, because a floating-point value is not acceptable. The argument 0x1 would generate a run-time error, because a hexadecimal integer literal is not acceptable. The argument must be a decimal integer literal. The leading 0 of the argument 011 is ignored and the argument is interpreted as a decimal integer literal.  
11HashSet  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. Adding and removing an element in a TreeSet involves walking the tree to determine the location of the element. A HashSet stores the elements in a hashtable; so elements in a HashSet can be accessed almost as quickly as elements in an array as long as the hash function disperses the elements properly. Although the LinkedHashSet is not among the answer options it could arguably satisfy the requirements. However, the put and remove methods of the LinkedHashSet are a little slower than the same methods of the HashSet due to the need to maintain the linked list through the elements of the LinkedHashSet.  
12Prints: false,true,true  The Map interface does not extend Collection. The reference variable a1 refers to an instance of HashMap; so the relational expression a1 instanceof Collection returns the value false. The List and Set interfaces extend the Collection interface; so both List and Set could be cast to type Collection without generating a ClassCastException. Therefore, the second and third relational expressions return true.  
13true  If two objects are equal according to the equals method, then the hashcodes produced by the hashCode method must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. It is preferable that unequal objects have different hashcodes, but that is not always possible. Since the hash code value is a 32 bit primitive int, it is not possible to produce a unique hash code for each value of a primitive long.  
14Prints: 2,5,8  Arrays a1, a2 and a3 all contain 3 integers. The first element of the array has an index of 0 so an index of one refers to the second element of each array.  
15a  b  c  The compiler will create a default constructor if no other constructor is declared.  The default constructor takes no arguments.  If a class A has a direct superclass, then the default constructor of class A invokes the no-argument constructor of the superclass.  If no constructor is declared explicitly, then the compiler will implicitly insert a default constructor. The default constructor takes no arguments. The primordial class Object has no superclass; so the default constructor of type Object does not invoke a superclass constructor. If a class A has a direct superclass, then the default constructor of class A will invoke the no-argument superclass constructor. It is unlikely that the real exam would try to trick you with a question that requires you to know that the constructor of type Object does not invoke a superclass constructor. For the purposes of the real exam, it might be safer to overlook that particular unique feature of type Object. If a subclass constructor attempts to invoke the no-argument superclass constructor when none exists, then a compile-time error is generated. The access modifier implicitly assigned to the default constructor is the same as that assigned to the class. The default constructor does not have a throws clause. Consequently, a compile-time error is generated if the no-argument constructor of the superclass has a throws clause.  
16transient  A transient field is not part of the persistent state of an object, so it is not serialized. A static field is also not part of the persistent state of an object, and also is not serialized.  
17a  b  e  f  g  final  private  static  synchronized  native  A final or private method can not be overridden, and can not be abstract. An abstract method declaration provides no implementation of the method, and all implementation details are left to the overriding method in the subclass. The synchronized modifier specifies an implementation detail that can be omitted from the declaration of an overriding method of a subclass, so it makes no sense to allow the use of the synchronized modifier in an abstract method declaration.  
18Compile-time error at 3.  There is a compile-time error at line 3. The long type variable, l, can not be assigned to type int without an explicit cast. The statement, "return l;", is a return statement with an expression, l. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m3, is int. The type of the variable, l, is long, so an explicit cast is needed to perform the narrowing primitive conversion, "return (int)l;". The declarations of methods m1 and m2 do not generate compile-time errors, because the types of the expressions contained in the return statements are assignable to type int. Widening conversions from types byte, char, or short to type int do not require an explicit cast.  
19a  d  e  h  class A extends ObjectThe compiler will insert a default constructor implicitly.  The default constructor has no throws clause.  The default constructor invokes the no-parameter constructor of the superclass.  Field i1 and m1 both have package access. When no constructor is declared explicitly the compiler will insert one implicitly. The implicitly declared default constructor will have the same access privileges as the class. In this case, the class is public, so the default constructor is also public. The default constructor accepts no parameters and throws no exceptions.  
20a  c  e  f  g  h  abstract  final  private  protected  public  static  The access modifiers, public, protected and private, can not be applied to a local class. The protected and private access modifiers can be applied to member classes, but not to a top level class. The public modifier can be applied to a top level class or a member class, but not a local class. The static modifier can be applied to a member class, but not to a local class or top level class. The keywords extends and implements are not modifiers. The synchronized modifier can be applied to a method, but not to a class. The modifiers, transient and volatile, can be applied to a field, but not to a class.  
21Compile-time error.  The keyword, case, is missing from the case labels.  
22Prints: 0,0,1,1,0,1  The nested catch clause is able to catch a Level2Exception or any subclass of it. The switch statement throws a Level1Exception that can not be caught by the nested catch clause; so the nested finally block is executed as control passes to the first of the two outer catch clauses. The outer finally block is executed as control passes out of the try statement.  
23Prints: ABC  Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression d1.m1(c1) uses reference d1 of type D to invoke method m1 on an instance of type D. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression d1.m1(c2) invokes method m1(B b). The declared type of c3 is type C; so the method invocation expression d1.m1(c3) invokes method m1(C c).  
24Prints: BDE  The substring method returns a new String instance that is a substring of the original String instance. The single parameter form of the substring method creates a new String that begins at the index specified by the argument value. The two parameter form creates a substring that starts at the index of the first parameter and ends at the index of the second parameter. The character at the start index is included in the substring, but the character at the end index is not.  
25Prints: true,false  String.equals overrides Object.equals. The String.equals method compares the contents of the String instances--not the references. Since the contents of s1 and s2 are the same, the method invocation expression s1.equals(s2) produces the value true. The StringBuffer.equals method does not override Object.equals. The StringBuffer.equals method compares the reference values--not the contents of the StringBuffer instances. Since the reference values sb1 and sb2 are different, the method invocation expression sb1.equals(sb2) produces the value false.  
26Prints: 2,3,5  Each of the three array variable declarations, a1, a2 and a3, is different in terms of the position of the square brackets, but each declares a variable of type int[][]. Each of the three declarations contains an array initializer. In each case, the initializer could be simplified as follows: {{1,2},{3,4,5}}. The initializer creates an array containing two components, and each is a reference to an array containing components of type int. The size of each of the subarrays is different: The size of the first is 2 and the second is 3. The array access expression, a1[0][1] = a[1st subarray][2nd component] = 2.  
27a  d  Assertions should not be used to validate arguments passed to public methods.  Assertions should not be used to perform processing that is required for the normal operation of the application.  Assertions may be enabled or disabled at run time. Since assertions are not always enabled, they should not be used to validate the parameters of public methods. Parameter checking is typically published in the API specification of a method and must be enforced even when assertions are not enabled. Rather than use an assertion, an appropriate runtime exception should be thrown such as IllegalArgumentException, IndexOutOfBoundsException, or NullPointerException. However, an assertion may be used to validate the parameters of a nonpublic method. Since assertions are not always enabled, an assertion should not be used to perform operations that are required for the normal operation of the program. For example, the boolean expression of an assertion should not be used to produce the side effect of incrementing a variable that controls a loop statement. If assertions are disabled then the loop is unlikely to function as intended. Section 14.20 of the Java Language Specification defines "unreachable" statements. If an assert statement is "unreachable" as defined by the JLS, then a compile-time error is generated. In contrast, a programmer may believe that some points in the code will not be reached as a result of design assumptions. For example, a programmer may believe that the default case of a switch statement will never be reached. An assertion can be placed in the default case to verify the behavior of the switch statement.  
28Indeterminate.  Since we don't know what method m2 might be doing, we can not know if the objects are eligible for garbage collection. Suppose that method m2 is declared inside of a class that also contains 10 instance variables (instance variables are non-static member fields) that are references to instances of class A. The argument that appears in the method invocation expression m2(q1) is a reference to an instance of class Q. Suppose that m2 saves each argument value in one of the ten instance variables or in an element of an array of type Q[]. When the loop in method m1 runs to completion, each instance of class Q would still be referenced by a one of the ten instance variables. Since the instance variables would continue to reference each instance of class Q when line 3 is executed, none of the instances would be eligible for garbage collection at that point. A second possibility is that method m2 does not save the reference values. In that case, all of the instances that were created inside the loop would be eligible for garbage collection when line 3 is executed.  
29Compile-time error  The instance variables x and y in class A are private, so they are not accessible to the anonymous subclasses. If you want to access the private variables of a superclass, then you will need to add accessor methods to the superclass such as getX and getY.  
 
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.