Which statement is true for the following 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); } } }

  1. Compile time error, class Rpcraven does not import java.lang.Thread

  2. Output of One One Two Two

  3. Output of One Two One Two

  4. Output of One Two Two One


Correct Option: B

AI Explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) Compile time error, class Rpcraven does not import java.lang.Thread - This option is incorrect. The java.lang.Thread class is part of the Java standard library and does not require an explicit import statement.

Option B) Output of One One Two Two - This option is correct. The main method creates two instances of the Pmcraven class, passing "one" and "two" as arguments to the constructor. Each instance of Pmcraven is a separate thread. When the run method is called on each thread, it prints the value of sTname twice, resulting in the output "One One Two Two".

Option C) Output of One Two One Two - This option is incorrect. The run method of Pmcraven does not alternate the printing of "One" and "Two". Instead, it prints the value of sTname twice in a row before moving on to the next thread.

Option D) Output of One Two Two One - This option is incorrect. The run method of Pmcraven does not print "Two" before printing "One" for the second time. Instead, it prints the value of sTname twice in a row before moving on to the next thread.

The correct answer is B. This option is correct because the output of the code will be "One One Two Two" as explained above.

Find more quizzes: