aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection question" Watch "Garbage collection question" New topic
Author

Garbage collection question

Francisco Montes
Ranch Hand

Joined: Sep 30, 2009
Posts: 30
Hi Javaranch,

A question taken from one of those mock exams that you can do at www.javachap.com.

What can be inserted at // insert code here, to make object created at line 5 eligible for garbage collection?



Possible answers (you can only choose one).
a) arr[0] = null;
b) x = null;
c) arr = null;
d) All of the above

I chose d) thinking that if we do a), b) and c) all together, we will be sure that the object will be garbage collected as all references to it will be cleared.
But they state that c) is the correct answer but if that were the case, we would still have x pointing to the Integer object, wouldn´t we?


SCJP 1.6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Yup, C is not the correct answer...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
I think your answer is correct, and the test is wrong.

Your answer would 'nuke the world' - ie, clear all objects, which would include they object they want destroyed.

For C to be correct, would have had to create and initialize an anonymous array - then, when they set the arr to null, it would nuke the anonymous array.

However, they didn't do that - they created the array on line 3, not line 5; therefore, C can't be correct.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18101
    
  39

Francisco Montes wrote:
I chose d) thinking that if we do a), b) and c) all together, we will be sure that the object will be garbage collected as all references to it will be cleared.


I generally read an "all of the above" answer as "all of the previous answers are correct", and not "if you apply all of the previous together, it will be correct". So, IMO, answer D, is also incorrect.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Cindy Carney
Ranch Hand

Joined: Jan 12, 2010
Posts: 57
Ooh, subtle - you're right, that interpretation could be applied. I hope it's clearer than this on the SCJP! I'm taking it today in 2 hours.
Larry Chung
Ranch Hand

Joined: Feb 02, 2010
Posts: 247
Henry Wong wrote:
I generally read an "all of the above" answer as "all of the previous answers are correct", and not "if you apply all of the previous together, it will be correct". So, IMO, answer D, is also incorrect.

Henry


In regular, normal test questions I would apply the same reasoning but be aware that the SCJP exam notoriously has trick questions that defy conventional thinking. So, Francisco is right with answer D and that mock exam at www.javachap.com is defective anyway.


SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Larry Chung wrote:the SCJP exam notoriously has trick questions that defy conventional thinking. So, Francisco is right with answer D

I agree with Henry here, the answer is not D either. All of the above means that all the options are correct individually not together...
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
why is not option D) correct? as it satisfies the choose only one option criteria

also, the question is about "object created at line 5 eligible for garbage collection" it does not care if all the objects(object pointed by "arr", & object pointed by arr[0] and x) are eligible for GC.
if the question is like, "only the object created at line 5 should be eligible for GC" then option D) cant be right.

Since option D) satisfies both, "object created at line 5 eligible for garbage collection" and "choose only one option" , it should be correct.
please explain why D) cant be correct answer?


SCJP 6.0 90%
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Anuradha Prasanna wrote:why is not option D) correct? as it satisfies the choose only one option criteria

Let me show you a question where "all of the above is correct"
A. The program will compile if line 1 is uncommented
B. The program will compile if line 2 is uncommented
C. The program will not compile if line 3 is uncommented
D. The program will not compile if line 4 is uncommented
E. All of the above
Choose only 1 correct answer


Here the answer is E. All the statements A,B,C and D are correct individually, so the answer is all of the above. But all of the statements are not true together. If I uncomment line 1 and 2 both, then the program will not compile. So all of the above doesn't mean all of the above together. But the SCJP exam is not ambiguous at all, so the statement A and B in the actual exam would be something like
A. The program will compile if only line 1 is uncommented
B. The program will compile if only line 2 is uncommented


So in the real exam you won't have to worry about such things.

Also the program that I showed above is not from any mock exam, its just an example that I created...
Anuradha Prasanna
Ranch Hand

Joined: Mar 09, 2006
Posts: 115
Thanks for the clarification, Ankit.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage collection question
 
Similar Threads
eligible for garbage collection
Garbage Collection issue
OCP book (K & B) - Exam2 - Num 50 - GC question
Garbage Collection
Garbage collection