The pages of this web site are not formatted to conserve paper, but my new book (ISBN: 0-9745862-0-X) is formatted to save paper, save your printer cartridge, save a loose-leaf binder, and save money. If you prefer to work my exams from printed pages, then give your printer a rest and buy my new book.
Today, you can find my book at amazon.com.
| 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. | Since we don't know what method m2 might be doing, we can not know if the objects are eligible for garbage collection. Suppose that method m2 is declared inside of a class that also contains 10 instance variables (instance variables are non-static member fields) that are references to instances of class A. The argument that appears in the method invocation expression m2(q1) is a reference to an instance of class Q. Suppose that m2 saves each argument value in one of the ten instance variables or in an element of an array of type Q[]. When the loop in method m1 runs to completion, each instance of class Q would still be referenced by a one of the ten instance variables. Since the instance variables would continue to reference each instance of class Q when line 3 is executed, none of the instances would be eligible for garbage collection at that point. A second possibility is that method m2 does not save the reference values. In that case, all of the instances that were created inside the loop would be eligible for garbage collection when line 3 is executed. |
| 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 | a c d e | A1A2A3A1 A1A2A3A1A2A3 A1A2A3A1A1A2A3 A1A2A3A1A3A2A1 |
The three instances of class
A
form an isolated ring where each instance references
the next instance and the third references the first instance.
Four iterations of the
for
loop are processed. Inside the body of the
for
statement, the invocation of the
print
method contains the argument expression
a0 = a0.other().
On the first iteration, the reference variable
a0
references the instance named
A3.
The value returned by the method named
other
is a reference to the instance named
A1.
The reference is assigned to the reference variable
a0
and is also the value produced by the expression
a0 = a0.other().
That reference value is passed as an argument to the
print
method, and the
print
method invokes the
|
| 9 | c d | , Ba, B1 B2, Ba, B1 | Class H declares two static member variables named ba and i. The type of i is int, and the value is initialized to 1. The type of ba is B. The declaration of ba contains the class instance creation expression new B("Ba"). The constructor of class B assigns the argument value to the instance variable called name. Inside the main method of class H, the method invocation expression m1(ba) invokes method m1. The argument is the static member variable ba. The body of method m1 contains a return statement with the expression b = new B("B" + i++). The assignment expression contains the class instance creation expression new B("B" + i++) which creates a new instance of the class B. For this first invocation of method m1, the argument appearing in the class instance creation expression is the String value B1. The reference to the new String is assigned to the parameter variable b, but that assignment does not change the value of the member variable ba. The value of the assignment expression is the reference to the new instance of class B with the name B1, and that reference value is returned by the method m1. The returned value is assigned to the local variable x. The next statement inside the main method is another invocation of method m1. The argument appearing in the method invocation expression m1(x) is the local reference variable x. The method invocation does not change the value of x. The value returned by this second invocation of m1 is a reference to a new instance of class B that has the name B2. The returned reference value is not assigned to a variable, so the instance named B2 is eligible for garbage collection. There is no guarantee that the garbage collector will run before the print statement is invoked. If it does run, then the instance named B2 could be finalized causing the name to be printed. |
| 10 | e | Ba, B1, B2, Bb | Class J declares two static member variables named bc and i. The type of i is int, and the value is initialized to 1. The type of bc is B. Inside the main method of class J, the method invocation expression m1(new B("Ba")) invokes method m1. The argument is the class instance creation expression new B("Ba"). The constructor of class B assigns the argument value to the instance variable called name, so a new instance of class B named Ba is created. The reference to the new instance of class B is the argument that is passed to method m1. The body of method m1 contains two statements. The first contains the assignment expression bc = b that assigns the value of the method parameter b to the static member variable bc, so bc now references the instance of class B named Ba. The second statement in the body of m1 is a return statement with the class instance creation expression new B("B" + i++). For this first invocation of method m1, the argument appearing in the class instance creation expression is the String value B1. The reference to the new String is returned by method m1. The returned value is assigned to the local variable x. Inside the main method, the declaration of the local variable y contains another invocation of method m1. The argument appearing in the method invocation expression m1(new B("Bb")) is the class instance creation expression new B("Bb"), so the argument value for the invocation of method m1 is a reference to a new instance of class B named Bb. Inside the body of method m1, the reference to the new instance of class B named Bb is assigned to the static member variable Bc. At that point, the instance of class B named Ba becomes eligible for garbage collection. Method m1 returns a reference to a new instance of class B named B2. There is no guarantee that the garbage collector will run before the print statement is invoked. If it does run, then the instance named Ba could be finalized causing the name to be printed. |
| 11 | c | The program compiles, runs and prints X. |
The parameter
i
of method
m1
is a copy of the local variable
i
of method
|