The moose likes Programmer Certification (SCJP/OCPJP) 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 » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection" Watch "Garbage Collection" New topic

Garbage Collection

Shruthi Molakaseema

Joined: Jun 10, 2004
Posts: 9
I have a small doubt regarding garbage collection,


1. class Test{
2. public static void main(String args[])
3. {
4. Test2 t2 = new Test2();
5. Test2 t3;
6. t3 = t2.m1();
7. t3 = t2;
8. t2 = null;
9. System.out.println("t2 is" + t2);
10. }
11. }

12. class Test2 {
13. Test2 m1() {
14. Test2 x = new Test2();
15. return x;
16. }
17. }

Which two statements are true?
a. after line 6 one object is eligible for gc
b. after line 7 one object is eligible for gc
c. after line 7 two objects are eligible for gc
d. after line 8 two objects are eligible for gc
e. an exception occurs at runtime


what is the result?
a. x = 0
b. x = 1
c. x = 2
d. x = 3
e. x = 4
f. compilation fails

answer is e

I have come across these questions in some sample exam.

(added code tags)
[ August 04, 2004: Message edited by: Barry Gaunt ]
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
this is regarding ur Q2 question

Q2- x=4 is correct...

whats ur difficulty in this questions
Ranch Hand

Joined: Jun 16, 2004
Posts: 41
whats ur difficulty in Q1?
once the line 8 executed t2=null; so its eligible ..but t3=t2; already assigned.. so those both are eligible for gc...
Amit Goel
Ranch Hand

Joined: Dec 07, 2000
Posts: 50
hi ..

answer to Q1 .. only one object will be garbage collected that is t2 ..
when we assign objects only shallow copy is done.. so when we created t2 , t3 ..two objects were created but when we assigned t3 = t2.. there was only one object but with two references t3 , t2.. both the references are pointing to same when we wrote t2 = null, the reference t2 became null..but the object is still there as t3 is referring to it.. so the extra object created in the first step will be garbage collected and this null reference will be destroyed..

Q2 ... x= 4 ... i hope it is pretty clear.. don;t know how to writethe explaination..

Amit<br /> <br />The Less I have, The more I gain..Off the Beaten Path, I Reign.
francis joseph

Joined: Jul 31, 2004
Posts: 13
Regarding Q2, the main method is having the access specifier as "private". Is it intentional? If so the program won't print anything. Otherwise the anser is right, ie, x = 4.
Shruthi Molakaseema

Joined: Jun 10, 2004
Posts: 9
For Q#1
the answer given are b and d,

After line 7, the object created in the Test2 class has no refernce so it is eligible for GC. But after 8, t2 is made null, but still t3 is referring the object which is created in the Test class. So, how 2 objects are eligible for GC after line 8. please explain.

For Q#2
main method is declared private, when we run it, we get main is not public
Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
So to paraphrase the last question:

Q: I'm thinking of an integer between 1 and 5 inclusive. What is it?
a) 1
b) 2
c) 3
d) 4
e) 5

A: 6! Didn't you get it? Well you're not very good then, and I'm the Magic Pixie!

An explanation of what would happen with a public main() is below, reformatted for sanity's sake...

[ August 05, 2004: Message edited by: Julian Kennedy ]
I agree. Here's the link:
subject: Garbage Collection
It's not a secret anymore!