A is incorrect because static methods can be synchronized; they synchronize on the lock on the instance of class java.lang.Class that represents the class type.
C is incorrect because only methods?not variables?can be marked synchronized.
D is incorrect because a sleeping thread still maintains its locks.
Option B is incorrect because to call wait(), the thread must own the lock on the object that wait() is being invoked on, not the other way around.
Option C is wrong because notify() is defined in java.lang.Object.
Option D is wrong because notify() will not cause a thread to release its locks. The thread can only release its locks by exiting the synchronized code.
Option A is incorrect - just because another thread activates the modify method in A this does not mean that the thread will automatically resume execution
Option C is incorrect - This is incorrect because as said in Answer B notify only wakes the thread but further to this once it is awake it goes back into the stack and awaits execution therefore it is not a "direct and sole consequence of the notify call"
Option D is incorrect - The notify method wakes one waiting thread up. If there are more than one sleeping threads then the choice as to which thread to wake is made by the machine rather than you therefore you cannot guarantee that the notify'ed thread will be the first waiting thread.
Runnable target = new MyRunnable(); Thread myThread = new Thread(target);Which of the following classes can be used to create the target, so that the preceding code compiles correctly?
Option A is incorrect because interfaces are not extended; they are implemented.
Option B is incorrect because even though the class would compile and it has a valid public void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it.
Option D is incorrect because the run() method must be public.
(6) is correct because wait()/notify()/notifyAll() must all be called from within a synchronized, context. A thread must own the lock on the object its invoking wait()/notify()/notifyAll() on.
(1) is incorrect because wait()/notify() will not prevent deadlock.
(2) is incorrect because a sleeping thread will return to runnable when it wakes up, but it might not necessarily resume execution right away. To resume executing, the newly awakened thread must still be moved from runnable to running by the scheduler.
(3) is incorrect because synchronization prevents two or more threads from accessing the same object.
(5) is incorrect because notify() is not overloaded to accept a duration.
class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); } }
(1) is correct - Extending the Thread class and overriding its run method is a valid procedure.
(4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method.
(2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here)
(3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface.
(5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected)
Option A is incorrect because only Java expressions that return a value can be used. For instance, a method that returns void is illegal.
Option C is incorrect because the expression after the colon must have a value.
Option D is incorrect because assertions throw errors and not exceptions, and assertion errors do cause program termination and should not be handled.
Option A is incorrect because at runtime assertions are ignored by default.
Option B is incorrect because as of Java 1.4 you must add the argument -source 1.4 to the command line if you want the compiler to compile assertion statements.
Option D is incorrect because the VM evaluates all assertion flags left to right.
(2) is correct. You're never supposed to handle an assertion failure.
(3) is correct. Assertions let you test your assumptions during development, but the assertion code?in effect?evaporates when the program is deployed, leaving behind no overhead or debugging code to track down and remove.
(4) is wrong. See the explanation for (5) below.
(5) is correct. Assertion checking can be selectively enabled or disabled on a per-package basis. Note that the package default assertion status determines the assertion status for classes initialized in the future that belong to the named package or any of its "subpackages".
The assertion status can be set for a named top-level class and any nested classes contained therein. This setting takes precedence over the class loader's default assertion status, and over any applicable per-package default. If the named class is not a top-level class, the change of status will have no effect on the actual assertion status of any class.
Option B is wrong. Is there such a thing as conditional compilation in Java?
Option C is wrong. For private methods - yes. But do not use assertions to check the parameters of a public method. An assert is inappropriate in public methods because the method guarantees that it will always enforce the argument checks. A public method must check its arguments whether or not assertions are enabled. Further, the assert construct does not throw an exception of the specified type. It can throw only an AssertionError.
Option D is wrong. Because you're never supposed to handle an assertion failure. That means don't catch it with a catch clause and attempt to recover.
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.