Menu
Topics Index
...
`

Bomb Explosion


If you need explanation Read this topic

If you need Answer Take test on this topic

Bomb Explosion
What will be the output of the following program?
public class ThreadTest {
    public static void main(String[] args) {
        Bomb b1 = new Bomb("Bomb 1", 2500);
        Bomb b2 = new Bomb("Bomb 2", 1000);
        Bomb b3 = new Bomb("Bomb 3", 500);
        b1.start();
        b2.run();
        new Thread(b3).start();
    }
}
class Bomb extends Thread implements Runnable {
    String name;
    int timeToWait;
    public Bomb(String name, int timeToWait) {
        this.name = name;
        this.timeToWait = timeToWait;
    }
    public void run() {
        try { Thread.sleep(timeToWait);    } catch (Exception e) {    }
        System.out.println(name + " exploded");
    }
}

A. Bomb 2 exploded
Bomb 3 exploded
Bomb 1 exploded
B. Bomb 1 exploded
Bomb 2 exploded
Bomb 3 exploded
C. Bomb 3 exploded
Bomb 2 exploded
Bomb 1 exploded
D. Compilation Error
E. Runtime Error or goes into infinite loop
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 A after calling start method by by b1 it will create new thread in run method it does not create new thread so in the same thread b2 is executed then b3 is having another thread so it will execute next then it comes to b1

Posted by Bheema Reddy    2014-03-18 08:39:12


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

Posted by Merit Campus    2014-03-19 04:25:42


© meritcampus 2019

All Rights Reserved.

Open In App