my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Bill Brogden's Exam Cram Question seems to be wrong Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Bill Brogden Watch "Bill Brogden New topic
Author

Bill Brogden's Exam Cram Question seems to be wrong

Varsha Dighe
Ranch Hand

Joined: Oct 14, 2000
Posts: 32
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).]
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi,
Peter Tran has already proven that the correct answer should be 11. See here:
http://www.javaranch.com/ubb/Forum24/HTML/007585.html

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
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: 12769
    
    5
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

------------------
author of:
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
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
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: 12769
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Bill Brogden's Exam Cram Question seems to be wrong