• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Garbage collection question

 
Francisco Montes
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, C is not the correct answer...
 
Cindy Carney
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Cindy Carney
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ankit Garg
Sheriff
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the clarification, Ankit.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic