Question discussion

What is the output of the following Java program?public class Main { public static void main(String[] args) throws Exception { Thread t = n…

2119Medium DSSSB TGT Computer Science 0 replies 1 views
0

What is the output of the following Java program?

public class Main {
    public static void main(String[] args) throws Exception {
        Thread t = new Thread(() -> System.out.print("T"));
        t.start();
        t.join();
        System.out.print("M");
    }
}
  • AMT
  • BTM
  • CT
  • DThe order of the output is unpredictable
Correct answer: B

Explanation

join() makes the main thread wait until thread t has finished, so T is always printed before M.

Discussion 0

Nobody has replied yet. Start the discussion!