wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mock question from khalid Moughal 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 "mock question from khalid Moughal" Watch "mock question from khalid Moughal" New topic
Author

mock question from khalid Moughal

Udaya Raj
Greenhorn

Joined: Nov 01, 2002
Posts: 26
Given the following code, which statements are true?


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 ]

IBM Certified WebSphere 4.0 Specialist<br />IBM Certified in OOAD and UML
Chris Allen
Ranch Hand

Joined: Feb 01, 2003
Posts: 127
Originally posted by Udaya Prakash Dorai Raj:
Given the following code, which statements are true?
<CODE>

public class Vertical {
private int alt;
public synchronized void up() {
++alt;
}
public void down() {
--alt;
}
public synchronized void jump() {
int a = alt;
up();
down();
assert(a == alt);
}
}
</CODE>

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)
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

I think code will not compile.

you will get warning that

warning: as of release 1.4, assert is a keyword, and may not be used as an identifier

but same time reason for which code will not compile is that it will not be able to resolve assert.

error will be "cannot resolve symbol"
[ October 23, 2004: Message edited by: Shailesh Chandra ]

Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
I only see one correct choice among those presented. Here goes:

1. The code will fail to compile
If the code fails to compile, none of the other options are viable because they occur only at runtime.

If this code is compiled with the "Enable Assert Keyword" or the like option in your IDE or with, in some form, the -ea switch at the command line, the code will compile.

If we agree to run this under Java 1.4 and set the appropriate switches, we can discuss the other choices.

2. Seperate threads can execute the up method concurrently
Separate threads cannot execute a synchronized 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
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
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.

I think that Choice 4 is written ambiguously.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: mock question from khalid Moughal