• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection Questions

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the name object is eligible for GC. Ans. is One Object.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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?
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with ann
1. 0
2. b
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic