Need help understanding the following
SCJP question :
public class SychTest{
private int x;
private int y;
public void setX(int i){ x=i;}
public void setY(int i){y=i;}
public Synchronized void setXY(int i){
setX(i);
setY(i);
}
public Synchronized boolean check(){
return x!=y;
}
}
Under which conditions will check() return true when called from a different class?
A.check() can never return true.
B.check() can return true when setXY is callled by multiple threads.
C.check() can return true when multiple threads call setX and setY separately.
D.check() can only return true if SychTest is changed allow x and y to be set separately.
What I have understood till now is that when ever 'x' and 'y' are equal check() will return 'FALSE' (because the condition checked is x!=y , not x==y). Since what ever value is passed to setXY(int i) , the same value will be assigned to x and y , and x!=y will thus always return 'FALSE'.
Another possibility is that, if we change the method setXY(int i) to accept two int parameters instead of one, and assign the different parameters to x x and y in the following manner. Now if while invoking the method setXY() two different numbers as passed like this, setXY(22,20); the condition x!=y will be rendered' 'TRUE':
setXY(int i, int j)
{
setX(i);
setY(j);
}
In the light of the above two possibilities, I feel that A and D should be the correct answers.
However majority of sites state that "C" is supposed to be the answer. Can someone be kind enough to write a code and explain it, demonstrating that "C" is the correct answer.