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
 
Answers: Certified Java Programmer Mock Exam
No.AnswerRemark
1 All methods declared within an interface are implicitly abstract. The final, synchronized and native modifiers can not appear in the declaration of an abstract method, and can not be applied to an abstract method declared within an interface.  
2Prints: FDIL  The method name abs is overloaded. There are versions that accept one argument of type int, long, float or double. The type of the return value is the same as the argument type. There are two versions of the Math.round method. One accepts an argument of type float; the return type is int. The other accepts an argument of type double; the return type is long.  
3Prints: ABCBCC  The switch statement does not contain any break statements. The first case falls through to the second. The second case falls through to the third. There is no default switch label, so nothing is printed when variable i is 3.  
4Compile-time error  In the declaration of interface B, the keyword, extends, has been replaced by the keyword, implements.  
5round   
6Compile-time error  The loop variable, h, is local to the for statement. The print statement causes a compile-time error, because it refers to the out-of-scope local variable, h. Always check for variables that are out-of-scope!  
7Compile-time error  Fields declared within an interface are implicitly public, final, and static. A compile-time error is generated in response to the attempt to increment the value of i.  
8None of the above   
9b  d  The Enumeration interface declares only two methods: hasMoreElements and nextElementThe Iterator interface declares a total of three methods.  The Enumeration interface was introduced with Java 1.0 to provide an easy means of moving through the elements of a Vector or the keys or values of a Hashtable. The Iterator interface was introduced with the collections framework with Java 1.2. The Iterator interface declares three methods: hasNext, next and remove. The first two methods, hasNext and next, are similar to the two methods declared in the Enumeration interface, hasMoreElements and nextElement. The third method of the Iterator interface, remove, provides new functionality relative to the Enumeration interface.  
10b  c  Prints: 0  Prints: 1  The new thread is started before the print statement, but there is no guarantee that the new thread will run before the print statement is processed. The guarantee could be provided by placing the method invocation expression a.join() before the print statement, but the invocation of the join method does not appear in the program. If the new thread runs before the print statement is processed, then 1 is printed. Otherwise, 0 is printed.  
11None of the above   
12None of the above.  The Iterator interface declares three methods: hasNext, next and remove. The ListIterator interface was introduced in Java 1.2 along with the Iterator interface. The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities and the ability to determine the position of the iterator in the List. The ListIterator interface does not extend the List interface.  
13Prints: 1  The a.run() method was called instead of a.start(); so the entire program runs as a single thread, and a.run() is guaranteed to complete before the print statement is called.  
14The returned value is of type long only if the argument is of type double 
15a  c  Prints a number greater than or equal to 0  This program will run for at least ten seconds  Thread a1 will run for at least ten seconds, but the main method is likely to run to completion very quickly. The start method will return without waiting for thread a1 to complete. Since thread a1 immediately goes to sleep the thread that is processing the main method has an opportunity to complete the main method quickly. The number printed in the main method can be as small as zero.  
16LinkedList  A stack or queue must be implemented using a data structure that stores the elements based on the order of insertion. Any data structure that is implemented using a hashtable is not a good choice. The ArrayList and Vector are both implemented using an internal array. Although an array allows elements to be easily organized based on the order of insertion, an array does not allow the list of elements to be easily shifted in memory as elements are appended to the tail of the list and removed from the head of the list. The LinkedList is implemented using a doubly linked list that allows elements to be easily appended to the tail of the list, and removed from the head of the list.  
17LinkedList  The LinkedList class provides methods such as addFirst, addLast, getFirst, getLast, removeFirst and removeLast that facilitate the implementation of stacks and queues.  
18None of the above.  The RandomAccess interface is a marker interface; so it does not declare any methods. Its purpose is to provide information about the RandomAccess capabilities of a List implementation. Generic list algorithms can check to see if an instance of a List implements the RandomAccess marker interface. If not, then the algorithm can avoid operations that require fast random access. Both Vector and ArrayList implement the RandomAccess interface. LinkedList does not implement RandomAccess.  
19d  e  Compile-time error at line 2  Compile-time error at line 3  A compile-time error occurs at line 2 as a result of the attempt to shadow the method local variable, k, within the for-loop. A variable declared within the for-loop can not shadow a local variable of the enclosing method. At line 3, the declaration of variable j causes a compile-time error. The initialization part of a for statement may use a single local variable declaration statement to declare more than one local variable. Each of the new variables is declared in a comma separated list of variable declarators. In this program, the local variables should have been declared as follows: "int i=0, j=0;". Instead, the type of variable j has been incorrectly added to the declarator: "int i=0, int j=0;". The result is a compile-time error.  
20Compile-time error at line 4  Class C10 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C10; then, no compile-time error occurs. Line 4 generates the compile-time error, because it is the first to access the name field as a member of class C10.  
21Prints: 1212013  A method local variable, i, is declared at line 3. At line 4, the initialization part of the for statement initializes the method local variable to the value zero. The for statement does not declare a new variable, i, so the method local variable is not shadowed within the loop. Suppose a local variable, v, is declared within a statement or block. Variable, v, can shadow a class variable or an instance variable of the same name, but a compile-time error is generated if v shadows a method local variable. Please note that a compile-time error is not generated if a local variable is shadowed within the declaration of a class that is nested within the scope of the local variable.  
22Prints: I10.s10,I20.s20,I20  Class C20 inherits ambiguous declarations of the name field. As long as the field is not referenced as a member of class C20; then, no compile-time error occurs. Although line 4 may appear to generate the compile-time error it does not, because name is accessed directly as a member of interface I20. Therefore, the compiler does not encounter an ambiguity.  
23The ListIterator interface extends both the List and Iterator interfaces.  The ListIterator interface extends the Iterator interface and declares additional methods to provide forward and backward iteration capabilities, List modification capabilities and the ability to determine the position of the iterator in the List.  
24Compile-time error at line 4  A method local variable, i, is declared at line 3. At line 4, the initialization part of the for statement attempts to shadow the method local variable with a new variable that is intended to be local to the for statement. The result is a compile-time error. At line 2, a new variable, i, is declared within the for statement. That variable shadows the class variable, but it does not shadow the method local variable declared at line 3. The scope of a method local variable begins with its own initializer--if present--and extends to the end of the method body. At line 2, the method local variable is not in scope, so shadowing does not occur. Suppose a local variable, v, is declared within a statement or block. Variable, v, can shadow a class variable or an instance variable of the same name, but a compile-time error is generated if v shadows a method local variable. Please note that a compile-time error is not generated if a local variable is shadowed within the declaration of a class that is nested within the scope of the local variable.  
25Prints: 1,3,2  Suppose the print statement, System.out.print("("+h+","+i+","+j+","+k+")");, is inserted before the switch statement. The output of the program would be as follows: (1,1,0,2)(1,2,1,4)(1,3,2,6)1,3,2. Three iterations of the loop are executed. On the first iteration, the switch expression, k, matches the case constant, 2. The subsequent break statement is executed and the switch statement completes normally. On the second iteration, the switch expression matches the case constant, 4; and the subsequent continue statement causes control to pass to the loop-continuation point of the do statement where the expression, ++j < 5, is evaluated and found to be true. On the final iteration, the switch expression does not match any case constant; so the break statement following the default label is processed.  
26An IllegalThreadStateException is thrown at run-time  For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. The actual behavior of Java might be different. If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored, and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4 version of the JVM.  
27An IllegalThreadStateException is thrown at run-time  For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. The actual behavior of Java might be different. If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored, and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4 version of the JVM. The Thread.join method is included here to verify that the thread is already dead before the start method is invoked the second time. If this code is executed using the 1.4 version of the JVM, the exception will not be thrown. However, for the purposes of the exam, the exception is always thrown. The real exam question will probably not include the invocation of the join method.  
28b  d  Prints: [T1,A][T2,B]  Prints: [T2,B][T1,A]  Since method m1 is synchronized, it is guaranteed that no more than one thread will execute the method at any one time. Even though the start method is invoked on thread T1 first, there is no guarantee that it will actually begin to run first.  

Copyright © 2002-2003, Dan Chisholm
All rights reserved.