• Post Reply 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads questions from Sun's Assessment exam

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Source: Sun's Assessment Exam for SCJP 5

The questions on threads were really difficult for me. I worked out the questions based on my understanding but could not get them right. I am seeking the help of ranchers in finding out where I went wrong.

Question 1:

Options were: (choose 2)
in pre
pre in
in post pre
in pre post
pre in post
pre post in

My answer: in pre post, pre in post

---------------------------------------------------------------------------

Question 2:


Options were: (Choose 1)
The output can never contain the value 10.
The output can never contain the value 30.
The output can never contain the value 297.
The output can never contain the value 820.
The output can never contain the value 1010.

I think the 3rd option is right..If so, how is it correct?

---------------------------------------------------------------------------
Question 3:


Options were: (Choose 2)
6 7 8 9
6 7 8 6
6 7 8 6 7 8
6 7 8 6 7 9
6 7 8 8 6 7
6 7 8 6 6 7 8
6 7 8 9 6 7 8 9
My answer: 6 7 8 6 7 8 , 6 7 8 8 6 7
---------------------------------------------------------------------------

If some one could explain me, in brief atleast, the gist of these, I would be pleased.

Thanks in advance.

[ November 12, 2008: Message edited by: Rekha Srinath ]
[ November 12, 2008: Message edited by: Rekha Srinath ]
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all Rekha Use One Thread Per Question

1. You are correct. The output can be in pre post OR pre in post. This is because start is called before displaying pre. So in can be displayed before pre. But post will always be displayed after in as there is t.join call before that.

2. You are right. The third option is correct. This is a tricky question.First of all run method is synchronized. So only one method can execute it at a time. Tread.yield() doesn't looses the object lock so that that is also just tricky. The if (i%10 != 0) { i++; } part will never be executed. This is because the first time a thread enters value of i will be 0. So the condition will be false. Then that thread will set the value of i to 10. So when the next thread will enter run, the condition will be again false and thus 20 will be displayed. So the output will multiples of 10 i.e. 10,20,30,40 .... 1010.

3. You are right again. The first three threads will enter the if block. The last one will enter the else block. So there will be 6 values displayed. This rules out option 1,2,6 and 7. Each value will be displayed twice. This rules out option 4. The values can appear in any order as notify and notifyAll may wake any of the waiting threads...
[ November 12, 2008: Message edited by: Ankit Garg ]
 
Rekha Srinath
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot about the "1 thread/question" rule as I was excited to get the answers :-)

And I am glad that my answers are correct...

Thanks Ankit !!
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
about the 1st question, i think the answer should be only "pre in post",because when the thread t started, it got to sleep,and the thread main should run ,so the "pre" must show before the "in", Ankit please tell me where i'm wrong
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
long you are right that in most of the cases the output will be pre in post. But it is POSSIBLE that the output will be in pre post. This is an extreme case where main will not run i.e. get a chance to run while the thread t is asleep for 2 seconds. I know that this is most unlikely to happen in real life but the output is surely a possibility
 
reply
    Bookmark Topic Watch Topic
  • New Topic