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  i  j  k  catch  instanceof  const  goto  import  transient   
2 The literal 1.0 is a double and can not be used to initialize a float without an explicit cast.  
3Prints: 1, 1, 0, 1  Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1 reference, the change is visible in the r2 instance of class Red.  
4a  b  c  d  g  i  j  k  byte  short  int  long  float  double  boolean  char   
5c  d  The compiler interprets \u000a as a line terminator. The escape sequence \n should be used instead. Similarly, \u000d is interpreted as a line terminator. The escape sequence \r should be used instead.  
6static  All field declarations within an interface are implicitly public, static and final. Use of these modifiers is redundant but legal. A field that is declared final can not also be declared volatile; so a field of an interface can not be declared volatile. The modifier, synchronized, is never applicable to a field.  
7Prints: 2,true  Suppose the left operand were divided by the right operand. The remainder operator returns the remainder of the division operation. For integral types, the identity, (y == ((y/x)*x+(y%x))), is always true.  
8None of the above  Class GFC503 is not tightly encapsulated; so no subclass of GFC503 is tightly encapsulated.  
9Compile-time error  The StringBuffer class has an append method, but the String class does not. A compile-time error is generated due to the attempt to invoke the append method on an instance of type String.  
10Prints: int,int,int  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.  
11a  b  intValue  parseInt  Integer.valueOf returns an instance of the Integer wrapper class.  
12e  g  parseDouble  valueOf   
13List  The Map interface organizes entries as key/value pairs. A list generally allows duplicate entries. A Set rejects duplicate entries. A List allows entries to be accessed using an index.  
14Prints: true,true,false  HashMap does not implement the Collection interface.  
15Compile-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 parentheses. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}.  
16 A method and a field can share the same name, because they are used in different contexts and use different lookup procedures. They can even share the same name with the class in which they are declared. Please note that class names usually begin with an upper case letter while method and field names usually begin with a lower case letter. A nested class can not share the same name with its enclosing class.  
17Prints: C  The first if statement initializes the value of b. The expression, b = false, appears to be testing the value of b, but it is really setting the value of b. Always look carefully at the boolean expression of an if statement to verify that the expected equality operator (==) has not been replaced by the simple assignment operator (=).  
18Compile-time error  The throws clause of White.m1 declares a ColorException, but the catch clause in the main method catches only a subclass of ColorException. The result is a compile-time error.  
19a  d  e  f  public  abstract  static  final  A class that is declared within an enclosing interface is implicitly public and static; so the access modifiers, protected and private, are not applicable.  
20Prints: CBA  Class C extends B, and B extends A. The static method C.m hides method B.m, and B.m hides A.m. In the method invocation expression c.m(), the compile-time type of the reference c is C. A static method is invoked based on the compile-time type of the reference; so the method invocation expression c.m() invokes the method m declared in class C. The compile-time type of the reference b is B; so the method invocation expression b.m() invokes the method m declared in class B. The compile-time type of the reference a is A; so the method invocation expression a.m() invokes the method m declared in class A.  
21 The non-static members of an enclosing class are not directly available to a static nested class. From within StaticNested, the non-static members of the enclosing class can not be referred to by a simple name. Instead, a qualified name is required. Suppose a reference variable r1 refers to an instance of the enclosing class Red. Then the instance member c of the enclosing class instance referenced by r1 could be accessed using the qualified name r1.c.  
22b  e  f  g  h  i  A compile-time error occurs if the expression produces a value of any primitive type  If execution of the block completes normally, then the lock is released  If execution of the block completes abruptly, then the lock is released  A thread can hold more than one lock at a time  Synchronized statements can be nested  Synchronized statements with identical expressions can be nested   
23Prints: true,false,true  The left operand of the instanceof operator must be null or a reference to an instance of an Object or a subclass of Object. The right operand of the instanceof operator must be a class type, interface type or array type. If the left operand is a reference to an instance of the type specified by the right operand or if the left operand is a reference to an instance of a subclass of the type specified by the right operand, then instanceof returns true.  
24a  d  f  h  With assertions enabled it prints an error message.  With assertions disabled it prints: true,true,false  The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2 or b3, to be true.  The assert statement is being used to check an internal invariant--something that the programmer assumes to be true at a particular point in the program.  Method m1 has a series of if/else statements. The first if statement is processed if none of the booleans are true. The second is processed if only b1 is true. The third is processed if only b2 is true. A set of three booleans can exist is eight states. The three if statements account for three of those states; so five more states remain. The assert statement indicates that the programmer assumes that only one of those five remaining states is valid--that is the state where only b3 is true. The combination of the three if statements and the assert statement indicate that the programmer believes that no more than one of the booleans will be true at that point in the program. That assumption is called an internal invariant.  
25a  c  d  e  A1A2A3A1  A1A2A3A1A2A3  A1A2A3A1A1A2A3  A1A2A3A1A3A2A1  The three instances of class A form an isolated ring where each instance references the next instance and the third references the first instance. Four iterations of the for loop are processed. Inside the body of the for statement, the invocation of the print method contains the argument expression a0 = a0.other(). On the first iteration, the reference variable a0 references the instance named A3. The value returned by the method named other is a reference to the instance named A1. The reference is assigned to the reference variable a0 and is also the value produced by the expression a0 = a0.other(). That reference value is passed as an argument to the print method, and the print method invokes the A.toString method. With each iteration of the loop, the reference moves to the next object in the loop and the name of the object is printed. After four iterations, the loop ends and the method m1 returns. The invocation of the System.gc method serves as a suggestion that the garbage collector should be allowed to run. The system could ignore the suggestion, so there is no guarantee that the eligible arguments will be garbage collected. If they are collected, there is no guarantee which will be collected first. The only guarantee is that the finalize method will be invoked on each particular instance before the resources that had been allocated to that instance are reclaimed.  
26Prints: BBABBA  Type B is a subclass of type A, and method m(B x, B y) is more specific than the other three; because any invocation of it could be handled by any of the other three without causing a compile-time type error. All four methods are applicable to the first method invocation expression, m(null,null). The most specific method, m(B x, B y), is chosen; and both arguments are converted to type B. In the second method invocation expression, m(a1=null,b1=null), simple assignment expressions initialize the local variables a1 and b1 with null references of types A and B respectively. The invoked method is m(A x, B y). In the third method invocation expression, the positions of the arguments are reversed relative to the previous invocation: The type of the first argument is now B and the second is A. The invoked method is m(B x, A y).  
27a  c  e  Prints "Only b1 is true" followed by an error message.  The combination of the if/else statements and the assert statement indicate that the programmer expects no more than one boolean, b1, b2, or b3, to be true.  The assert statement is being used to check a control-flow invariant to verify that the control flow never reaches a point in the program.  Method m1 has a series of if/else statements. The first if statement is processed if none of the booleans are true. The second is processed if only b1 is true. The third is processed if only b2 is true. The fourth is processed if only b3 is true. A set of three booleans can exist in one of eight states. The first four if statements account for four of those states; so four more states remain. The combination of the three if statements and the fact that an AssertionError is thrown from the last else block indicates that the programmer believes that no more than one of the booleans will be true when method m1 is being processed. An assumption concerning the state of a set of variables is called an internal invariant. In this case, however, the assertion was tested by verifying that control never reached a particular point in the program. Based on the testing technique, we would say that the assertion tests a control-flow invariant. A throw statement is used in place of an assert statement, because the throw statement can not be disabled. As a result, the method is certain to generate an error once control passes beyond all of the return statements. The declared return type of method m1 is String. No return statement appears after the sequence of if statements; therefore, every if statement must either return a String or throw an exception. Assertions can be disabled at run time, so an assert statement in the final if block is no guarantee that an exception will be thrown. For that reason, an assert can not replace the throw statement.  
28c  d  , Ba, B1  B2, Ba, B1  Class H declares two static member variables named ba and i. The type of i is int, and the value is initialized to 1. The type of ba is B. The declaration of ba contains the class instance creation expression new B("Ba"). The constructor of class B assigns the argument value to the instance variable called name. Inside the main method of class H, the method invocation expression m1(ba) invokes method m1. The argument is the static member variable ba. The body of method m1 contains a return statement with the expression b = new B("B" + i++). The assignment expression contains the class instance creation expression new B("B" + i++) which creates a new instance of the class B. 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 assigned to the parameter variable b, but that assignment does not change the value of the member variable ba. The value of the assignment expression is the reference to the new instance of class B with the name B1, and that reference value is returned by the method m1. The returned value is assigned to the local variable x. The next statement inside the main method is another invocation of method m1. The argument appearing in the method invocation expression m1(x) is the local reference variable x. The method invocation does not change the value of x. The value returned by this second invocation of m1 is a reference to a new instance of class B that has the name B2. The returned reference value is not assigned to a variable, so the instance named B2 is eligible for garbage collection. There is no guarantee that the garbage collector will run before the print statement is invoked. If it does run, then the instance named B2 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.