Output of Java Programs | Set 46 (Multi-Threading)

Prerequisite : Multi-threading in Java

1. What will be the output of the program?

class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
    }
}

Options:
1. One thread created
2. Two thread created
3. Depend upon system
4. No thread created
Output:

 The answer is option (2)

Explanation : In the above program, one thread will be created i.e. the main thread which is responsible to execute the main() method and the child thread will be created after the execution of t.start() which is responsible to execute run() method.

2. What will be the output of the program?

class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.run();
    }
}

Options:
1. One thread created
2. Two thread created
3. Depend upon system
4. No thread created

Output:

 The answer is option (1)

Explanation : In the above program only one thread will be created i.e. the main thread which is responsible to execute the main() method only. The run() method is called by the object t like a normal method.

3. What will be the order of output of the program?

class Test extends Thread {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main");
    }
}

Options:
1. Main Run
2. Run Main
3. Depend upon Program
4. Depend upon JVM

Output:

 The answer is option (4)

Explanation : In the above program, we cant predict the exact order of the output as it is decided by the Thread scheduler which is the part of JVM.

4. What will be the output of the program?

class Test implements Runnable {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Test t = new Test();
        t.start();
        System.out.println("Main");
    }
}

Options:
1. Main Run
2. Run Main
3. Compile time error
4. Depend upon JVM

Output:

 The answer is option (3)

Explanation : In the above program, we will get compile time error because start() method is present in the Thread class only and we are implementing Runnable interface.

5. What will be the output of the program?

class Test implements Runnable {
public
    void run()
    {
        System.out.println("Run");
    }
} class Myclass {
public
    static void main(String[] args)
    {
        Thread t1 = new Thread();
        t1.start();
        System.out.println("Main");
    }
}

Options:
1. Run
2. Main
3. Compile time error
4. Run Main

Output:

 The answer is option (2)

Explanation : In the above program, we are calling start() method of Thread class which is responsible to execute run() method of Thread class and Thread class run() method has empty implementation. That’s why one child thread will be created but it will not execute Test class run() method.

This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.