File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes Garbage Collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic
Author

Garbage Collection

Bhavana Gupta
Greenhorn

Joined: Dec 14, 2000
Posts: 6
Q25 At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: String anObj = "sample";
06:
07: anObj.trim();
08:
09: anObj = anObj.toUpperCase();
10:
11: anObj = null;
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) At line 5
b) At line 7
c) At line 8
d) At line 10
e) At line 12
26) At what point is the object anObj available for garbage collection.
01: public class Base{
02:
03: private void test() {
04:
05: if(true) {
06: String anObj = "sample";
07: String locObj = anObj;
08: anObj.trim();
09: anObj = null;
10: locObj.trim();
11: }
12: }
13:
14: static public void main(String[] a) {
15: new Base().test();
16: }
17:
18: }
Select most appropriate answer
a) After line 7
b) After line 8
c) After line 9
d) After line 10
e) After line 11
Ans. they have given for 25 is D and for 26 is E what i feel it should be E and C respectively , Pls help, i'm confused

Find A Purpose In Life So Big That It Will Challenge Every Capacity To Be At Your Best.
rajani peddi
Ranch Hand

Joined: Nov 27, 2000
Posts: 73
I think that the answers must be E & E.
In the second question since locObj is having a reference to anObj at line 7 it is eligible for GC only after line 11.

Correct me if i am wrong.
rajani
Ahmad_Rahmani
Greenhorn

Joined: Dec 27, 2000
Posts: 3
The object is elligible for garbage collection when there is no refrences to it. therfore in question 25 the refrence to anObj ends in line 10 implicitly, and in line 11 is explicitly mentioned.
As for question 26 I think it the correct answer should be "d", i.e. "after line 10" which means line 11 and not "e. after line 11" which means line 12. My reasons remain the same as question 25.
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Bhavana,
in my opinion the answers are correct.And here is my explanations:
for q25:the question should be:when the object reffer by anObj will be available for garbage collection.
So,after anObj.toUpperCase(),a new String is created and reference will points to another object.Your first object("sample") will be now eligible for gc
for q26:both anObj & locObj(maybe from local ?!) points to "sample".ok?one reference is set to null,but the other(locObj) remain until the scope of the block.When it finishes the last reference -locObj will vanishes.So the answer is e.
if i'm wrong,please correct me
rgds,
Cristi

------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection