Menu
Question Index
...

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");
    }
}


Bomb 2 exploded
Bomb 3 exploded
Bomb 1 exploded
Bomb 1 exploded
Bomb 2 exploded
Bomb 3 exploded
Bomb 3 exploded
Bomb 2 exploded
Bomb 1 exploded
Compilation Error
Runtime Error or goes into infinite loop

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