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 | c e g i | const continue extends break | All of the letters of all Java keywords are lower case. The word instanceof is a Java keyword, but instanceOf is not. |
| 2 | f | None of the above | Floating-point literals are covered in section 3.10.2 of the JLS. A floating-point literal can begin with either a digit or a decimal point. Optionally, it can have a fractional part, an exponent part and a floating point suffix--f, F, d, or D. |
| 3 | d | Prints: 0 | Member variables are initialized automatically. Type int variables are initialized to zero. |
| 4 | c | Prints: 0,1,1,0 | The first try block contains two statements. The first invokes method m1, and the subsequent statement contains a post increment expression with the variable, a, as the operand. Method m1 throws a WhiteException exception, so variable a is not incremented as control passes to the catch block where b is incremented. The throws clause of m1 declares a ColorException, so the body may throw a ColorException or any subclass of ColorException. The second try block also contains two statements. The first invokes method m2, and the subsequent statement contains a post increment expression with the variable, d, as the operand. Method m2 does not throw an exception, so d is incremented, and the try block completes normally. Although the throws clause of m2 declares a WhiteException, there is no requirement to throw any exception. |
| 5 | c f | final public | The modifier, abstract, is not applicable to a variable. All field declarations within an interface are implicitly public, static and final. Use of those modifiers is redundant but legal. Although const is a Java keyword, it is not currently used by the Java programming language. An interface member can never be private or protected. |
| 6 | b c e | 2 3 5 | The maximum char value is 0xffff. The maximum byte value is 127 = 0x7f. The hex value 0xff is of type int, and the decimal value is 255. The maximum positive value of type byte is 127. The value 255 is beyond the range of type byte; so 255 can not be assigned to type byte without an explicit cast. The assignment expression b3 = (byte)0xff would assign the value -1 to variable b3. |
| 7 | d | Prints: true,true | The bitwise complement operator produces the same result as changing the sign and subtracting one. Please note that the operand must be an integral type. The bitwise complement operator can not be applied to a floating-point value. |
| 8 | d | None of the above | All three classes are tightly encapsulated, because the data members are private. A tightly encapsulated class can have public accessor and mutator methods, but it is not required to have those methods. |
| 9 | b | Prints: A B C | Strings are immutable. No method will change the contents of a String instance. Some String methods such as concat, replace, substring and trim will return a new String instance with the desired modifications. In this case, the String instances returned by the methods trim and concat are ignored. |
| 10 | b | Compile-time error at 1 |
At line 1, the invocation of the
|
| 11 | e | Prints: true,false,false |
|
| 12 | b d e | Long.parseLong("+1") Long.parseLong("1L") Long.parseLong("1.0") |
|
| 13 | e f | parseDouble valueOf | |
| 14 | d | HashMap | 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. The requirement to allow null elements is not satisfied by a Hashtable. TreeMap and TreeSet store elements in a sorted order based on the key. |
| 15 | h | Prints: true,true,true | All three implement Cloneable. |
| 16 | e | return 31 * getI1() + getI2(); | All of the statements would produce a hashCode method that is consistent with the hash code contract. The expression 31 * getI1() + getI2() produces the most efficient hashCode method, because it is most likely to produce unique hashcodes for various combinations of i1 and i2. The expression getI1() + getI2() is less efficient, because it produces the same hash code when the values of i1 and i2 are swapped. |
| 17 | f | Compile-time error | The array initializer, {{1;2};{3;4;5};{6;7;8;9}} generates a compile-time error, because the commas have been replaced by semicolons. The array initializer should have been specified as follows: {{1,2},{3,4,5},{6,7,8,9}}. |
| 18 | a b c d | 1 2 3 4 | The modifiers, abstract and final, can be applied to a method local class declaration. The synchronized modifier can not be applied to a class, because it is a method modifier. The modifiers, transient and volatile, can not be applied to any class, because they are field modifiers. |
| 19 | d | Prints: D | 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 (=). |
| 20 | f | Compile-time error | The throws clause of White.m2 declares a WhiteException, so the body of m2 may throw a WhiteException or any subclass of WhiteException. Instead, the body of m2 throws a superclass of WhiteException. The result is a compile-time error. |
| 21 | a d f | With assertions enabled it prints an AssertionError message. With assertions disabled it prints nothing. 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. | The assert statement indicates that the programmer believes that b1 and b2 will never be true simultaneously, and the assert statement should not be reached under normal operating conditions. |
| 22 | g | None of the above |
After method
m1
returns, none of the objects are
eligible for garbage collection.
Method
m1
sets the parameter variable
a1
to
null,
but that does
not change the reference
a1
in the
|
| 23 | f | Compile-time error | The method invocation expression, m(gfc213, gfc213), is ambiguous; because, no applicable method is more specific than all of the others. Method m(GFC212 x, GFC212 y) is more specific than m(GFC212 x, GFC211 y), because any invocation of m(GFC212 x, GFC212 y) could also be handled by m(GFC212 x, GFC211 y) without causing a compile-time type error. However, some invocations of m(GFC212 x, GFC212 y) can not be handled by m(GFC211 x, GFC213 y), so m(GFC212 x, GFC212 y) is not more specific than m(GFC211 x, GFC213 y). Furthermore, not all invocations of m(GFC211 x, GFC213 y) could be handled by m(GFC212 x, GFC212 y). |
| 24 | b | Prints: DE | At run-time, the actual field that is accessed depends on the compile-time type of the reference--not the run-time type of the object. The compile-time type of the reference x in the name x.s1 is D; so the selected field is the one declared in class D. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. The same reference variable x is used in the method invocation expression x.getS1(), and the compile-time type is still D. At run-time, the actual type of the object is E; so the selected method is the one declared in class E. |
| 25 | g h j | 7 8 10 | A non-static nested class is called an inner class. An inner class can not declare a static field unless it is a compile-time constant. Even though f is final, it does not have a value at compile time; so it causes a compilation error. Member variable g also causes a compile-time error, because it is static. The static initializer of NonStaticInner causes a compile-time error, because inner classes (i.e. non-static nested classes) can not declare static initializers. |
| 26 | d | Compile-time error | The declaration of c2 causes a compile-time error, because a reference of a subclass type can not refer to an instance of the superclass class. |
| 27 | b | A member variable that is an object reference | Primitives don't have locks; therefore, they can not be used to synchronize threads. A method local variable that is a reference to an instance that is created within the method should not be used to synchronize threads, because each thread has its own instance of the object and lock. Synchronization on an instance that is created locally makes about as much sense as placing on your doorstep a box full of keys to the door. Each person that comes to your door would have their own copy of the key; so the lock would provide no security. |
| 28 | c | Prints: 3 | The expression can be simplified as follows: j = 1 + ((2 * 3) % 4) + 5 = 8. The original expression is as follows: j = ++i + ((++i * ++i) % ++i) + ++i;. Step one. Evaluate the unary expressions from left to right: j = 1 + ((2 * 3) % 4) + 5. Step two. Evaluate the inner-most parentheses: j = 1 + (6 % 4) + 5. Step three: Evaluate the inner-most parentheses. j = 1 + 2 + 5. Step four: Work through the expression from left to right. j = 8. The argument of the print expression is: j%5. The result is: 8 % 5 = 3. |