| Author |
Garbage Collection
|
Shruthi Molakaseema
Greenhorn
Joined: Jun 10, 2004
Posts: 9
|
|
Hi, I have a small doubt regarding garbage collection, question#1 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 Question#2 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 ]
|
 |
n.chenththuran
Ranch Hand
Joined: Jun 16, 2004
Posts: 41
|
|
hi, this is regarding ur Q2 question Q2- x=4 is correct... whats ur difficulty in this questions
|
 |
n.chenththuran
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 object..so 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
Greenhorn
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. Joe
|
 |
Shruthi Molakaseema
Greenhorn
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... Jules [ August 05, 2004: Message edited by: Julian Kennedy ]
|
 |
 |
|
|
subject: Garbage Collection
|
|
|