Option B is correct because in the first line of main we're constructing an instance of an anonymous inner class extending from MyThread. So the MyThread constructor runs and prints "MyThread". The next statement in main invokes start() on the new thread instance, which causes the overridden run() method (the run() method defined in the anonymous inner class) to be invoked, which prints "foo"
When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.
public class Q126 implements Runnable
private int x;
private int y;
public static void main(String  args)
Q126 that = new Q126();
(new Thread(that)).start( ); /* Line 8 */
(new Thread(that)).start( ); /* Line 9 */
public synchronized void run( ) /* Line 11 */
for (;;) /* Line 13 */
System.out.println("x = " + x + "y = " + y);
The program prints pairs of values for x and y that are always the same on the same line (for example, "x=1, y=1". In addition, each value appears once (for example, "x=1, y=1" followed by "x=2, y=2")
Answer: Option D
The synchronized code is the key to answering this question. Because x and y are both incremented inside the synchronized method they are always incremented together. Also keep in mind that the two threads share the same reference to the Q126 object.
Also note that because of the infinite loop at line 13, only one thread ever gets to execute.