Multiple choice technology architecture

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?

  1. public class MyRunnable extends Runnable{public void run(){}}

  2. public class MyRunnable extends Object{public void run(){}}

  3. public class MyRunnable implements Runnable{public void run(){}}

  4. public class MyRunnable implements Runnable{void run(){}}

Reveal answer Fill a bubble to check yourself
C Correct answer
Explanation

To solve this question, the user needs to have knowledge about the concept of threads in Java and how to create a thread using a Runnable target.

The correct answer is:

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

Explanation:

In the given code, we are creating a thread using a Runnable target. The Runnable interface provides a way to implement multi-threading in Java by defining a run() method that will be executed in a separate thread.

To create the target, we need to implement the Runnable interface by providing a definition for the run() method. Option C correctly implements the Runnable interface with a run() method, so it is the correct answer.

Option A is incorrect because it extends the Runnable interface without providing a definition for the run() method.

Option B is incorrect because it extends the Object class and does not implement the Runnable interface.

Option D is incorrect because it implements the Runnable interface but does not provide the run() method with the correct signature (i.e., it should be public void run()).

Therefore, the correct answer is:

The Answer is: C