Which of the following statements about this code are true? class A extends Thread{ public void run(){ for(int i =0; i < 2; i++){ System.out.println(i); } } } public class Test{ public static void main(String argv[]){ Test t = new Test(); t.check(new A(){}); } public void check(A a){ a.start(); } }

  1. 0 0

  2. Compilation error, class A has no start method

  3. 0 1

  4. Compilation succeed but runtime exception


Correct Option: C
Explanation:

To understand the code, the user needs to know the basics of Java threads and anonymous inner classes.

The code defines a class A that extends Thread and overrides its run() method to print the values of i from 0 to 1. It also defines a class Test with a main() method that creates an instance of Test and calls its check() method with an anonymous inner class that extends A. The check() method calls the start() method on the A instance, which starts a new thread that executes the run() method.

Now let's go through each option and explain why it is true or false:

A. 0 0: This option is false. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

B. Compilation error, class A has no start method: This option is false. The A class extends Thread, which provides the start() method. Thus, there is no compilation error.

C. 0 1: This option is true. The code creates a new thread that executes the run() method of the A instance, which prints the values of i from 0 to 1. Since the start() method is called only once, the output will be either "0 1" or "1 0", depending on which thread runs first.

D. Compilation succeed but runtime exception: This option is false. There is no runtime exception in the code.

Therefore, the correct answer is:

The Answer is: C. 0 1

Find more quizzes: