File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question 8 Valentin's mock

 
Rowan Chattaway
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Graeme Brown
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay......so what happends to:
Question08 q08 = new Question08();
Sorry If I'm being stupid!
Rowan.
 
Graeme Brown
Ranch Hand
Posts: 193
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, I think I've got that now.
Cheers Graeme!
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rowan Chattaway
Ranch Hand
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob, I think what you say makes sense, I'll put that line back.
Thanks
 
Corey McGlone
Ranch Hand
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic