Java Reference
In-Depth Information
Answers to Review Questions
1.
E. If a constructor does not call this or super on its fi rst line of code, the compiler inserts
the statement super();, which occurs in the Rectangle class just after line 15. A call to
super() in Rectangle invokes a no-argument constructor in Shape, but Shape does not
have a no-argument constructor. The compiler error occurs at line 15, so the answer is E.
2. A. The statement new Child() invokes the constructor on line 12. The call to this(123)
invokes the constructor on line 8, which calls super() implicitly before line 9. The call to
super() invokes the constructor on line 3, where A is printed. Control jumps back to line 9
and B is printed. Control jumps back to line 14 and C is printed.
3.
A, B, and E. A is valid because you can use the dollar sign in identifi ers. B is valid because
the underscore is a valid Java character. C is not a valid identifi er because transient is
a Java keyword. D is not valid because the dot (.) is not allowed in identifi ers. E is valid
because Java is case sensitive, so Public is not a keyword and therefore a valid identifi er.
F is not valid because the fi rst character is not a letter.
4.
E. The code compiles fi ne, so A, B, and C are incorrect. Boolean fi elds initialize to false
and references initialize to null, so empty is false and brand is null. Therefore, line 7 is
true and Brand = null is output. Therefore, D is incorrect and the answer is E.
5.
C. The code compiles fi ne, so E is incorrect. Because explicit initialization occurs before
a constructor is invoked, line 2 executes before the Television constructor on line 4 is
executed. The 7 is output on line 11, then the constructor is invoked and 12 is output.
Therefore, the output is 7 12, so the answer is C.
6.
B, C, D, and E. The code does not compile, so A is incorrect. Line 5 is not valid because
globalKey has the default access and School is in a different package than ClassRoom.
Line 6 is not valid for the same reason: the ClassRoom constructor has default access so
School does not have access to it. Line 7 is not valid because roomNumber is private and
therefore not accessible outside of ClassRoom. Line 8 is not valid because teacherName
is protected and School is neither in the same package nor a subclass of ClassRoom.
Therefore, the answers are B, C, D, and E.
7.
B. E is incorrect. Without any imports, the Administrator class will not compile because
line 5 of Administrator refers to globalKey, a static fi eld in ClassRoom.
A imports the ClassRoom class, which is a valid import but does not import globalKey.
B imports all static fi elds of ClassRoom, so B is a correct answer. C and D are not valid
statements and generate compiler errors. Therefore, the only correct answer is B.
8. E. The printScores method takes in a variable-length argument on line 2 and it is
correctly declared, so A is incorrect. Line 9 is a valid array initializer statement, so B is
incorrect. A variable-length parameter is an array behind the scenes and can accept an
array argument, so line 10 is valid and C is incorrect. The code compiles and the for-each
loop displays each number in the array followed by a comma, so D is incorrect and E is the
correct answer.
Search WWH ::




Custom Search