Question:

What will be the output of the program?

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 */
        { 
            x++; 
            y++; 
            System.out.println("x = " + x + "y = " + y); 
        } 
    } 
}
  1. An error at line 11 causes compilation to fail
  2. Errors at lines 8 and 9 cause compilation to fail.
  3. The program prints pairs of values for x and y that might not always be the same on the same line (for example, "x=2, y=1")
  4. 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")

Answers:


Keywords:

© 2017 QuizBucket.org