Java Reference
In-Depth Information
Answers to Review Questions
1.
A. The code does not contain any compiler errors. It is valid to defi ne multiple classes in a
single fi le as long as only one of them is public and the others have the default access.
2.
C and E. C assigns the -classpath fl ag to the appropriate directory. E also set the class
path correctly except -cp is used. The -cp and -classpath fl ags are identical. A uses an
equals sign = with the -classpath fl ag, which is not the correct syntax. B and D set the
class path to the wrong directory and also incorrectly refer to the Main class without its
fully qualifi ed name, which is com.mycompany.Main.
3. E. The -d fl ag creates the appropriate directory structure that matches the package name.
In this case, that directory created is c:\abcproject\deploy\a\b\c. Therefore, C and D
are wrong. A NoClassDefFoundError occurs if the compiler cannot fi nd the source fi le, but
in this example the javac command is executed from the same directory that contains the
source fi le, so this error does not occur. A ClassNotFoundException is a runtime exception
that is not thrown by a compiler, so B is incorrect.
4.
A. Based on the defi nition of the equals method, two Employee objects are equal if they
have the same employeeId fi eld, so line 21 evaluates to true and “Success” is output, so
B is incorrect. Line 6 successfully overrides hashCode, so C is incorrect. Line 10 is a valid
overriding of equals, so D and E are incorrect.
5.
A. B is incorrect because hashCode does not have to return a unique value (not that the
compiler could determine if the value was unique anyway). C is incorrect because the
equals method correctly overrides equals in Object. D is incorrect because a ClassCast-
Exception does not need to be handled or declared. E is incorrect because although ISBN is
a private fi eld, the equals method is within the class and therefore has access to the pri-
vate fi eld. Therefore, the code compiles successfully and the answer is A.
6. C. The reference s1 points to a String object in the string pool because “Canada” is a
literal string known at compile time. The reference s2 points to a String object created
dynamically at runtime, so this object is created on the heap. Therefore B is incorrect
because s1 and s2 point to different objects. However, C is correct because s1 and s2 are
both String objects that equal “Canada”, so s1.equals(s2) evaluates to true. Because C
is correct, A and D must be incorrect.
7.
B and D. The Date object from line 5 has two references to it — one and three —
and becomes eligible for garbage collection after line 10, so B is a true statement. The refer-
ence four is set to null on line 9, which does not affect the object from line 5. The Date
object from line 6 only has a single reference to it — two — and therefore becomes eligible
for garbage collection after line 11 when two is set to null, so D is a true statement.
8.
A. A top-level class cannot be declared private, so line 1 causes a compiler error. This
is one of those exam questions where you might waste a couple of minutes if you do not
notice the compiler error right away. Don't forget to keep an eye out for these subtle types
of compiler errors.
Search WWH ::




Custom Search