aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads questions from Sun's Assessment exam 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 "Threads questions from Sun Watch "Threads questions from Sun New topic
Author

Threads questions from Sun's Assessment exam

Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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 ]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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 ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
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 !!
long meng
Ranch Hand

Joined: Oct 10, 2008
Posts: 58
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


SCJP 5.0 98%<br />SCWCD 5.0 in progress . . .
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads questions from Sun's Assessment exam