Menu
Topics Index
...
`

Runnable


If you need explanation Read this topic

If you need Answer Take test on this topic

Runnable
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();
    }
}

A. 1 1, 2 2, 3 3, 4 4, 5 5,
B. 1 1, 1 1, 2 2, 2 2, 3 3, 3 3, 4 4, 4 4, 5 5, 5 5,
C. 0 0, 1 1, 2 2, 3 3, 4 4,
D. 1 1, 2 2, 3 3, 4 4, 5 5, 1 1, 2 2, 3 3, 4 4, 5 5,
E. Output cannot determined
F. Compilation Error because there is no run().
Topic: What is Multitasking and Multithreading?

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C

Posted by Bharath Yelchuri    2014-02-26 13:15:58


ans is D since two threads will print the values in the main function so it prints 1 1,22,33,44,55,11,22,33,44,55 as incremented by run method in interface ....:)

Posted by Naga Sinst    2014-02-26 13:25:40


Ans is: D ...because two threads t1 and t2 will be created separately using run method as a parameter .so two threads have
created seprately..so when ever the the run method called here it gets sequential execution..insted of it we callin with start method of thread class then the out put is not determained.. so it prints 1 1, 2 2, 3 3, 4 4, 5 5, 1 1,2 2, 3 3, 4 4, 5 5 , are printed....

Posted by Sapparapu Pradeep Kumar    2014-02-26 17:56:47


Congratulations Javaaspirant Merit. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-02-27 04:22:11


thanks nyc concept programs and pls send me recharge link :)

Posted by Naga Sinst    2014-02-27 04:51:48


Javaaspirant Merit, can you please comment with your real name  to keep the competition spirit. Hope you understand. :-)

Posted by Merit Campus    2014-02-27 06:28:47


my name is nagaraju....:)

Posted by Naga Sinst    2014-02-27 07:44:55


© meritcampus 2019

All Rights Reserved.

Open In App