| No. | Answer | Remark | |
|---|---|---|---|
| 1 | a | 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. |
| 2 | g | Prints: 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
|
| 3 | d | Prints: 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. |
| 4 | c | Compile-time error | In the declaration of interface B, the keyword, extends, has been replaced by the keyword, implements. |
| 5 | g | round | |
| 6 | f | Compile-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! |
| 7 | c | Compile-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. |
| 8 | l | None of the above | |
| 9 | b d | The Enumeration interface declares only two methods: hasMoreElements and nextElement. The 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. |
| 10 | b 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. |
| 11 | l | None of the above | |
| 12 | e | None 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. |
| 13 | c | Prints: 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. |
| 14 | e | The returned value is of type long only if the argument is of type double. | |
| 15 | a 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. |
| 16 | d | LinkedList | 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. |
| 17 | c | LinkedList | The LinkedList class provides methods such as addFirst, addLast, getFirst, getLast, removeFirst and removeLast that facilitate the implementation of stacks and queues. |
| 18 | g | None 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. |
| 19 | d 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. |
| 20 | h | Compile-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. |
| 21 | b | Prints: 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. |
| 22 | b | Prints: 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. |
| 23 | b | The 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. |
| 24 | e | Compile-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. |
| 25 | b | Prints: 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. |
| 26 | e | An 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
|
| 27 | e | An 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
|
| 28 | b 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. |