aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection Questions 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 Questions" Watch "Garbage Collection Questions" New topic
Author

Garbage Collection Questions

Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37
Here are exam qns from MindQ

1. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4


2. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

I think the answers are
1.a (0) String literal pool will be referring to all objects
2.b finalise method has to be called atleast once ..

Both were given as wrong...

Could somebody explain this
Ann Sebastian
Ranch Hand

Joined: Sep 21, 2005
Posts: 37
I got the Second Question..
Ans is a,b and c and I agree to that..

For the first quetstion the given ans is 1.
Which object is eligible for GC
cathymala louis
Ranch Hand

Joined: Nov 02, 2005
Posts: 77
I think the name object is eligible for GC. Ans. is One Object.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
There's some good news if you're studying for the Java 5 exam... because of some of the vaguaries associated with the String pool, you won't have to study how String objects relate to garbage collection. Any other type of object is fair game, but don't worry about the GC and the String pool!
[ December 02, 2005: Message edited by: Bert Bates ]

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Fes D Gaur
Ranch Hand

Joined: Apr 29, 2005
Posts: 54
1. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4


2. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

I think the answers are
1.a (0) String literal pool will be referring to all objects
2.b finalise method has to be called atleast once ..

Here's my take on it:

Question 1: There is one object that is eligible for Gargbage collection. That object is "Nick". The newName reference first refers to "Nick" and then points to "Jason" leaving "Nick" without a reference and making it eligible for Garbage Collection. All other objects have references.

Question 2. I think the correct answer is b. One of the very few guarantees the Garbage Collection in Java provides is that before an object is garbage collected its finalize method (if it has one) will be invoked. I don't know of any runtime object that is used to explicitely make GC run. I don't think you need to call a super class if you have a finalize method in your class. Lastly GC behavior is by no means predictable.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Bert]: There's some good news if you're studying for the Java 5 exam...

I had the impression this was true all along - that the actual SCJP exam has always avoided GC questions that depend on knowing about String literals and the intern pool. It's possible there were some questions like this in the earliest days of the exam, but I think they're probably long gone. I know that when I took the 1.4 exam there were no problems on my exam that depended on pooling. Of course I didn't see all the possible questions. But I and other people in this forum have stated at various times in the past that the real exam didn't seem to have any such questions, and no one's stepped up to contradict this, so I'm inclined to think it's true.

Of course, there are many, many mock exam questions that depend (perhaps inadvertently) on String pooling, and many people here discuss these questions. But I have the strong impression from personal observation, and reports from others, that it's been a non-issue on the real exam for a long time. Do you have reason to believe otherwise?


"I'm not back." - Bill Harding, Twister
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
Jim -

I'm pretty darned sure that you're correct... like I'd bet $100 on it, but I wouldn't bet my life

So 100% sure on 1.5 and 99% sure on 1.4

- Bert
kalvin chain
Greenhorn

Joined: Nov 11, 2005
Posts: 1
I agree with ann
1. 0
2. b
Mahendar Reddy
Ranch Hand

Joined: Sep 27, 2005
Posts: 73
Originally posted by Ann Sebastian:
Here are exam qns from MindQ

1. How many objects are eligible for garbage collection once execution has reached the line labeled Line A?

String name;
String newName = "Nick";
newName = "Jason";
name = "Frieda";

String newestName = name;

name = null;
//Line A

a) 0
b) 1
c) 2
d) 3
e) 4


2. Which of the following statements about Java's garbage collection are true?

a) The garbage collector can be invoked explicitly using a Runtime object.
b) The finalize method is always called before an object is garbage collected.
c) Any class that includes a finalize method should invoke its superclass' finalize method.
d) Garbage collection behavior is very predictable.

I think the answers are
1.a (0) String literal pool will be referring to all objects
2.b finalise method has to be called atleast once ..

Both were given as wrong...

Could somebody explain this


Hi friends...
The answers are... 1) 0. 2)c (also 'a' if we have to select 2 options)
Regarding second question:
a)We can request for the GC using RT, but we r not sure it is invoked or not...
b)finalize method is executed only once when there is no reference pointing the object.finalize() it self can pass "this" as an argument to some other method which creates reference to "this". If after some time it becomes eligible for GC then it won't invoke finalize() again.


SCJP- 98%<br />SCWCD-92%
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection Questions