• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

A Synchronized Qestion

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
107
====
Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. public synchronized void setX (int i) { x = i;}
5. public synchronized void setY (int i) { y = i;}
6. public synchronized void setXY(int i) {setX(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.
=============================================
I cannot confirm the corrected ans is A or C. Anyone could give me the explanation? Thank you!
 
Richard Yi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Pls refer to below question as well. The only difference between these 2 (Q57 & Q107) is at line6 of the program: one is synchronized but the other is not.
The standard ans for Q57 is B. C is incorrect ans so I think it is incorrect for Q107 as well (But I cannot catch the real reason
). Thus A will be the ans for my first question(Q107), is this right?
Q57. Given:
1. public class SyncTest {
2. private int x;
3. private int y;
4. private synchronized void setX (int i) {x=i ;}
5. private synchronized void setY (int i) {y=i ;}
6. public void setXY(int 1){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.
[ February 09, 2004: Message edited by: Richard Yi ]
 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Richard,
The answer for Q.57 is B, that's correct and I am thinking that the answer for Q.107 is A.
I assume that the question is trying to test our knowledge of threading and sychronized word. And I also assume that they are trying to tell us that if a method is synchronized, then we cannot access the object and change instance values.
I think I could not justify my answer of A for Q.107. Please correct me if I am wrong.
Thanks.
 
Richard Yi
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Satish! I think there is another difference between these 2 Q:
setX() and setY() in Q107 are public, however, in Q57 they are private.
I think it is a typo for Q107(i.e.they should have been private!) for option D will be non-sense if they are public! (It originally allow x and y to be set separately, NOT need change!)
Thus the ans should be A.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic