Java Reference
In-Depth Information
Answers to Review Questions
1. A. Making all of the fi elds of a class private and providing public setter and getter meth-
ods is the defi nition of tight encapsulation, so the answer is A.
2. C. The Television class has a public fi eld channel , so it does not follow tight encapsu-
lation and A is incorrect. It does not refer to any other classes, so it is loosely coupled and B
is incorrect. The methods of Television perform tasks reminiscent of a TV and are closely
related, which is the goal of high cohesion, making D incorrect and C the correct answer.
3. B. The defi nition of loose coupling is to minimize an object's dependencies on other
objects, so the answer is B.
4. D. Information hiding is a benefi t of tight encapsulation, so A is incorrect. Easier code
changes and reuse of code are benefi ts of both tight encapsulation and loose coupling. No
matter how well you design your application, there is always a need to test your code, so D
is the correct answer.
5. D. The only inheritance that does not satisfy the is-a relationship is D, because a
square is not a triangle. Therefore, the answer is D.
6. B. The code compiles and runs fi ne, so C, D, and E are incorrect. The compiler sees the
sayHi method of Parent on line 16, but at runtime the sayHi method of Child is invoked
because the Child class overrides the sayHi method. Therefore, the output is Hello and the
answer is B.
7. E. The code does not compile, so A is incorrect. On line 12, the child class SodaPop
attempts to access the ounces fi eld of its parent class Beverage. Because the ounces fi eld is
private, SodaPop does not have access to it and a compiler error is generated. Therefore,
the answer is E.
8. A. The code compiles and runs fi ne, so C, D, and E are incorrect. The main method is
defi ned within Fruit, so main has access to the private fi eld color. (If main were defi ned
in a different class, then lines 6 and 7 would not compile.) Line 6 changes the color to Red
and line 7 prints it out, so the answer is A.
9. A, B, and E. C is incorrect because it attempts to change the return value of closeWindow in
the parent, which is not allowed. D is incorrect because it has the default access, which is a
weaker access than protected in the parent. A and E successfully override the closeWindow
method in MyWindowCloser, so they are valid methods that could appear in a child class. B is
also valid because it is overloading the closeWindow method (not overriding the method) in
the parent and can have any access modifi er. Therefore, the correct answers are A, B, and E.
10. A. An abstract method can be protected, and a child class can override the method with
public access. The catchAnObject method on line 16 is a valid overloading of the method.
The code compiles fi ne, so C, D, and E are incorrect. Line 7 invokes catchAnObject with
a Date object, which through polymorphism causes the method on line 12 to be invoked.
Therefore, the output is Caught object and the answer is A.
Search WWH ::




Custom Search