To answer this question, let's analyze the given code:
public class Rpcraven {
public static void main(String argv[]){
Pmcraven pm1 = new Pmcraven("one");
pm1.run();
Pmcraven pm2 = new Pmcraven("two");
pm2.run();
}
}
class Pmcraven extends Thread {
private String sTname="";
Pmcraven(String s) {
sTname = s;
}
public void run(){
for(int i =0; i < 2 ; i++){
try {
sleep(1000);
} catch(InterruptedException e){ }
yield();
System.out.println(sTname);
}
}
}
The code defines a class Rpcraven
with a main
method, and a class Pmcraven
that extends Thread
.
In the main
method, two Pmcraven
objects pm1
and pm2
are created with arguments "one" and "two" respectively. The run
method of each object is then called.
In the Pmcraven
class, the run
method contains a loop that runs twice. Inside the loop, the thread sleeps for 1 second, yields the CPU, and then prints the value of sTname
.
Now, let's go through each option to determine which statement is true:
Option A) Compile time error, class Rpcraven
does not import java.lang.Thread
- This option is incorrect. The java.lang.Thread
class is automatically imported by default in Java, so there is no need for an explicit import statement.
Option B) Output of One One Two Two - This option is correct. The program creates two Pmcraven
threads, pm1
and pm2
, with arguments "one" and "two" respectively. When the run
method of each thread is called, it enters a loop that runs twice. In each iteration, it sleeps for 1 second, yields the CPU, and then prints the value of sTname
. Therefore, the output will be "One One Two Two".
Option C) Output of One Two One Two - This option is incorrect. The run
method of pm1
is called before the run
method of pm2
, so it will complete its execution first. Therefore, the output will start with "One" twice, followed by "Two" twice.
Option D) Output of One Two Two One - This option is incorrect. The run
method of pm1
is called before the run
method of pm2
, so it will complete its execution first. Therefore, the output will start with "One" twice, followed by "Two" twice.
The correct answer is option B) Output of One One Two Two.