This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Khalids Mock exam  RSS feed

 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any thread may call the methods a, b and c at any time?
class Counter{
int v = 0;
synchronized void inc() { v++;}
synchronized void dec() { v--;}
}
public class Q7ed5{
Counter i;
Counter j;
Counter k;
public synchronized void a(){
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b(){
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c(){
k.inc();
System.out.println("c");
k.dec();
}
}
1. i.v is guaranteed always to be 0 or 1
2. j.v is guaranteed always to be 0 or 1
3. k.v is guaranteed always to be 0 or 1
4. j.v will always be greater than or equal to k.v at any given time
5. k.v will always be greater than or equal to j.v at any given time
Can you please let me know the correct answer for this.
Thanks


------------------
Ch. Vijayalakshmi
 
sunilkumar ssuparasmul
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Refer to previous 2 day post the answer are i and ii
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!