A. public int read(int a, int b){return a+b;}
public void set(int a, int b){this.a=a;this.b=b;}
Obviously incorrect. As there is no synchronization, and you have a race condition.
B. public synchronized int read(int a, int b){return a+b;}
public synchronized void set(int a, int b){this.a=a;this.b=b;}
Correct. The synchronization will solve the race condition.
C. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(a){this.a=a;this.b=b;}}
Incorrect. This one is a trick answer. As long as you synchronize on a common object, it should work. However, in this case, a is a primative, which can't be used for synchronization.
D. public int read(int a, int b){synchronized(a){return a+b;}}
public void set(int a, int b){synchronized(b){this.a=a;this.b=b;}}
Incorrect. Same reason as C. And even if it were allowed, it is not the same instance.
E. public synchronized(this) int read(int a, int b){return a+b;}
public synchronized(this) void set(int a, int b){this.a=a;this.b=b;}
Incorrect. Not legal
Java syntax. This one will not compile.
F. public int read(int a, int b){synchronized(this){return a+b;}}
public void set(int a, int b){synchronized(this){this.a=a;this.b=b;}}
Correct. The synchronization on the common instance, this, should solve the race condition. In fact, this is the same instance as used in answer B.
Henry