Menu
Question Index
...

What will be the output of the following program?

public class SpiderMan implements Runnable {
    int x = 0, y = 0;
    int addX() { x++; return x; }
    int addY() { y++; return y; }
    public void run() {
        for (int i = 0; i < 5; i++)
            System.out.print(addX() + " " + addY() + ", ");
    }
    public static void main(String args[]) {
        SpiderMan run1 = new SpiderMan();
        SpiderMan run2 = new SpiderMan();
        Thread t1 = new Thread(run1);
        Thread t2 = new Thread(run2);
        t1.run();
        t2.run();
    }
}


1 1, 2 2, 3 3, 4 4, 5 5,
1 1, 1 1, 2 2, 2 2, 3 3, 3 3, 4 4, 4 4, 5 5, 5 5,
0 0, 1 1, 2 2, 3 3, 4 4,
1 1, 2 2, 3 3, 4 4, 5 5, 1 1, 2 2, 3 3, 4 4, 5 5,
Output cannot determined
Compilation Error because there is no run().

Doubts

Problems

Topic: What is Multitasking and Multithreading?

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App