File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Mock Exam Errata and the fly likes threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Professional Certification » Mock Exam Errata
Reply Bookmark "threads" Watch "threads" New topic
Author

threads

Prasanna Vijaya
Greenhorn

Joined: Oct 21, 2000
Posts: 5
hi all
these are the questions from a mock exam. i'm unable to find the right answer. can any body help me find right answers and with little explanation on how it works.
Question no 1
class A {
private static int x = 0;
synchronized int increment() {
int n=x;
x = n + 1;
return n;
}
}
Is class A thread-safe? Why or why not?
Choice 1
No, because increment() operates on variables outside its scope
Choice 2
No, because "x" is static, and vulnerable to threads operating on different instances
Choice 3
Yes, because its only method, increment(), must acquire the object's monitor
Choice 4
Yes, because increment() is synchronized
Choice 5
Yes, because "x" is private and not available to outside threads
question no 2
How could you coordinate two threads that execute simultaneously inside synchronized methods of the same object?
Choice 1
place the threads in the same ThreadGroup and let the ThreadGroup acquire the object's monitor
Choice 2
use Thread.wait() and Thread.notify()
Choice 3
dynamically control the priorities of the threads within the method
Choice 4
use Thread.yield() to switch between threads at set points
Choice 5
use Thread.sleep()
 
IntelliJ Java IDE
 
subject: threads
 
Threads others viewed
thread-safe
Mock question
Six java questions: Could anyone answer
few questions ...
Problems with importing from another source code
MyEclipse, The Clear Choice