File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question 8  Valentin's mock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question 8  Valentin Watch "Question 8  Valentin New topic
Author

Question 8 Valentin's mock

Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
from Valentin's mock:
Question 8. How many objects are eligible for garbage collection immediately after line 1? Select one correct answer.
public class Question08 {
public static void main(String[] args) {
Question08 q08 = new Question08();
q08.doSomething();//line 1
}
public void doSomething(){
Object[] objArray = new Object[2];
for(int i=0;i<objArray.length;i++){
objArray[i] = new Object();
}
}
}


A. 0
B. 1
C. 2
D. 3
E. 4

ANSWER****************8
Question 8: You answered: E . That's wrong! Correct answer: D
Comments:
Relevant sections of the JLS:
10 Arrays
14.13 The for Statement
15.10 Array Creation Expressions
--------------------------------------------------------------------------------
We declare an array of Object of length two. We then initialize each element to a new Object. We have 2 objects in the array and the array itself (which is an object, too!), that makes 3.
(Thanks to Rajinder Yadav, Rob Ross and Corey McGlone for pointing out mistakes)
Okay, I understand all this, but what happen's to the Object:
Question08 q08 = new Question08();
thats declared in main. When main returns i.e. after line 1, isn't that object available for garbage collection as well?
Sorry If I'm getting confused!
Rowan


The early bird may get the worm, but the second mouse gets the cheese.........<br /> <br />Sun Certified Programmer for Java 2 Platform<br />Sun Certified Web-Component Developer for J2EE Platform
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Your understanding seems OK to me. Yes immediately after line 1 there are the three objects available for GC.
What happens when main terminates, well there are now no user threads running so the program will terminate, no need for GC.
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
okay......so what happends to:
Question08 q08 = new Question08();
Sorry If I'm being stupid!
Rowan.
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Technically speaking it is available for GC once main() has terminated. But at that point your program will also have terminated, so there is almost no chance of it actually getting garbage collected.
If you have a finalize() method it may run, but that is dependent on the implementation of the virtual machine.
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
okay, I think I've got that now.
Cheers Graeme!
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Just to clear things up, "just after line 1" also means "just before main ends", that is, between the last statement and the curly bracy '}'. At that point, Question08 is not eligible yet.
If you are interested, we had a discussion about that in the following thread:
New SCJP mock exam


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
okay thanks Valentin.
But lets just so for example the question had have meant when main ends as well, does that mean that:
Question08 q08 = new Question08();
would have been available for gc? and therefore 4 objects?
or does that simply not happen when the program ends?
sorry, but I just want to be 100% sure on exactly what happened in the gc process.
Cheers,
Rowan.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Normally when the programs ends, the JVM frees up the memory, and thus, would garbage collect Question08. But the JVM specification does not specify which garbage collection algorithm to use, and thus, lets the choice to the implementor to garbage collect Question08 or not. For the purpose of the certification, you should consider that all objects are garbage collected when the main method returns.
If you are interested, here is a good article about garbage collection :
http://developer.java.sun.com/developer/technicalArticles/ALT/RefObj
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
That's why I liked it better when you had another line of code just after line 1, so the question was just about the method that just finished running and not about main ending. I think you originally had somethong like Thread.sleep(1000); right after line 1, so clearly the program was still running at this point and main() hadn't exited yet.


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Thanks Rob, I think what you say makes sense, I'll put that line back.
Thanks
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
I know that we argued about this question once before, and just decided that the question was confusing and that Sun would ensure that any questions about garbage collection wouldn't be ambiguous, but, unfortunately, I ran into an almost identical question on my certification exam. Obviously, I can't say what the question was, but there was a bit of ambiguity just like with Val's question here.
Corey


SCJP Tipline, etc.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey and Rob,
I've re-added a statement after line 1 in Question 8 so that there is no ambiguity anymore...
Rowan Chattaway
Ranch Hand

Joined: Jan 18, 2002
Posts: 75
now your just scaring me.................
Think I might re-book my exam for a couple of weeks later........
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question 8 Valentin's mock
 
Similar Threads
Question on garbage collection
New SCJP mock exam
GC Question
garbage collection
Garbage Collection