Threads

Q1: The following block of code creates a Thread using a Runnable target:
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?

A public class MyRunnable extends Runnable{public void run(){}}

B public class MyRunnable extends Object{public void run(){}}

C public class MyRunnable implements Runnable{public void run(){}}

D public class MyRunnable implements Runnable{void run(){}}

ANS:A - public class MyRunnable extends Runnable{public void run(){}}

The class correctly implements the Runnable interface with a legal public void run() method. 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.



img not found
img

For help Students Orientation
Mcqs Questions

One stop destination for examination, preparation, recruitment, and more. Specially designed online test to solve all your preparation worries. Go wherever you want to and practice whenever you want, using the online test platform.