Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Mock question on Threads

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Burkhard
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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();
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Kota,

can you confirm where you got the question from please?

Thanks
 
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for confirming Chandra. Had me really puzzled there


Happy Studying
 
Bert Bates
author
Posts: 9046
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
this thread was closed because real exam questions were being discussed
 
This will take every ounce of my mental strength! All for a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic