This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock question on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
Author

Mock question on Threads

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

My current question, was already asked some where in Decemeber 2006 and it was abrubtly stopped. I thought of creating fresh thread, instead of appending to the old one.

Now coming to the questions,

QUESTION DELETED

2)

QUESTION DELETED

[/CODE]

Note from Bert: This post contained 2 questions that were real Sun exam questions. Possessing, let alone posting these questions is illegal - these questions are copyrighted material and are the property of Sun microsystems.
End of note.

Why does the run method defined in the subclass of thread supressed the run defined in MyClass. Is that always the run of Thread class takes over the control when compared to that defined in Runnable?
[ September 12, 2007: Message edited by: Bert Bates ]
Dan Walin
Ranch Hand

Joined: Nov 11, 2003
Posts: 109
I would think the answer to the first question would be D and E. The order would be unpredictable but you would expect an equal number of each letter printed out.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks Burkhard
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Dan,

Unfortunately the answer given in the mock is E and G. Did you get a chance to look why G is also an answer?
Prasanna Rajaperumal
Greenhorn

Joined: Aug 28, 2007
Posts: 9
Technically The code may run with no output, exiting normally is the correct option. ruin does not override the run method in thread class .

On a more serious note, i think all the letters have to appear and have to appear 3 times and since the printall method is also synced, only option
is ABCABCABC.

I do not see any other possiblity standing a chance
Lucky J Verma
Ranch Hand

Joined: Apr 11, 2007
Posts: 278
For me answer is ABCABCABC (3times)
but i insert some sleep() in between 3 insertions,anser is like F,G...other options.
while i dont understand.i tried with sleep() cause ,thread can sleep() internally ,without explicitly adding,when CPu not available.
So why output changed.
Since methods are synchronized ,and threads also started in for loop,so no two threads will be in parallel.

My code :
sl.add("A");
sl.add("B");
try{Thread.sleep(1000);}catch(InterruptedException e){}
sl.add("C");
sl.printAll();
Annette Sovereign
Greenhorn

Joined: Jul 12, 2005
Posts: 26
Kota,

can you confirm where you got the question from please?

Thanks


-- just trying to get one foot on the ladder
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Hi Chandra -

It's always important to state the source of your mock questions, in this case it's very important, so please do so.

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello All,

This question was taken from one of the exam simulators my company have for training their associates. Not sure where did they collect these kind of questions.
Carlos Fernandes
Greenhorn

Joined: Sep 11, 2007
Posts: 5
That answer is wrong, F will never happen.
You have to realize that, while the add and print methods are synchronized, no threads hold the lock to the NameList instance for throught the entire thread execution. While the JVM/SO may switch the executing thread whenever they please, they could switch them while they add ("A"), so we could have something like AABC (first print) AABCBC (second print), TOTAL would be AABCAABCBC, but no way three A's could be printed in sequence. the G answer could happen, if, before the first thread prints all, the jvm quickly switched to ther 2nd thread and back, and give it enough time to add another A, so first print all would be ABCA, and the second ABC ABC, total would be ABCAABCABC.

Only options E and G are correct. F is incorrect based on above, and the others are incorrect for obvious reasons.
[ September 11, 2007: Message edited by: Carlos Fernandes ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks for the explanation Carlos

I have very lately realized that I posted my question wrongly. My question was how E and G were the answers, which are explained already in this thread. I guess, I have mislead some of you by talking about the option F. I'm sorry for that.
Annette Sovereign
Greenhorn

Joined: Jul 12, 2005
Posts: 26
Thanks for confirming Chandra. Had me really puzzled there


Happy Studying
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
this thread was closed because real exam questions were being discussed
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mock question on Threads