This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confuse on the answer

 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jamal Hasanov
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whthout/with the synchronized keyword will very different answer! Becareful if anyone meet this question again.
haha...
 
Abu Yoosuf
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the code below

When it runs it returns true..
So how come the answer is check() never returns true?
Thanks
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic