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
1Compile-time error  A static method can not access a non-static variable.  
2None of the above   
3None of the above  All of the declarations are legal. String b is a single quote followed by the letter A followed by another single quote. String c is the letter A. String d is the Unicode character that is represented by the hexadecimal value D7AF. String literals are covered in section 3.10.5 of the JLS.  
4GFC507  Class GFC507 has a public field; so it is not tightly encapsulated.  
5Prints: int,int,long  The Math class declares four versions of the min method; each declares a pair of parameters of the same type. The parameter pair can be of type int, long, float or double. The return type is the same as the argument types. At run-time, the arguments might not match the declared parameter types; so one argument or both might require an implicit conversion to an acceptable type. If both arguments are of type byte, short or char, then both will be promoted to type int. If only one argument is of type byte, short or char, then it will be promoted to the type of the other argument. If both arguments are of type int, long, float or double but the types differ, then a primitive widening conversion will be applied to one of the two arguments.  
6Prints: true,false  Integer.hashCode overrides Object.hashCode. The Integer.hashCode method calculates the hash code based on the value contained in the Integer instance. The StringBuffer.hashCode method does not override Object.hashCode. 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.  
7Run-time error  An argument of type String must represent a floating-point value. The argument "0x10" represents a hexadecimal integer literal; so a NumberFormatException would be thrown at run-time.  
8c  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.  
9Compile-time error  The array initializer, [[1,2],[3,4,5],[6,7,8,9]], generates a compile-time error, because the curly braces have been replaced by square brackets. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}.  
10None of the above  A method, field, and a nested class can share the same name, because they are used in different contexts and use different lookup procedures. Please note that class names usually begin with an upper case letter while method and field names usually begin with a lower case letter. Also note that a nested class can not share the same name with its enclosing class; however, a method and field can share a name with the enclosing class. Even so, it is not a good idea to name a method with the name of the enclosing class, because it could be confused with a constructor.  
11Prints: 0,0,0,1,1  The first catch clause has a parameter e of type Level3Exception, so the first catch clause is able to catch any exception type that is assignable to type Level3Exception. Since Level2Exception is the superclass of Level3Exception, an instance of Level2Exception is not assignable to a catch clause parameter of type Level3Exception. Similarly, Level1Exception is also a superclass of Level3Exception, so an instance of Level1Exception is not assignable to a catch clause parameter of type Level3Exception. The only exception type that can be caught by the first catch clause is a Level3Exception. The second catch clause has a parameter e of type Level2Exception, so the second catch clause is able to catch a Level2Exception. The Level1Exception is the superclass of Level2Exception. An instance of Level1Exception is not assignable to a catch clause parameter of type Level2Exception, so the second catch clause can not catch a Level1Exception. Since a Level3Exception is a subclass of Level2Exception an exception of type Level3Exception is assignable to a catch clause parameter type Level2Exception. All exceptions of type Level3Exception will be caught by the first catch clause, so the second catch clause in this program will not have an opportunity to catch a Level3Exception. The third catch clause has a parameter e of type Level1Exception, so the third catch clause is able to catch a Level1Exception. The exceptions of type Level2Exception and Level3Exception are assignable to the catch clause parameter of the third catch clause, but the exceptions of those subclass types will be caught by the first two catch clauses. The switch statement throws a Level1Exception. The try block completes abruptly as control passes to the third catch block where d is incremented. The finally block is also executed, so f is incremented.  
12None of the above  All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. No other modifiers can be applied to a field declaration within an interface.  
13Compile-time error  The type of the reference color2 is Red. Since Red is not a subclass or a superclass of Blue, the expression color2 instanceof Blue is rejected at compile-time. Please note: The expression, x instanceof T, produces a compile-time error whenever the cast expression (T)x produces a compile-time error. If the program had been able to compile and run, the expression color1 instanceof Color would evaluate to true at run-time. The reference color1 refers to an instance of type Red. Since Red is a subclass of Color, the expression color1 instanceof Color would evaluate to true at run-time. The expression, color1 instanceof Blue would evaluate to false at run-time. The reference, color1, is of type Color. Since Color is a superclass of Blue, the expression, color1 instanceof Blue, is accepted at compile-time. The type of the object instance referenced by color1 is Red. Since Red is not Blue or a subclass of Blue, the expression, color1 instanceof Blue, would be false at run-time.  
14None of the above  If a superclass method is not private and is accessible to code in a subclass, then any subclass method that has the same signature as the superclass method must also have the same return type. In other words, if a superclass method is overridden or hidden in a subclass, then the overriding or hiding subclass method must have the same return type as the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods need not have the same return type. Class A declares one method: The name is m1 and the single parameter is of type String. Class B extends A and declares six methods that overload the name m1. The method, B.m1(String s1), overrides the superclass method, A.m1(String s1). The overriding subclass method must have the same return type as the superclass method, but the methods that overload the name m1 are free to have different return types. An overriding subclass method is not permitted to throw any checked exception that is not listed or is not a subclass of any of those listed in the throws clause of the superclass method. No such restriction applies to method overloading. If two methods share an overloaded method name but not the same parameter list, then the two methods are free to have differing throws clauses.  
15Compile-time error  The StringBuffer class has an insert method, but the String class does not. A compile-time error is generated due to the attempt to invoke the insert method on an instance of type String.  
16Prints: long,float,double  The Math class declares four versions of the min method; each declares a pair of parameters of the same type. The parameter pair can be of type int, long, float or double. The return type is the same as the argument types. At run-time, the arguments might not match the declared parameter types; so one argument or both might require an implicit conversion to an acceptable type. If both arguments are of type byte, short or char, then both will be promoted to type int. If only one argument is of type byte, short or char, then it will be promoted to the type of the other argument. If both arguments are of type int, long, float or double but the types differ, then a primitive widening conversion will be applied to one of the two arguments.  
17Prints: ABC  In all three cases, the object passed to method m1 is an instance of class C; however, the type of the reference is different for each method. Since fields are accessed based on the type of the reference, the value printed by each method is different even though the same instance is used for each method invocation.  
18 A member interface is implicitly static; therefore, it can not be declared as a member of a non-static nested class.  
19The process of executing a synchronized method requires the thread to acquire a lock  The synchronized modifier can not be applied to a class. A method that overrides a synchronized method does not have to be synchronized. If a thread invokes a synchronized instance method on an instance of class A, then the thread must acquire the lock of that instance of class A. The same is not true for synchronized static methods. A synchronized static method is synchronized on the lock for the Class object that represents the class for which the method is a member.  
20Prints: false,false,true  At run-time, the expression a+b is evaluated four times. Each evaluation produces a new String instance containing the value "AB". Each of the four instances has a unique reference. If any two of the four instances appear as the operands of the equality operator, then the result is always false. The left and right operands of the equality expression (a+b)==(a+b) reference unique String instances. Since the two references are not the same, the equality expression produces the value false. Similarly, the expression c==d produces the value false. Since the contents of the String instances referenced by c and d are the same, the method invocation expression c.equals(d) produces the value true.  
21Prints: 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.  
22Prints: byte byte boolean  Variable b1 was initialized by the first method invocation statement, so the second method invocation statement does not result in a compile-time error. The assignment expression, b1 = 1, initializes variable b1 with the value 1, and the same value is passed as an argument to method m1(byte b1). The method invocation expression, m1(b1), invokes the same method, m1(byte b1). The argument of the third method invocation expression, m1(b1 == 1), is the result of the equality expression, b1 == 1. The type of the result and the argument is boolean, so the invoked method is m1(boolean b1).  
23Prints: A0B0A1B1A1B2  Class A is the enclosing class of the inner class B. An instance of class B must be associated with an enclosing instance of class A. In a static context such as the main method, instantiation of a named inner class requires the use of a qualified class instance creation expression of the form Reference.new Identifier(ArgumentListopt). The reference could be provided by a reference variable of the type of the enclosing class, or it could be provided by another class instance creation expression. At line 2, the qualified class instance creation expression new A().new B() first creates a new instance of the enclosing class A, then it creates an instance of B. The new instance of A is the first instance created; so the name is A0. The new instance of B is the first instance created; so the name is B0. At line 3, the qualified class instance creation expression a1.new B() creates an instance of B that is associated with a previously existing instance of class A that is referenced by variable a1. The instance of class A referenced by variable a1 is the second instance created so the name is A1. The new instance of B is the second instance created; so the name is B1. At line 4, a new instance of B is created and associated with the instance of A this is referenced by variable a1.  
24c  d  e  f  The Ready state to the Running state  The Running state to the Not-Runnable state  The Running state to the Ready state  The Not-Runnable state to the Ready state  A dead thread can not be restarted.  
25Prints: m3Cm2Bm1A  The method invocation expression, m3(new C()), invokes method m3(C x), because the argument type matches the parameter type of the method declaration exactly. Method m3 uses the parameter as the argument of the next invocation expression, m2(x). Of the two overloaded versions of m2, the most specific is invoked, m2(B x). Type B is a subclass of A, so any invocation of m2(B x) could be handled by m2(A x) without causing a compile-time type error. For that reason, m2(B x) is more specific than m2(A x).  
26Prints: 121212  This trick question has a for loop nested inside of a do loop. For each iteration, the values of i and j are as follows: (1,0)(2,0)(1,1)(2,1)(1,2)(2,2).  
27b  e  f  The flow of control does not reach a particular point in the program.  The default case of a switch statement is not reached.  The else block of an if/else statement is not reached.  A control-flow invariant is placed at a point in the program that the programmer assumes will never be reached. Two examples are the default case of a switch statement or the else block of an if/else statement. It makes no sense to use an assert statement to verify that the flow of control does reach a particular point in the program, because it is unlikely that an assertion error is helpful when the program is found to be functioning correctly. An assert statement placed at the beginning of a method is generally used to check a precondition. An assert statement that is placed at the end of a method to check the state of some variables is generally said to be checking a post condition. However, it is also possible that an assert statement placed at the end of a method might also be checking a control-flow invariant. The correct term depends on the usage.  
28Ba, B1, B2, Bb  Class J declares two static member variables named bc and i. The type of i is int, and the value is initialized to 1. The type of bc is B. Inside the main method of class J, the method invocation expression m1(new B("Ba")) invokes method m1. The argument is the class instance creation expression new B("Ba"). The constructor of class B assigns the argument value to the instance variable called name, so a new instance of class B named Ba is created. The reference to the new instance of class B is the argument that is passed to method m1. The body of method m1 contains two statements. The first contains the assignment expression bc = b that assigns the value of the method parameter b to the static member variable bc, so bc now references the instance of class B named Ba. The second statement in the body of m1 is a return statement with the class instance creation expression new B("B" + i++). For this first invocation of method m1, the argument appearing in the class instance creation expression is the String value B1. The reference to the new String is returned by method m1. The returned value is assigned to the local variable x. Inside the main method, the declaration of the local variable y contains another invocation of method m1. The argument appearing in the method invocation expression m1(new B("Bb")) is the class instance creation expression new B("Bb"), so the argument value for the invocation of method m1 is a reference to a new instance of class B named Bb. Inside the body of method m1, the reference to the new instance of class B named Bb is assigned to the static member variable Bc. At that point, the instance of class B named Ba becomes eligible for garbage collection. Method m1 returns a reference to a new instance of class B named B2. There is no guarantee that the garbage collector will run before the print statement is invoked. If it does run, then the instance named Ba could be finalized causing the name to be printed.  
 
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.