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
1Prints: 1,3  Variables of primitive type are passed to methods by value: Only a copy of the value of the variable is passed to the method. While the method works with a local copy of the variable, the original variable remains unchanged by any actions performed on the method parameter. For that reason, method m1 does not change the contents of the variable y in the main method or the class variable x.  
2None of the above  One answer option reads as follows: "Accessor methods are used to prevent fields from being set with invalid data." The answer would be correct if the word "Accessor" were replaced by the word "Mutator". Accessor methods are used to read data members; mutator methods are used to set data members. The mutator methods can validate the parameter values before the values are used to change the state of the internal data model.  
3a  e  h  join  sleep  wait   
4a  c  d  append  delete  insert  The StringBuffer class has methods named append, delete and insert, but the String class does not. A typical trick question will attempt to invoke StringBuffer methods on a String instance.  
5Prints: 1.0,2.0  The Math.ceil method name is not overloaded. The Math.ceil method accepts an argument of type double and returns a double. The returned value is the smallest whole number that is greater than or equal to the argument.  
6Prints: false,true  The expression b1==b2 compares the references of two instances of Byte. The result is false, because the instances are distinct. The expression b1.equals(b2) compares the contents of two instances of Byte. The result is true, because the two instances contain the same value.  
7None of the above  The binary representation of 256 is one bit that is set to one followed by eight bits that are set to zero. When 256 is converted to an eight bit byte value, the bit that is set to one is lost and only the bits that are set to zero remain. When 256 is converted to a short, no information is lost; so the value remains 256.  
8Prints: long  The Long.parseLong method returns a primitive long.  
9Prints: true,true,true  NaN is the only value that is not equal to itself. The Double.isNaN method returns the result of the expression (v != v).  
10Prints: true,false,false  The Boolean constructor is overloaded: one version accepts a primitive boolean argument; the other accepts a String. If the String value is the word true, then the new Boolean instance will contain the value true. Both upper and lower case letters are acceptable. If the String contains any word other than true or if the reference is null, then the new instance will contain the value false.  
11Compile-time error at 1  The Float.toString method is overloaded: one declares no parameters and returns the value wrapped by the Float instance; the other accepts a primitive of type float. The literal, 1.0, is of type double and can not be implicitly narrowed to type float.  
12false,true,true  The equality expression s1==s2 compares the reference values of two distinct instances of type Short. Since the instance are distinct, the equality expression is false. The expression s1.equals(s2) compares the values of two instances of type Short. Since both instances contain the value 1, the returned value is true. The expression a1==b1 compares the hash codes of two instances of Short. The result is true, because the two instances contain the same value.  
13LinkedList  ArrayList and Vector both use an array to store the elements of the list. When an element is inserted into the middle of the list the elements that follow the insertion point must be shifted to make room for the new element. The LinkedList is implemented using a doubly linked list; an insertion requires only the updating of the links at the point of insertion. Therefore, the LinkedList allows for fast insertions and deletions.  
14Prints: true,true  TreeSet implements the Set interface and the SortedSet interface.  
15a  d  (a.hashCode() == b.hashCode())  (!g.equals(h))  If two objects are equal according to the equals method, then the hashcodes must also be equal. If two objects are not equal according to the equals method, then the hashcodes may or may not be equal. If two objects have the same hash code, then the objects may or may not be equal. If two objects have different hashcodes, then the objects must not be equal.  
16Compile-time error at 3.  The compiler creates a constructor for class C implicitly. The implicitly created constructor accepts no parameters and has no throws clause. The constructors for class B and class C both invoke the constructor for A. The constructor for class A declares Exception in the throws clause. Since the constructors for B and C invoke the constructor for A implicitly, both B and C must declare Exception in their throws clause. A compile-time error is generated at marker 3, because the default constructor does not declare Exception in the throws clause.  
17a  b  c  d  e  f  A variable that is local to a method can not be accessed from outside of the class, so the access modifiers are not useful and not legal. A variable that is local to a method can not be part of the persistent state of an object, so the transient modifier is not useful and not legal. Local variables can not be shared between threads, so the volatile modifier is not useful and not legal. A local variable can be declared final to prevent its value from being assigned more than once. If the value of the variable needs to be accessed from a local class or an anonymous class, then the local variable or method parameter must be declared final.  
18The keyword, super, may be used in the body of a static method.  The keyword, this, refers to the instance on which the method has been invoked. A static method--also known as a class method-- is not invoked on a particular instance of an object, but is instead invoked on the class. Since a static method is not associated with a particular instance, an attempt to use the keyword, this, within the body of a static method results in a Compile-time error. Similarly, the keyword, super, can not be used within the body of a static method.  
19d  e  f  Compile-time error at 2.  Compile-time error at 3.  Compile-time error at 4.  At line 2, the statement, "return i;", contains the expression, i. The enclosing method, m2, is declared void. The return statement generates a compile-time error, because it contains an expression. At line 3, the statement, "return;", does not contain an expression. The enclosing method, m3, is declared with the result type, int. The return statement generates a compile-time error, because it does not contain an expression that produces a value that is assignable to the declared result type.  
20 The length member of the array type is an attribute. A compile-time error is generated as a result of the attempt to access length as though it were a method.  
21Prints: String  A method invocation conversion can widen an argument of type String to match a method parameter of type Object, so any argument that can be passed to m(String x) without generating a compile-time type error can also be passed to m(Object x). For that reason, we can say that m(String x) is more specific than m(Object x). The argument of the method invocation expression, m(null), is of type null and can be converted to either type String or Object by method invocation conversion, so both methods, m(String x) and m(Object x), are applicable. The more specific of the two, m(String x), is chosen over the less specific, m(Object x).  
22Prints: P.printS1 Q.printS2  Suppose a method m1 is invoked using the method invocation expression m1(). If m1 is a static member of the class where the invocation expression occurs, then that is the implementation of the method that is invoked at run-time regardless of the run-time type of the object. If m1 is non-static, then the selected implementation is determined at run-time based on the run-time type of the object. The program invokes method printS1S2 on an instance of class Q. The body of method printS1S2 contains two method invocation expressions, printS1() and printS2(). Since method printS1 is static, the implementation declared in class P is invoked. Since printS2 is non-static and the run-time type of the object is Q, the invoked method is the one declared in class Q.  
23Compile-time error  An instance of class Z must be associated with an enclosing instance of class D. In a static context, an unqualified class instance creation expression of the form new ClassType(ArgumentListopt) ClassBodyopt can not be used to create an instance of an inner class. Instead, a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt) ClassBodyopt is required to create an association between an instance of the enclosing class and the new instance of the inner class. The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by a class instance creation expression such as new D(). In a static context, the expression new D().new Z() can be used to create the new instance of the enclosing class D and the new instance of the inner class Z.  
24Prints: A  The reference c1 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 c1. A method invocation conversion promotes the argument referenced by c2 from type C to type A, and the method declared in class A is executed. Class A declares only one method, m1. The single parameter is of type A. Class B inherits the method declared in class A and overloads the method name with a new method that has a single parameter of type B. Both methods sharing the overloaded name, m1, can be invoked using a reference of type B; however, a reference of type A can be used to invoke only the method declared in class A. Class C inherits the methods declared in classes A and B and overloads the method name with a new method that has a single parameter of type C. All three methods sharing the overloaded name, m1, can be invoked using a reference of type C; however, a reference of type B can be used to invoke only the method declared in class B and the method declared in the superclass A. The method invocation expression c1.m1(c2) uses reference c1 of type A to invoke method m1. Since the reference c1 is of type A, the search for an applicable implementation of m1 is limited to class A. The subclasses, B and C, will not be searched; so the overloading methods declared in the subclasses can not be invoked using a reference of the superclass type.  
25Prints: true,false  The StringBuffer class does not override the equals and hashCode methods of the Object class. The Object.equals method does not return the value true unless the argument is a reference to the same object on which the method is invoked. For example, the method invocation expression obj1.equals(obj2) only produces the value true when obj1 == obj2 is also true. The Object.hashCode method tends to return distinct hashcode values for distinct objects regardless of the internal contents of the object. Suppose that the reference variables sb1 and sb2 are of type StringBuffer. The expression sb1.hashCode() == sb2.hashCode() will not produce the value true unless the expression sb1 == sb2 is also true. The String class does override the equals and hashCode methods of the Object class. The String.hashCode method returns a hashcode value that is computed based on the contents of the String object. Suppose that the reference variables s1 and s2 are of type String. The expression s1.hashCode() == s2.hashCode() must produce the value true anytime the method invocation expression s1.equals(s2) produces the value true.  
26Compile-time error  Variable b1 is initialized to false, because it is a class member. The array component array[0] is initialized to the default value, false, of the array type, boolean[], even though the array is declared locally. Local variable b2 is not initialized, because it is local. A compile-time error is generated by the statement that attempts to print the value of b2.  
27Prints: 2,3,4,0,  The array initializer, {{1,2},{3,4,5},{6,7,8,9},{}}, creates an array containing four components, and each is a reference to an array of type int[]. The size of the first subarray is 2, the second is 3, the third is 4, and the fourth is zero. While the components of the array referenced by a1 are of type int[], the elements of the array referenced by a1 are of type int
28Prints: 3,1  Method m1 is not able to change the value of the local variables that are declared in the main method and serve as the arguments in the method invocation expression. However, method m1 is able to modify the contents of the arrays that are referenced by the method parameters.  
29Compile-time error  An anonymous class can extend Object and implement an interface, or the anonymous class can extend a named class including Object. An anonymous class declaration can not have an implements clause. In this case, the declaration of the anonymous class referenced by a1 generates a compile-time error as a result of the attempt to extend class A and implement interface B.  
 
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.