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