Output of Java program | Set 16 (Threads)
a) GFG Geeks Geeks for
b) Geeks GFG Geeks for
c) Either a or b
d) Both a and b together
Explanation: From the statement “thread.start()”, we have two threads Main thread and “thread” thread. So either “GFG” can be printed or “Geeks”, depend on which thread, thread scheduler schedule.
For (a), the parent thread after calling start() method is paused and the thread scheduler schedules the child thread which then completes its execution. Following this, the parent thread is scheduled. For (b), the parent thread calls start() method but continues its execution and prints on the console. When join() method is called, the parent thread has to wait for its child to complete its execution. Thread scheduler schedules child thread while the parent waits for the child to complete.
2) What is the output of the following program?
a) GFG GFG TERMINATED
b) GFG TERMINATED
c) Compilation Error
d) Runtime Error
Explanation: Invoking start() method on a thread moves the thread to a RUNNABLE state. But invoking start() method on a thread that has already started throws a IllegalThreadStateException because the thread is already in RUNNABLE state.
3) What is the output of the following program?
a) Runtime error
b) Compilation error
c) GFG GFG
d) None of the above
Explanation: Test class extends Thread class that has start() method implemented. So invoking start() on an object that extends Thread class invokes run() method defined in the program.
4) What is the output of the following program?
a) 30 GFG –
b) GFG – 30
c) GFG –
d) Compilation error
Explanation: An object must first acquire a lock before calling wait() method. Also wait() method throws Checked exception(InterruptedException), we must include it in a try-catch block or throws it.
5)What is the output of the following program?
a) GeeksforGeeks GFG GFG End
b) GFG GeeksforGeeks GFG End
c) GFG GFG GeeksforGeeks End
d) All the above
Explanation: For (a), the parent thread keep executing until it reaches the barrier. The child thread are then scheduled. For (b), thread scheduler scheduler thread1. Once it reaches the barrier, the parent thread is scheduled. Once parent thread reached the barrier, thread2 is scheduled. For (c), Both the child thread are scheduled. Finally when each of the child threads reach the barrier, the parent thread is scheduled.
This article is contributed by Mayank Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.