1)
class I {
private I other;
public void other(I i) {other = i;}
}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);i3.other(i2); i4.other(i4);
}
public static void main (String[] args) {
new J().m1();
}}
Which object is not eligible for garbage collection after method m1 returns?
A. i1,i2,i3 & i4
B. Compile-time error
C. Run-time error
D. None of the above
Answer: D
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 J.m1 references any of those objects. When method J.m1 returns, the ring becomes an island of isolated objects that are not reachable by any part of the user program. A key point to remember is that an object that is referenced by another object can be eligible for garbage collection if the two objects form an island of isolated objects.
2)
class B {
private String name;
public B(String s) {name = s;}
protected void finalize() {System.out.print(name);}
}
class E {
public static void m() {
B x1 = new B("X"), y1 = new B("Y");
}
public static void main(String[] args) {
m(); System.gc();
}}
Which of the following could not be a result of attempting to compile and run the program?
A. Prints: XY
B. Prints: YX
C. Prints: XXYY
D. Nothing is printed.
Answer: C
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.
3)
class I {private I other;public void other(I i) {other = i;}}
class J { private I i1 = new I(), i2 = new I(), i3 = new I();
private void m1() {i1.other(i2); i2.other(i1); i3.other(i3);
i1 = i3; i2 = i3;
m2();}
private void m2() {}
public static void main (String[] args) {
new J().m1();}}
//Which of the three objects, A, B or C, is not eligible for garbage collection when
method m2 begins to execute?
A. i1
B. i2
C. i3
D. None of the above
Answer: C
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& B.The objects A and B form an island of islolated objects and are eligible for garbage collection
4)
class B {private String name;
public B(String name) {this.name = name;}
protected void finalize() {System.out.print(name);}
}class H {
static B ba = new B("Ba");
static int i = 1;
static B m1(B b) {return b = new B("B" + i++);}
public static void main (String[] args) {
B x = m1(ba); m1(x); //Line 9
}}
Which object is eligible for garbage collection after line 9 is executed?
A. Ba
B. B1
C. B2
D. None of the above
Answer: C
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.
5)
class I {
private String name;
public String name() {return name;}
public I(String s) {name = s;}
}
class J {
public static void m1(I i) {i = null;}
public static void main (String[] args) {
I i = new I("X"); // 1
m1(i); // 2
System.out.print(i.name()); // 3
}}
Which of the following is a true statement?
A. The object created a line 1 is eligible for garbage collection after line 2.
B. A NullPointerException is generated at line 3.
C. The program compiles, runs and prints X.
D. The program fails to compile.
Answer: C
The parameter i of method m1 is a copy of the local variable i of method J.main. Setting the parameter variable i of method m1 to null does not change the local variable i of method J.main.
6)
void m1() {
Q q1 = null;
for (int i = 0; i < 10; i++) {
q1 = new Q(); // 1
m2(q1); // 2
}
System.out.print("All done"); // 3
}
When the processing of line 3 begins, how many objects of type Q that were created at
line 1 have become eligible for garbage collection?
A. 0
B. 9
C. 10
D. Indeterminate.
Answer: D
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 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.So none of the instances would be eligible for garbage collection at that point.
7)
class I {private String name;
protected void finalize() {System.out.print(name);}
public I(String s) {name = s;}}
class J {
private static void m1(I[] a1) {
a1[0] = a1[1] = a1[2] = null;
}public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);System.gc();}}
After method m1 returns, the object created on which line is not eligible for garbage
collection?
A. 1
B. 2
C. 3
D. 4
Answer: A
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.
8)
class B {
private String name;
public B(String name) {this.name = name;}
protected void finalize() {System.out.print(name);}
}class J { static B bc;static int i = 1;
static B m1(B b) {bc = b; return new B("B" + i++);}
public static void main (String[] args) {
B x = m1(new B("Ba")), y = m1(new B("Bb"));//Line 8
}}
Which object is eligible for garbage collection after line 8 is executed?
A. Ba
B. Bb
C. B1
D. b2
Answer: A
During first call of m1() , bc references the instance of class B named Ba. In the second call bc references instance of the call B named Bb. Then after the execution of line 8 Ba become eligible for garbage collection.
9)
class Q {
private int id;
protected void finalize() {System.out.print(id);}
public Q(int i) {id = i;}
}
class R {
public static void main(String[] args) {
Q q1 = null;
for (int i = 0; i < 10; i++) {q1 = new Q(i);} // 1
System.gc(); // 2
}}
When the processing of line 2 begins, how many objects of type Q that were created at
line 1 have become eligible for garbage collection?
A, 0
B. 9
C. 10
D. Indeterminate.
Answer: B
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.
10)
class I { private String name;
public String toString() {return name;}
public I(String s) {name = s;}}
class J {
private static void m1(I[] a1) {a1 = null;}
public static void main (String[] args) {
I[] a1 = new I[3]; // 1
a1[0] = new I("A"); // 2
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}}}
After method m1 returns, the object created on which line is eligible for garbage
collection?
A. 1
B. 2
C. Run-time error
D. None of the above
Answer: D
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 J.main method. Since array a1 continues to reference all three objects, none of the three are eligible for garbage collection.
No comments:
Post a Comment