mock question from khalid Moughal

1. The code will fail to compile 2. Seperate threads can execute the up method concurrently 3. Seperate threads can execute the down method concurrently 4. Seperate threads can execute both the up and down methods concurrently 5. The assertion in the jump method will not fail under any circumstances

Can U tell me what is the 2 correct answers for this question [ October 22, 2004: Message edited by: Barry Gaunt ]

1. The code will fail to compile 2. Seperate threads can execute the up method concurrently 3. Seperate threads can execute the down method concurrently 4. Seperate threads can execute both the up and down methods concurrently 5. The assertion in the jump method will not fail under any circumstances

Can U tell me what is the 2 correct answers for this question

I believe the answer to your question is 3 and 4 for the following reasons:

- since the down method is not synchronized, multiple threads can execute it at any given time. - since the up method is synchronized, only one thread can execute it at any given time but a different thread can execute the down method (not the one that is currently executing the up method)

3. Seperate threads can execute the down method concurrently This is true. The down method is not synchronized.

4. Seperate threads can execute both the up and down methods concurrently Apply the && operator to choices 2 && 3, and it evaluates to false.

5. The assertion in the jump method will not fail under any circumstances Here's the interesting one. This, however, is false. It could be true, however, if we could assume that the down method were not called elsewhere. Because the jump method is synchronized, we know only one thread of execution could hold a lock at any time until the method exited; however, the down() is public and may be called in other code.

I think the question would be more interesting if both the up() and down() methods are private to the class, jump() is not synchronized, and the class otherwise exists as written.

Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127

posted Oct 23, 2004 17:45:00

0

Originally posted by J Borderi:

4. Seperate threads can execute both the up and down methods concurrently Apply the && operator to choices 2 && 3, and it evaluates to false.

Hmmm, I don't believe this the case. When one thread is executing a synchronized block of code, it prevents all other threads from executing any other synchronized code. It does NOT however prevent other threads from executing any non-synchronized code contained within the class at the same time. Thus, one thread can be executing the synchronized up method and one or more threads can be executing the down method.

Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151

posted Oct 23, 2004 22:10:00

0

Ahhh.... Interesting in how you have read the choice. Now that I have reread your initial post, I understand your interpretation.

Given that the question writer wanted two correct choices, I believe that your interpretation may be the question writer's interpretation. If the question writer were looking for one correct choice, my interpretation would reflect the author's intention.

The English language, unlike mathematical and computer languages, is often ambiguous. It's what keeps both poets and lawyers in business.