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.
| No. | Answer | Remark | |
|---|---|---|---|
| 1 | e | Compile-time error | The access modifiers public, protected and private, can not be applied to variables declared inside methods. |
| 2 | b d g h | strictfp super goto native | |
| 3 | e | 5 | The escape sequences are as follows: '\b' (backspace), '\f' (formfeed), '\n' (newline), '\r' (carriage return), '\t' (horizontal tab), '\\' (backslash), '\"' (double quote), '\'' (single quote). Yes, you must memorize the escape sequences! Just remember "big farms need red tractors". |
| 4 | e f g | parseDouble toString(double) valueOf | |
| 5 | c | Prints: false,true,false | The Boolean.valueOf method 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 String reference is null, then the new instance will contain the value false. |
| 6 | i | Compile-time error |
The
|
| 7 | b | Prints: p,p,S |
The
|
| 8 | b | TreeMap | The requirement to store key/value pairs is directly satisfied by a concrete implementation of the Map interface. The List and Set interfaces recognize objects, but do not recognize keys and values. TreeMap and TreeSet store elements in a sorted order based on the key, but the TreeSet does not support key/value pairs. |
| 9 | h | Prints: true,true,true | The 1.2 version of Java introduced the updated Vector class that implements the List interface. |
| 10 | d | None of the above | Suppose that the hashCode method is invoked on the same object more than once during the same execution of a Java application. If no information used in the equals comparison is modified, then each invocation of the hashCode method must produce the same hash code value. The hashCode methods of classes B and C will always return the same value during an execution of the Java application and are therefore consistent with the hash code contract. Even so, the hashCode methods of classes B and C are not efficient, because they will cause hashtables to place every instance of classes B and C in the same bucket. The hashCode method of class D is appropriate and will allow a hash table to operate efficiently. |
| 11 | a b e | A final method can not be overridden. All methods declared in a final class are implicitly final. A machine-code generator can inline the body of a final method. | All methods declared in a final class are implicitly final. It is permissible--but not required--that all such methods be explicitly declared final. All private methods are implicitly final. It is permissible--but not required--that all private methods be explicitly declared final. The body of an inline method is inserted directly into the code at the point where the method is invoked. If the method is invoked at 10 different points in the code, then the body can be copied to all 10 points. Inline code runs very quickly, because it removes the need to do the work associated with a method invocation. If the method is executed repeatedly in a loop, then inlining can improve performance significantly. The machine-code generator has the option to inline a final method. |
| 12 | a | 1 | The modifiers, private and static, can be applied to a nested class, but can not be applied to a class that is not nested. A class that is not nested can have public or package access, but not private. The transient modifier can not be applied to any class; because it is a field modifier. |
| 13 | f | None of the above | The following modifiers can be applied to a member class: abstract, private, protected, public, static and final. |
| 14 | d | Prints: 0,0,1,0,1,1 | The nested catch clause is able to catch a Level2Exception or any subclass of it. The switch statement throws an Exception that can not be caught by the nested catch clause; so the nested finally block is executed as control passes to the second of the two outer catch clauses. The outer finally block is executed as control passes out of the try statement. |
| 15 | a e | abstract public | The modifier, abstract, is applicable to an interface declaration, but its use is strongly discouraged; because every interface is implicitly abstract. An interface can not be final. The modifiers, private and protected, are applicable only to an interface declaration that is a member of a directly enclosing class declaration. If an interface is not a member of a directly enclosing class, or if the interface is a member of a directly enclosing interface; then, the modifiers, private and protected, are not applicable. If an interface is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the interface plus the extension .java. |
| 16 | b d | '\u0000' to '\uffff' 0 to 65535 | A char is a 16 bit unsigned value; so none of the char values are negative and the minimum value is zero. The maximum value is 216 - 1. |
| 17 | f | Prints: true,false,true | The positive infinity of type float is promoted to the positive infinity of type double. NaN is not equal to anything including itself. |
| 18 | c | Prints: 195ab | Both operands of the first addition operator are promoted from type char to int, and are evaluated as integral numeric values. The right hand operand of the second addition operator is of type String, so the result of the first addition operator is converted to type String, and is concatenated with the right hand operand. As evaluation of the expression continues from left to right, the remaining operands are also converted to type String. |
| 19 | c | Compile-time error | An anonymous class declaration can not contain an explicit declaration of a constructor. |
| 20 | c | Prints: 61433 | On the first pass through the loop, the value of x is 6, so 5 is subtracted from x. On the second pass, the value of x is 1, so 3 is added to x. On the third pass, the value of x is 4, so 1 is subtracted from x. On the fourth pass, the value of x is 3, so the variable, success, is incremented from zero to one. On the final pass, the value of x is 3 and the variable, success, is incremented to the value, 2. The boolean expression of the do loop is now false, so control passes out of the loop. |
| 21 | a d | With assertions enabled it prints ABC followed by an AssertionError message. With assertions disabled it prints ABCE | If the default label of a switch statement should not be reached under normal operating circumstances, then the default label might be a good candidate for the use of an assert statement. |
| 22 | c | C | Please note that this question asks which objects are NOT eligible for garbage collection when method m2 begins to execute? All three references, i1, i2 and i3, refer to object named C; so C is not eligible for garbage collection when method m2 begins to execute. The objects named A and B have references to each other, but no other objects refer to A and B. The objects A and B form an island of islolated objects and are eligible for garbage collection. |
| 23 | c | Compile-time error | Local variables are not initialized automatically, and must be initialized explicitly before attempting to access the value. The local variable i3 will not be initialized if i1 is less than or equal to zero; so the result is a compile-time error. |
| 24 | a | Prints: 78 ABC*$ | When char variables a and b are converted to String types they are printed as *$. When not converted to String types they are promoted to type int, and are printed as the numeric sum of 0x2a and 0x24. At line 1, the expression, a + b, can be evaluated as follows: 0x2a + 0x24 = 42 + 36 = 78. At line 2, the first operand of the expression, " ABC" + a + b, is of type String. Since one operand of the first addition operator is of type String the other operand must be converted to type String: (" ABC" + "*") + b = " ABC*" + b. The process is repeated for the second addition operation: " ABC*" + b = " ABC*" + "$" = " ABC*$" |
| 25 | b | Prints: GFC203 | The type of the argument is null and could be converted to either type GFC202 or GFC203 by method invocation conversion; so both methods are applicable. The more specific of the two, m(GFC203 x), is chosen. Type GFC203 is a subclass of type GFC202; so any argument that can be passed to m(GFC203 x) can also be passed to method m(GFC202 x) without causing a compile-time type error; therefore, we can say that method m(GFC203 x) is more specific than m(GFC202 x). |
| 26 | b | Prints: 147258369 | The array variable a1 is declared with the initializer, {{1,2,3},{4,5,6},{7,8,9}}. The array access expression, a1[0][1] = a1[first subarray][second element] = 2. If the argument of the print statement had been a1[i][j] then the output would have been 123456789. The tricky feature of this question is the reversal of i and j to produce the deceptive array access expression, a1[j][i]. The output is 147258369. |
| 27 | c | Prints: 5 | The two statements, int a=1 followed by a += ++a + a++, can be rewritten as the single statement, a=(int)((1)+(++a + a++)). Further evaluation produces a=(int)((1)+(2 + 2)). Generally speaking, a compound assignment expression of the form E1 op= E2 can be rewritten as E1=(T)((E1)op(E2)) where T is the type of E1. |
| 28 | f | None of the above | The program compiles and runs without error and prints 112. It is necessary to remember that arrays are Cloneable objects. Furthermore, a two dimensional array is also a single dimensional array of single dimensional arrays. At line 2, a two dimensional array of int primitives is converted to a single dimensional array with components of type Object where each Object is a single dimensional array. The loop iterates through each element of the Object array. Since each element is actually a reference to a single dimensional array of integer primitives a conversion from type Object to an int array is legal. |
| 29 | c | Prints: 1,3 | Although the reference parameters i1 and i2 are reassigned inside of m1, the change has no impact outside of m1. Array references are passed by value: the invoked method gets a copy of the array reference. |