• 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

mock question from khalid Moughal

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic