Menu
Topics Index
...
`

Threads1604


If you need explanation Read this topic

If you need Answer Take test on this topic

Threads1604
What will be out put of the following program?
public class Cruiser {
    private int a = 0;
    public void meth() {
        Runnable r = new LittleCruiser();
        new Thread(r).start();
        new Thread(r).start();
    }
    public static void main(String arg[]) {
        Cruiser c = new Cruiser();
        c.meth();
    }
    public class LittleCruiser implements Runnable {
        public void run() {
            int current = 0;
            for (int i = 0; i < 4; i++) {
                current = a;
                System.out.print(current + ", ");
                a = current + 2;
            }
        }
    }
}

A.  0, 2, 4, 6, 0, 2, 4, 6
B.  0, 2, 4, 6, 8, 10, 12, 14,
C.  0, 2, 4, 6, 8, 10, 2, 4,
D.  0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,
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.

One possible answer is B
Two Threads are being started
the flow of execution depends on the switching of the threads
so output isnt unique
depends on the machine on which it is being run

Posted by Kittu Manoj    2014-04-25 08:43:42


ans is d since 2 threads are started at the same time so it is opt d it depends on switching of threads so o/p may vary

Posted by Teja Sri Arikati    2014-04-25 09:04:14


the ans is A as loop iterates till current =6 and the switch of threads not take place i.e, after the 1st thread executes the 2nd thread executes

Posted by Anusha Reddy    2014-04-25 13:05:18


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

Posted by Merit Campus    2014-04-28 04:14:14


© meritcampus 2019

All Rights Reserved.

Open In App