public class MyRunnable implements Runnable { public void run() { // some code here } }
A is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor.
B is incorrect for the same reason; you can't pass a class or interface name to any constructor.
D is incorrect because MyRunnable doesn't have a start() method, and the only start() method that can start a thread of execution is the start() in the Thread class.
Option B is wrong. notify() - wakes up a single thread that is waiting on this object's monitor.
Option C is wrong. notifyAll() - wakes up all threads that are waiting on this object's monitor.
Option D is wrong. Typically, releasing a lock means the thread holding the lock (in other words, the thread currently in the synchronized method) exits the synchronized method. At that point, the lock is free until some other thread enters a synchronized method on that object. Does entering/exiting synchronized code mean that the thread execution stops? Not necessarily because the thread can still run code that is not synchronized. I think the word directly in the question gives us a clue. Exiting synchronized code does not directly stop the execution of a thread.
Option A is wrong. There is no init() method in the Thread class.
Option C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
Option D is wrong. The resume() method is deprecated. It resumes a suspended thread.
wait(2000);
After calling this method, when will the thread A become a candidate to get another turn at the CPU?
Option B is incorrect because a waiting thread will not return to runnable when the lock is released, unless a notification occurs.
Option C is incorrect because the thread will become a candidate immediately after notification, not two seconds afterwards.
Option D is also incorrect because a thread will not come out of a waiting pool just because a lock has been released.
Option B is wrong. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
Option C is wrong. The stop() method is deprecated. It forces the thread to stop executing.
Option D is wrong. Is the main entry point for an application.
Option B, C, and D are incorrect because they do not define these methods. And yes, the Java API does define a class called Class, though you do not need to know it for the exam.
public class CommandArgsThree { public static void main(String [] args) { String [][] argCopy = new String[2][2]; int x; argCopy[0] = args; x = argCopy[0].length; for (int y = 0; y < x; y++) { System.out.print(" " + argCopy[0][y]); } } }and the command-line invocation is
> java CommandArgsThree 1 2 3
public class X { public static void main(String [] args) { String names [] = new String[5]; for (int x=0; x < args.length; x++) names[x] = args[x]; System.out.println(names[2]); } }and the command line invocation is
> java X a b
> java F0091 world
public class F0091 { public void main( String[] args ) { System.out.println( "Hello" + args[0] ); } }
Exception in thread "main" java.lang.NoSuchMethodError: main
The Java Language Specification clearly states: "The main method must be declared public, static, and void. It must accept a single argument that is an array of strings."
public class CommandArgsTwo { public static void main(String [] argh) { int x; x = argh.length; for (int y = 1; y <= x; y++) { System.out.print(" " + argh[y]); } } }and the command-line invocation is
> java CommandArgsTwo 1 2 3
public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } } class Dog { }
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.