File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from PGJC 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 "Question from PGJC Exam" Watch "Question from PGJC Exam" New topic

Question from PGJC Exam

Sahul Yasin
Ranch Hand

Joined: Aug 28, 2002
Posts: 48
Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any thread may call the methods a(), b() and c() at any time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {;
public synchronized void b() {;;;
i.dec(); j.dec(); k.dec();
public void c() {;
Select 2 correct answers
1. i.v guaranteed always between 0 and 1
2. j.v guaranteed always between 0 and 1
3. k.v guaranteed always between 0 and 1
4. j.v will aways be greater than or equal to k.v at any time
5. k.v will aways be greater than or equal to j.v at any time

Regards,<br />Yasin<br />SCJP,SCWCD,SCBCD
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hi Mohamed,
the code seems to be incomplete, however, I think the answers are 1 and 2.
The key I think is the fact that c() is not synchronized, so 3 will be false. And 5 is definitely false since in b(), there is a moment where k will be less than j. And 4 can also be shown to be false because c() is not synchronized.
Hope this helps.
If you are still confused, let me know, may be we can work this one more.
I agree. Here's the link:
subject: Question from PGJC Exam
It's not a secret anymore!