| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | Prints: XXYY |
The program will not print
XXYY.
Please note that the question asks which could
NOT
be a result of attempting to
compile and run the program.
The finalize method of each instance can only run once;
so X or Y can never be printed more than once.
The instances referenced by
x1
and
y1
become
eligible for garbage collection
when method
m
returns; so both could be finalized at
that time, but there is no guarantee that they will
be. Even though
|
| 2 | e | Indeterminate. | If method m2 causes a reference to be saved for each object that is passed in as an argument, then none of the objects are eligible for garbage collection when line 3 begins. Since we don't know what method m2 might be doing, we can not know if the objects are eligible for garbage collection. |
| 3 | c | 9 | With each pass through the loop, q1 references a new object, and the old object becomes eligible for garbage collection. When the processing of line 2 begins, the last object referenced by q1 is not eligible for garbage collection. |
| 4 | g | None of the above |
Please note that this question asks which object is
NOT
eligible for garbage collection after method
m1
returns.
The objects referenced by
i1,
i2
and
i3
form a ring such that each object is
referenced by another.
Even so, nothing outside of method
|
| 5 | c | C | Please note that this question asks which objects are NOT eligible for garbage collection when method m2 begins to execute? All three references, i1, i2 and i3, refer to object named C; so C is not eligible for garbage collection when method m2 begins to execute. The objects named A and B have references to each other, but no other objects refer to A and B. The objects A and B form an island of islolated objects and are eligible for garbage collection. |
| 6 | a | 1 | Please note that this question asks which objects are NOT eligible for garbage collection after method m1 returns. After method m1 returns, the array a1 created on line 1 is not eligible for garbage collection. Method m1 sets all elements of the array to null; so the objects created on lines 2, 3 and 4 are eligible for garbage collection when method m1 returns. |
| 7 | g | None of the above |
After method
m1
returns, none of the objects are
eligible for garbage collection.
Method
m1
sets the parameter variable
a1
to
null,
but that does
not change the reference
a1
in the
|
| 8 | c | The program compiles, runs and prints X. |
The parameter
i
of method
m1
is a copy of the local variable
i
of method
|