Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage Collection and Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage Collection and Threads" Watch "Garbage Collection and Threads" New topic
Author

Garbage Collection and Threads

Kunal Goel
Greenhorn

Joined: Sep 08, 2002
Posts: 16
This is for the first time that I am posting a query.
Following are two questions that I had come across in a mock exam, I don’t recollect the location of the exam.
Question 1
The programmer can signify through a reference that an object is no longer needed - True/False
My Answer
I think this statement is both true and false.
True part
If you want to tell the system that you done with the object, all you have to do is to remove all references to it like this:
myObj=null;
False part
I think the answer is false since an object can have more than one reference. Hence, one reference cannot always determine the fate of an object.
Question 2
Which is true about threads?
a. If suspended cannot be restarted
b. Stop running,if another thread with higher priority is getting runnable.
c. Stop running, if the thread starts another thread with the same priority.
d. Stop running, if the thread creates another thread.
e. If interrupted, stop running.
f. If dead cannot be restarted.
The answer given is b,c,e,f.
What about option (a), calling resume() (although depreciated) on a suspended thread wil put the same in Runnable state. Here if I am not mistaken “restarted” is not a call to start() method, but the Thread in Running state.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For the first question I would say that setting the last reference to an object to null would make that object eligible for garbage collection. That's how I would interpret the question anyway.
For the second question: a) is not true because, as you said, the thread can be resumed at a later time. So a) is not an valid answer to the question.
The wording of the question is vague, in my opinion b) and c) could better start off with "May stop..."
or "Possibly stop..."
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
These are not well-worded questions. 99% of the questions on the current 1.4 exam are *not* ambiguous.
That said, here's what I think of the answers...
Question 2:
Ignore anything about suspend/resume. Long gone.
The ONLY correct answers are "b" and "f".
Answer c and d are false because there's no guarantee about that behavior.
But answer e is also false. A thread does NOT necessarily stop running when it is interrupted. In fact, normally the thread that gets "interrupted" is NOT the running thread (although it is possible for a thread to interrupt itself). Normally the running thread sends a signal to another thread by interrupting it, but what the interrupted thread does with that "knowledge" is up to the interrupted thread. Interrupt is just a flag, although some methods (sleep(), wait(), join() can throw interrupted exceptions -- which the thread itself won't even know about until it actually becomes the running thread (although it is likely to become the running thread more quickly if it is interrupted because it will move *out* of a blocked state. So, not only will interrupting a thread NOT stop it from running, but interrupting it is more likely to get it to actually *begin* running again!

Question 1:
I agree with your statements. There is no way to know, from this question, if the programmer is holding the *last* reference. So might be true, might be false. You won't have questions like this on the exam. Use of the word "might" "must" "will" etc. are used carefully in the exam.
cheers,
Kathy


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Garbage Collection and Threads