Java Reference
In-Depth Information
9.
D. The code compiles, so E is incorrect. The Car object on line 8 has an initial velocity of
10 from line 19. The call to go on line 9 changes its velocity to 20. The stolen reference
points to the same Car object, so calling go with the stolen argument changes the Car
object's velocity to 30, so the correct answer is D.
10. B. The code compiles successfully, so F is incorrect. The two GregorianCalendar
references are passed to the swap method, which does not change either object. In fact, the
only thing swapped in the swap method is b getting assigned to a, but these changes do
not affect the references one and two. Because the objects that one and two refer to are not
changed in the swap method, the output is 20102011 and B is the correct answer.
11. D. The object on line 4 is referred to by the temp reference, which goes out of scope after
line 5. However, the result reference gets a copy of temp, so it refers to the “Jane Doe” object
until line 12 when result is set to null, at which point “Jane Doe” is no longer reachable
and becomes immediately eligible for garbage collection. Therefore, the answer is D.
12. A. Line 5 generates a possible loss of precision compiler error. The cast operator has the
highest precedence, so it is evaluated fi rst, casting a to a byte (which is fi ne). Then the
addition is evaluated, causing both a and b to be promoted to ints. The value 90, stored
as an int, is assigned to sum, which is a byte. This requires a cast, so the code does not
compile and therefore the correct answer is A. (This code would compile if parentheses
were used around (a + b).)
13. B. The * and % operators have the same level or precedence and are therefore evaluated
left-to-right. The result of 5 * 4 is 20 and 20 % 3 is 2 (20 divided by 3 is 18; the
remainder is 2). Therefore, the answer is B.
14. E. To evaluate the & operator, you need to express the numbers in binary and evaluate & on
each column, as shown here:
14 = 0000 1110
9 = 0000 1001
14&9 = 0000 1000
The resulting binary number 00001000 is 8 in decimal, so the answer is E.
15. C. The code compiles successfully, so D is incorrect. Due to the unpredictable behavior of
System.gc, the output cannot be determined. The House object from line 4 is eligible for
garbage collection after line 6, and the call to System.gc may free its memory and cause
“Inside House” to be displayed from the finalize method. However, the System.gc
method may not free the memory of the House object, in which case there would be no
output. Because A or B may occur, the answer is C.
16. A and D. Just before an object is garbage collected, its finalize method is invoked once,
so A is true but B is incorrect. C is incorrect because it is just not a true statement. D is
correct; there is no need to assign address to null because it is about to be deleted from
memory. E is incorrect, though, because address may not be the only reference to the
String object that address refers to.
Search WWH ::




Custom Search