File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Mock3 Q#44 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 "Marcus Mock3 Q#44" Watch "Marcus Mock3 Q#44" New topic

Marcus Mock3 Q#44

marie m

Joined: Sep 27, 2000
Posts: 7
Given a reference t to a class which extends Thread, which of the ff will cause it to give up cycles to allow another thread to execute
1) t.yield();
2) yield()
3) yield (100)
4) yield(t);
the answer given was 2 only
i would think #1 is also a valid answer (if you don't consider the fact that 2 and 3 does not have semi-colons and won't even compile)
since yield is a static method of the Thread class, couldn't it also be called/referenced to by an instance of Thread? and since t extends Thread then x instanceof Thread should return true.
any comments?
Mapraputa Is
Leverager of our synergies

Joined: Aug 26, 2000
Posts: 10065
Answer1 is correct answer in sense that it will compile and cause the same result as yield(). It is recommended to use
yield() instead of t.yield() because last variant is misleading.
When you call static method yield() and there are more than one threads, it will stop currently running thread (which, BTW, means a thread in which yield() call occured) - and it may be not "t" thread.
But since the question asks "which of the ff will cause it to give up cycles to allow another thread to execute" - answer 1 is correct.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
I agree, 2 would not compile. I also read this question to be that reference t IS a class that extends Thread given to some THING of unknown type.
Even if we put a ';' at the end of 2, the class executing the statement would have to be of type Thread for the static 'yield();' to work. So I think 1 is the only correct answer.
Otherwise, it should read "a class which extends Thread is given a reference to an object t,....". Then I can see 2 with a semicolon is correct.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I agree with Mapraputa. I got this answer wrong when I took the mock test coz I had answered both 1 & 2.
Scott, you are right in a way, but I believe the question is really not intended to test the ';' aspect of the question. Therefore, we have to assume that Marcus indeed meant yield();
Also, in the real exam you do not have these kind of typos and the questions are (most of the time) very clear and precise.
Jim Goodwin

Joined: Sep 15, 2000
Posts: 14
Scott's remark was a bit unclear to me, but I think
he means, and I think I agree, that simply writing
won't work unless it is written in a class that
inherits from Thread.
As I understand it, the preferred way to invoke
this method is by "Thread.yield();" which works anywhere
and avoids all ambiguity. But that option isn't given in
the question. Hmmm....
scott irwin
Ranch Hand

Joined: Aug 07, 2000
Posts: 87
Correct. I could not tell from the wording if I (the class) was a thread or I (the class) was given a thread. I read it to be I was given a thread. Therefore, t.yield(); would be correct. If I was a thread, then a simple yield(); would work and the bit about giving a reference was just to cloud the question.
marie m

Joined: Sep 27, 2000
Posts: 7
i understood this question to be t is a reference to a class that extends Thread, i.e., t = new ClassThatextendsThread();, which means t is an instanceof Thread
so answer #1) t.yield();
though not recommended should be correct and if the statement is within the class declaration of t itself, then i can understand that 2 will also be correct...
i wonder what Marcus has to say about this?
Marcus Green
arch rival

Joined: Sep 14, 1999
Posts: 2813
Hmm this question was supposed to be testing the knowlege that calling yield will cause the currently executing thread to give up cycles but I guess it doesn't do that very well, and I'll fix those semi-colons.
How could it be phrased better? God I hate threads but they are important. I own the O'reilly/Oaks book on the subject and it is not very good. I have just been reading the reviews of another book on the subject at Amazon that gets good reviews (Hyde)

[This message has been edited by Marcus Green (edited September 28, 2000).]

SCWCD: Online Course, 50,000+ words and 200+ questions
I agree. Here's the link:
subject: Marcus Mock3 Q#44
It's not a secret anymore!