aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confuse on the answer 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 "confuse on the answer" Watch "confuse on the answer" New topic
Author

confuse on the answer

Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) {x=1;}
5. public synchronized void setY (int i) {y=1;}
6. public synchronized void setXY(int i) {set X(i); setY(i);}
7. public synchronized boolean check() {return x !=y ;}
8. }
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 called by multiple threads.
C. Check() can return true when multiple threads call setX and setY separately.
D. Check() can only return true if SyncTest is changed to allow x and y to be set separately.
The answer from the mock paper is A, but I regards it should C. Anyone would explain for it if you are agree A?
[ Jess disabled smilies ]
[ Kelvin changed all the illegal syntax error ( ) --> } )]
[ November 16, 2002: Message edited by: Jessica Sant ]
[ November 17, 2002: Message edited by: Kelvin Michael ]

SCJP1.4
Jamal Hasanov
Ranch Hand

Joined: Jan 08, 2002
Posts: 411
Hi, Kelvin
Yes, it's a very tricky question:-)) At first, it confused me,as well... In setX(int) and setY(int) methods value of X and Y set to 1,not to i!!! That's why X and Y are always equal !
I suggest you to read questions carefully and don't hurry ro answer.
Regards,
Jamal Hasanov
www.j-think.com
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
thx,Jamal. I found two version about this thread at mock exam, but they are difference.

[ November 16, 2002: Message edited by: Kelvin Michael ]
[ November 16, 2002: Message edited by: Kelvin Michael ]
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
Whthout/with the synchronized keyword will very different answer! Becareful if anyone meet this question again.
haha...
Abu Yoosuf
Ranch Hand

Joined: Nov 14, 2002
Posts: 33

What happens if we come across a scenario where by:
SyncTest is new'ed - SyncTest test = new SyncTest();
this results in x = 0; y = 0;
Thread A calls setX(i);
x = 1; y = 0;
Thread B calls check();
shouldn't it return true for the evaluation of x!=y ???
[ Jess adjusted the [code] so its not all on one line ]
[ November 16, 2002: Message edited by: Jessica Sant ]
Abu Yoosuf
Ranch Hand

Joined: Nov 14, 2002
Posts: 33
Sorry Jessica,
Had to go out soon after posting the message and didn't wait to see what I posted.
Thanks for formatting the msg.
[ From Jess: no problem -- that's why I'm here ]
[ November 16, 2002: Message edited by: Jessica Sant ]
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Consider the code below

When it runs it returns true..
So how come the answer is check() never returns true?
Thanks


Sunita<br />SCJP 1.4
Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
Jessica,
Pls note the setX, setY method, no matter when you call it, it always set the x = 1, y = 1. so that is reason why cannot return true.
Thanks for disable the smilies!
[ November 17, 2002: Message edited by: Kelvin Michael ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confuse on the answer