• 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

confuse on the answer

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whthout/with the synchronized keyword will very different answer! Becareful if anyone meet this question again.
haha...
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
What kind of corn soldier are you? And don't say "kernel" - that's only for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic