| No. | Answer | Remark | |
|---|---|---|---|
| 1 | g | None of the above | Section 12.1.4 of the Java Language Specification requires that the main method must accept a single argument that is an array of components of type String. In each of the three class declarations, the single argument is indeed an array of components of type String. Please note that the square brackets within an array declaration may appear as part of the type or part of the declarator (i.e. array name). |
| 2 | d | 4 | 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 | f | Compile-time error | Method m1 is an instance method, and must be invoked with reference to an instance of type GFC402. Method m1 can not be invoked from a static context. |
| 4 | a b | protected public | Constructors can not be inherited, so an abstract constructor would be useless, since it could never be implemented. A static constructor would also be useless--or nearly so--since it would be unable to access the non-static members of the new instance. An object is not available to multiple threads during the construction process, so the synchronized modifier would not provide additional protection. The transient modifier can be applied to a field, but not a constructor. |
| 5 | a c d | static transient volatile | A transient field is not part of the persistent state of an object. Transient fields are not serialized. Fields that are shared between threads may be marked volatile to force each thread to reconcile its own working copy of the field with the master copy stored in the main memory. The synchronized modifier may be applied to methods but not to fields. |
| 6 | d | transient | A final method can not be overridden. A static method is associated with a class, but not a particular instance of the class. A thread can not enter a synchronized method without first acquiring a lock. The field modifiers, transient and volatile, are not applicable to method declarations. A native method is implemented in platform-dependent code. |
| 7 | d | Compile-time error at 2. | There is a compile-time error at 2. The short type variable, s, is not a compile-time constant, so it can not be assigned to type byte without an explicit cast. The statement, "return s;", is a return statement with an expression, s. A compile-time error occurs if the type of the expression is not assignable to the declared result type of the method. The declared result type of the method, m4, is byte. The return statement attempts to return the value of the short type variable, s. If a short value is a compile-time constant, and if the value falls within the range of type byte, then the short value is assignable to type byte. In method m4, variable s is not a compile-time constant, so the value of variable s is not assignable to type byte. While the declaration of method m4 produces a compile-time error, the declaration of method m2 does not; because the variable is a compile-time constant with a value, 2, that is assignable to type byte. |
| 8 | b | A local class can be declared abstract. | An anonymous class can not be extended; therefore, an anonymous class can not be declared abstract. A local class can be abstract. An abstract class can not be instantiated. If a class declaration contains an abstract method, then the class must also be declared abstract. A class can be declared abstract even if it does not contain an abstract method. An abstract class can never be declared final. |
| 9 | a c e | A local class is declared within a method, constructor or block. A local class is a nested class. A local class is always a named class. | Every class declared within the body of another class is known as a nested class. If the nested class does not have a name, then it is an anonymous class. If the nested class has a name, then it is not anonymous. If the nested class has a name and is declared inside of a method, constructor or any block, then it is a local class. If a nested class does not have a name, then it can not be called a local class even if it is declared inside of a block. Therefore, an anonymous class is never called a local class. If the nested class has a name and is not declared inside of a method, constructor or any block, then it is a member class. If the class is not nested, then it is a top level class. Please note that this question is more rigorous than those that one might expect to find on the real exam. It has been included here as a convenient place to define some terms that are used to explain the answers to some of the other questions that appear in this mock exam. |
| 10 | c | Prints: v w x x y z z | Cases one and three have no break statement, so the next case is also executed and x and z are printed twice. |
| 11 | b | Prints: false,true | Error is a direct subclass of Throwable. RuntimeException is a direct subclass of Exception. |
| 12 | c d e | Compile-time error at 2. Compile-time error at 3. Compile-time error at 4. | Classes A and D are not declared in the same package, so class D does not have access to package access method, m4. Since class D does not extend class A, class D does not have access to the protected method, m2, of class A. |
| 13 | a e | An interface declaration can be a member of an interface. An abstract method declaration can be a member of an interface. | An interface can be declared within an enclosing class or interface. The members of an interface can be constants, abstract method declarations, class declarations, or interface declarations. The body of a method declared within an interface is a semicolon. An interface can extend another interface, but can not implement an interface. An abstract class that has an interface, I1, in its implements clause is not required to implement any of the methods declared within I1. |
| 14 | b | Prints: -32768,32767 | A short is a 16 bit signed value; so the minimum short value is -(215) and the maximum value is (215 - 1). |
| 15 | e | Prints: true,false,false | The conditional and expression is not evaluated, because the left hand operand of the conditional or expression is true. The original expression is as follows: x = (a = true) || (b = true) && (c = true). The left hand operand of the conditional or expression is the result of the assignment expression, (a = true). Since the left hand operand of the conditional or expression is true, the right hand operand will not be evaluated. In this case, the right hand operand is the conditional and expression. Consequently, neither operand of the conditional and expression is evaluated and the variables, b and c, maintain their default values of false. |
| 16 | b c f | 2 3 6 | The maximum value of type byte is 127. The minimum value of type short is -32768. The maximum value of type char is 65535. |
| 17 | b e g | With assertions enabled it prints 012. With assertions disabled it attempts to print an infinite sequence of numbers. As a rule, the boolean expression of an assert statement should not be used to perform actions that are required for normal operation of the program. | An assert statement should not be used as demonstrated in the program. The boolean expression of the do-loop depends on the value of the local variable i1. The value of i1 is set within the boolean expression of the assert statement. If assertions are disabled, then the boolean expression of the assert statement is not processed and the value of i1 is not updated with each iteration of the loop; so the loop runs indefinitely. |
| 18 | e | Compile-time error at line 5. | The local variable y has not been initialized so the attempt to access the variable results in a compile-time error. |
| 19 | c | Prints: 1, 2, 3, 4, 9, | The expression can be simplified as follows: 1 + 2 % 3 * 4 = 9. The original expression is as follows: m(m(1) + m(2) % m(3) * m(4)). Simplification step one. Evaluate each operand from left to right: m(1 + 2 % 3 * 4). Step two. Add parentheses to indicate operator precedence and associativity: m(1 + ((2 % 3) * 4). Step three. Evaluate the inner-most parentheses: m(1 + (2 * 4)). Step four. Evaluate the inner-most parentheses: m(1 + 8). The result is 9. |
| 20 | c | 3 | Short is signed and char is not signed so an explicit cast is necessary when a short is assigned to a char and vice versa. |
| 21 | d | Run-time error at line 2 | The compiler accepts the explicit cast at line 2, but an error is generated at run-time. Type Base is the super class of type Sub, so an instance of type Base can not be converted to the type of the subclass, Sub. |
| 22 | e | Compile-time error at line 3. | Although the referenced object is indeed an array of type int, an explicit cast is necessary to cast the obj reference to an int array. |
| 23 | a | Prints: float,float | A method invocation conversion can widen an argument of type float to match a method parameter of type double, so any argument that can be passed to m(float i) without generating a compile-time type error can also be passed to m(double i). For that reason, we can say that m(float i) is more specific than m(double i). The arguments of the method invocation expressions, m(a1) and m(b1), are of types char and long respectively. A method invocation conversion can widen an argument of type char or long to match either of the two method parameter types float or double; so both methods, m(float i) and m(double i), are applicable to the two method invocation expressions. Since both methods are applicable, the more specific of the two, m(float i) is chosen rather than the less specific, m(double i). |
| 24 | f | Compile-time error | Suppose a superclass method is not private and is accessible to code in a subclass. If the superclass method is declared static, then any subclass method sharing the same signature must also be declared static. Similarly, if the superclass method is declared non-static, then any subclass method sharing the same signature must also be declared non-static. The attempted declaration of the non-static method D.printS2 generates a compile-time error; because the superclass method, C.printS2, is static. |
| 25 | b | s2 | Please note that this question asks which variable can NOT be substituted. Class Z is a static member class of class B; therefore, all of the static fields and methods of the enclosing class B are available to Z. For example, from within the static member class Z, the static field s1 of the enclosing class B can be accessed using the simple name s1. The simple name s1 does not need to be qualified with a reference to an instance of the enclosing class B, because s1 is not associated with a particular instance of the enclosing class. In contrast, non-static fields and methods of the enclosing class B are associated with a particular instance of class B. From the static context of class Z, the instance fields and methods of the enclosing class B can be accessed only if the simple name of the instance field or method is qualified with a reference to a specific instance of class B. Suppose a reference variable r1 refers to an instance of the enclosing class B. Then the instance member s2 of the enclosing class instance referenced by r1 could be accessed using the expression r1.s2. |
| 26 | b g i | goto implements const | The words virtual, ifdef, typedef, friend, struct and union are all words related to the C programming language. Although the words const and goto are also related to the C programming language, they are also Java keywords. |
| 27 | b | Prints: 1,2,3,4,1 | The expression can be simplified as follows: j = 1 + (2 * 3) + 4 = 11. The original expression is as follows: j = m(i++) + m(i++) * m(i++) + m(i++). The method, m, prints and then returns the value of the parameter, so the original expression is equivalent to the following: j = i++ + i++ * i++ + i++. Step one. Work through the expression from left to right to evaluate the unary expressions: j = 1 + 2 * 3 + 4. Step two. Add parentheses to indicate operator precedence: j = 1 + (2 * 3) + 4. Step three. Work through the simplified expression: j = 1 + 6 + 4 = 11. Step four. Evaluate the expression that is the argument of the print method: j % 5 = 11 % 5 = 1. |
| 28 | a | Prints: Dog,Cat | Although the reference parameters pet1 and pet2 are reassigned inside of m1, the change has no impact outside of m1. Object references are passed by value: the invoked method gets a copy of the object reference. |
| 29 | c | The program compiles, runs and prints X. |
The parameter
i
of method
m1
is a copy of the local variable
i
of method
|