To determine the output of the given code, let's go through the code step by step:
In the Test
class's main
method:
- Create an instance of the
Test1
class named pm1
and pass the string "One" as an argument to its constructor.
- Call the
run
method of pm1
.
- Create another instance of the
Test1
class named pm2
and pass the string "Two" as an argument to its constructor.
- Call the
run
method of pm2
.
In the Test1
class:
- Declare a private instance variable
sTname
of type String
and initialize it with an empty string.
- Define a constructor that takes a string argument and assigns it to the
sTname
variable.
- Override the
run
method of the Thread
class.
- In the
run
method:
- Iterate from 0 to 1 (two iterations).
- Within each iteration:
- Use the
sleep
method to pause the execution for 1000 milliseconds (1 second).
- Use the
yield
method to give up the CPU to other threads.
- Print the value of
sTname
.
Based on the above code, the output will be:
One
One
Two
Two
Therefore, the correct answer is B) One One Two Two.