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 | d e f | An attempt to run GRC7 from the command line fails. An attempt to run GRC8 from the command line fails. An attempt to run GRC9 from the command line fails. | Section 12.1.4 of the JLS requires the main method to be declared static. In this example, each of the three main methods are not declared static. The result is an error at run-time. |
| 2 | b | 2 | 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". |
| 3 | d e | Compile-time error at 3. Compile-time error at 4. | Class A and C are not declared in the same package; therefore, class C does not have access to package access method, m4. Since class C extends class A, class C does have access to the protected method, m2, of class A. |
| 4 | a f | abstract public | All interfaces are implicitly abstract. The explicit application of the abstract modifier to an interface declaration is redundant and is strongly discouraged. The declaration of an interface within the body of an enclosing class or interface is called a member type declaration. Every member type declaration appearing within the body of a directly enclosing interface is implicitly static and public. Use of the access modifiers, private or protected, is contradictory and results in a compile-time error. In contrast, the modifiers, private and protected, are applicable to a member type declaration appearing within the body of a directly enclosing class. The modifier, final, is never applicable to an interface. The keyword, implements, is not a modifier. |
| 5 | a | Prints: 1111111,177,127,7f |
A
byte
is an 8 bit signed value. The left most bit is the
sign bit. The sign bit is set to zero for positive numbers
and is set to one for negative numbers. The most positive
byte value is represented as a sign bit that is set to zero
and all of the other bits set to one. The
|
| 6 | d | Prints: false,true,true | The right hand operand of the conditional or operator is evaluated only if the left hand operand is false. The right hand operand of the conditional and operator is only evaluated if the left hand operand is true. In this case, the left hand operand of the conditional or operator is false, so the right hand operand must also be evaluated. The left hand operand of the conditional and operator is the result of the conditional or expression, true, so the right hand operand is evaluated. |
| 7 | a d e | 1 4 5 | The minimum value of type byte is -128. The maximum value of type short is 32767. The minimum value of type char is 0. |
| 8 | b | Prints: 2,3 | Variables of primitive type are passed to methods by value: Only a copy of the value of the variable is passed to the method. While the method works with a local copy of the variable, the original variable remains unchanged by any actions performed on the method parameter. For that reason, method m1 does not change the value of the variable y in the main method. However, method m1 does have direct access to the class variable x and the content of the class variable is modified by method m1. |
| 9 | e | None of the above | If a class A has a method that returns a reference to an internal, mutable object; then external code can use the reference to modify the internal state of class A. Therefore, class A can not be considered tightly encapsulated. However, the methods of a tightly encapsulated class may return a reference to an immutable object or a reference to a copy or clone of an internal object. |
| 10 | d h i | resume stop suspend | For the purposes of the exam, you don't need to memorize the deprecated methods of the Thread class. Even though a question such as this will not be on the exam, every Java programmer should know that the deprecated methods should not be used in new programs. |
| 11 | c | Prints: 0 |
If
NaN
is the argument passed to
|
| 12 | c | Compile-time error | A compile-time error is generated, because the Byte class does not have a charValue method. The Byte class extends the Number class and implements all six of the methods declared in Number. |
| 13 | c | Compile-time error | The Integer class has only two constructors: one accepts a primitive int, and the other accepts a String. There is no constructor that accepts an argument that is an instance of type Integer. |
| 14 | d | Compile-time error | Long is a subclass of the abstract class Number, and Long implements all of the methods of Number: byteValue, shortValue, intValue, longValue, floatValue and doubleValue. The attempt to invoke the charValue method on an instance of Long generates a compile-time error, because there is no charValue method. |
| 15 | d | Prints: -1,-1,true |
Double
is a subclass of the abstract class
Number,
and implements all of the methods of
Number
such as
byteValue,
shortValue,
floatValue,
etc.
In this case, the
Double
instance contains the value
0xFFFF.
When that value is converted to type
byte
the result is
0xFF
which is also the two's complement representation of the
byte
value -1. Similarly,
0xFFFF
is the two's complement representation of the
short
value -1.
Please note there is no
|
| 16 | c | Prints: 0null | The numeric sum of variables a, b, c, d and e is zero. The zero is converted to a String and concatenated with s. |
| 17 | d | 4 | The assignment expression, a = c + a, requires an explicit cast to type int. If one of the two operands of a numeric expression is of type long and if the other operand is of type int, short, char or byte; then it will be promoted to type long, and the result of the expression will be of type long. (Note: The rule does not apply to the shift operator.) The type long result can not be assigned to a variable of type int without an explicit cast. |
| 18 | e | None of the above | Although the reference named base is of type Base, the instance that it refers to is of type Sub, and the reference can be cast to type Sub. Since instances of type Sub implement both interfaces, I1 and I2, the Sub type instances can be assigned to references of type I1 and I2 without an explicit cast. |
| 19 | g | None of the above | All array types implement the Serializable interface and may be assigned to a reference of type Serializable. |
| 20 | e | Compile-time error | The method invocation expression, m(b1), contains an argument of type double. A method invocation conversion will not implicitly narrow the argument to match the parameter type of the method, m(float i). The method invocation expression, m(a1), contains an argument of type long. A method invocation conversion will widen the argument to match the parameter type of the the method, m(float i). |
| 21 | c | Prints: F.printS1 E.printS2 | A static method is selected based on the compile-time type of the reference--not the run-time type of the object. A non-static method is selected based on the run-time type of the object--not the compile-time type of the reference. Both method invocation expressions, x.printS1() and x.printS2(), use a reference of the superclass type, E, but the object is of the subclass type, F. The first of the two expressions invokes an instance method on an object of the subclass type; so the overriding subclass method is selected. The second invokes a static method using a reference of the superclass type; so the superclass method is selected. |
| 22 | a b c e | s1 s2 s3 s5 | Class Z is a local class defined within the code block of method m1 of class C. All of the fields and methods of class C (static, non-static, and private) are available to Class Z. Additionally, the parameters of method m1 that are declared final are available to class Z. If a final local variable declaration appears before the declaration of class Z, then it is also available to class Z. Parameter s6 and variable s4 are not final and are therefore not available to class Z. There are at least two reasons why non-final variables are not available to a local inner class. The first reason is because the life cycle of a local variable might be shorter than that of a local class. A local variable lives on the stack and disappears as soon as the method is exited. A local class, however, might continue to exist even after the method has been exited. The second reason is due to multithreading issues. Suppose a local class extends the Thread class or implements the Runnable interface, and it is used to spawn a new Thread from within the method m1. If the local variables of method m1 were available to the local class, then a mechanism for synchronizing access to the variables would be required. |
| 23 | b | Prints: ABC | Three methods overload the method name m1. Each has a single parameter of type A or B or C. For any method invocation expression of the form m1(referenceArgument), the method is selected based on the declared type of the variable referenceArgument--not the run-time type of the referenced object. The method invocation expression c4.m1(c1) uses reference c4 of type C to invoke method m1 on an instance of type C. The argument, c1, is a reference of type A and the run-time type of the referenced object is C. The argument type is determined by the declared type of the reference variable c1--not the run-time type of the object referenced by c1. The declared type of c1 is type A; so the method A.m1(A a) is selected. The declared type of c2 is type B; so the method invocation expression c4.m1(c2) invokes method B.m1(B b). The declared type of c3 is type C; so the method invocation expression c4.m1(c3) invokes method C.m1(C c). |
| 24 | c | Compile-time error | A compile-time error is generated due to the attempt to access the length method of the String class as though it were a variable. |
| 25 | a | Prints: false,false |
|
| 26 | d | import, break, double, exception, throws | The word exception is not a Java keyword. The words import, break, double and throws are Java keywords. |
| 27 | j | Compile-time error. | The precedence of the cast operator is higher than the precedence of the addition operator, so the cast applies only to variable a and not to the result of the addition. Binary numeric promotion causes the byte variables a and b to be promoted to type int before the addition operation, and the result of the addition is also of type int. The attempt to assign the int result to the byte variable e generates a possible loss of precision error. |
| 28 | b | Prints: 1,3 | The method m1 is invoked by the method invocation expression m1(i1, i2). The argument i1 denotes a local variable of type int[] that is declared in the main method. The value of the argument is a reference to the array, and the argument value is used to initialize the method parameter i1 of method m1. Inside the body of m1, the expression i1 = i2 sets the value of parameter i1 to the value of parameter i2, but the change in the value of the parameter i1 does not change the original argument value or the local variable i1 of the main method that the argument denotes. Similarly, the assignment expression i2 = i3 in method m1 does not change the value of the local variable i1 declared in the main method. |
| 29 | c | Prints: -2, 3, 0, 2, 2, 5, | The expression can be simplified as follows: -2+3+0+2+2=5. The original expression is as follows: m(m(~1) + m(1|2) + m(1&2) + m(1^3) + m(1<<1)). Expr 1: ~1 = -1 - 1 = -2. Expr 2: 1|2 = 0001 | 0010 = 0011 = 3. Expr 3: 1&2 = 0001 & 0010 = 0000. Expr 4: 1^3 = 0001 ^ 0011 = 0010 = 2. Expr 5: 1<<1 = 0001 << 1 = 0010 = 2. Note: The bitwise expressions were demonstrated using only four bits of the 32 bit int type values. |