File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection II Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection II" Watch "Garbage Collection II" New topic
Author

Garbage Collection II

bobby chaurasia
Ranch Hand

Joined: Mar 30, 2002
Posts: 84
I am a bit flustered ....
Case 1
For the following question...
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
a1[1] = new I("B"); // 3
a1[2] = new I("C"); // 4
m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}
After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.
Ans : e
Reason : A copy of the refrence a1 is set to null in the method m1 but not the original .
Fine....
Case 2:
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[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);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}

After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.
Ans : a,b,c
Reason : Method m1 sets all elements of the array to null so the objects created on lines 2, 3 and 4 are all eligible for garbage collection when method m1 returns.
Question : Isnt a copy of the refrence to the array, passed to m1? so why should the answer be a,b,c and not e.
Case 3 :
What would be the result of this....
class I {
private String name;
public String toString() {return name;}
public I(String s) {name = s;}
}
class J {
private 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
new J.m1(a1);
for (int i = 0; i < a1.length; i++) {
System.out.print(a1[i]);
}
}
}

After method m1 returns the objects on which of the following lines are eligible for garbage collection?
a. 1
b. 2
c. 3
d. 4
e. None of the above.
f. Compiler error.
g. Run time error.
h. None of the above.
Thanks !
bobby chaurasia
Ranch Hand

Joined: Mar 30, 2002
Posts: 84
Can anybody throw some light on this please ?
Thanks !
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8803
    
    5
Bobby -
Case 2: Actually answers b,c, and d refer to lines 2, 3 and 4 - (are you trying to trick us ) so i think the answer is b,c, d.
The reason is that we're talking about 4 objects here,
1 array object
3 class I objects
You can still get to (refer to), the array object, but how do you get to the three class I objects?
Case 3 - why do you think this is different than case 2 from a GC perspective?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection II
 
Similar Threads
garbage collection doubt
A question on Dan's GC Question
Garbage Collection
Dan exam doubt 19
garbage collection