Bill Brogden's Exam Cram Question seems to be wrong
Varsha Dighe
Ranch Hand
Joined: Oct 14, 2000
Posts: 32
posted
0
William Brogdens Exam Cram Q7 on GC It on page 163 and goes like this Q:Here is a method that creates a number of String objects in the course of printing a countdown sequence. 1. public void countDown() { 2. for(int i = 10; i >= 0; i--) { 3. String tmp = Integer.toString(i); 4. System.out.println(tmp); 5. } 6. System.out.println("Boom!"); 7. }
When the program reaches line 6, how many of the String objects created in line 3 are eligible for garbage collection? (Assum that the System.out object is not keeping a reference.) Answers a - None b - 1 c. 10 d. 11 Correct answer is c (as per William). I was thinking d was correct because the variable tmp which is a local variable also goes out of scope when the program reaches line 6 does'nt it ? ..which would add 1 to the objects eligible for gc after line 6 i.e 11 objects Any help will be appreciated Varsha Nachane
[This message has been edited by Varsha Dighe (edited February 16, 2001).]
Regards, Manfred. [This message has been edited by Manfred Leonhardt (edited February 16, 2001).] [This message has been edited by Manfred Leonhardt (edited February 16, 2001).]
Varsha Dighe
Ranch Hand
Joined: Oct 14, 2000
Posts: 32
posted
0
Thanks a lot.. I thought my fundamentals have gotten wrong.. I am delighted that my answer was correct as felt by other people on this forum. -Varsha
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
There is an extensive discussion of that question at http://www.lanw.com/java/localvariables.htm It turns out that the answer 10 was correct with early JVMs but that the recent JVMs are smarter. You could have found out about this by getting the errata through our errata system at: http://www.lanw.com/books/errata/ If you thought the question is in error, why didn't you write me? My email address is in the book. Bill
Bill is correct. The older JVMs would have given an answer of 10. I can't wait until the GC is smart enough to look ahead within its scope and determine an object isn't being referenced after a certain point and GC at that point. Currently, you have to set the object to null. -Peter
Varsha Dighe
Ranch Hand
Joined: Oct 14, 2000
Posts: 32
posted
0
Bill, First I thought of writing to you, but then I just wanted to make sure my understanding was correct before sending it to you as an error. I also knew you quite often visit this forum. Probably, I should have searched for the topic in the previous posts... believe me I hate to swamp this forum with questions repeatedly if they have been discussed already. Bill/Peter If such type of question comes in the exam, should we base our answer keeping "dump" JVM in mind or "intelligent" JVM in mind ? Thanks for your help varsha
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
Fortunately, you will not get any questions that depend on the oddities of JVM implementation. I would certainly assume a "perfect" JVM when answering questions. Be sure to visit the errata site no matter which study guide you are using. Some books have been published with real howlers in them. In my company we try to keep the errata up to date by using a dynamic system rather than a static errata page, but we still get behind some times. Visit: http://www.lanw.com/books/errata/ Bill