MCQs > IT & Programming > Java > What would happen on compiling and running the following code? public class Test { public static void main (String args[]) { TestThread t = new TestThread (); t.setValue (5); t.start (); t.setValue (10); } } class TestThread extends Thread { private int value; synchronized public void setValue (int v) { value = v; } public void run () { System.out.println ('before: ' + value); setValue (50); System.out.println (after: ' + value); } }

Java MCQs

What would happen on compiling and running the following code?

 public class Test
 {
     public static void main (String args[])
     {
     TestThread t = new TestThread ();
     t.setValue (5);
     t.start ();
     t.setValue (10);
     }
 }
class TestThread extends Thread
     {
     private int value;
     synchronized public void setValue (int v)
     {
         value = v;
     }
     public void run ()
     {
     System.out.println ("before: " + value);
     setValue (50);
     System.out.println ("after: " + value);
     }
 }

Answer

Correct Answer: When run, this code will always print ''before: 5'' and ''after: 10''

Explanation:

Note: This Question is unanswered, help us to find answer for this one

Java Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it

search

Java Skill Assessment

Overall Skill Level-Poor

Your Skill Level: Poor

Retake Quizzes to improve it