Which of this method can be used to make the main thread to be executed last among all the threads

Thread thread has been made by using Runnable interface, hence it is necessary to use inherited abstract method run() method to specify instructions to be implemented on the thread, since no run() method is used it gives a compilation error.

This set of Java Assessment Questions and Answers on “isAlive(), Join() & Thread Synchronization”.

1. Which of this method can be used to make the main thread to be executed last among all the threads?
a) stop()
b) sleep()
c) join()
d) call()

Answer: b
Clarification: By calling sleep() within main(), with long enough delay to ensure that all child threads terminate prior to the main thread.

2. Which of this method is used to find out that a thread is still running or not?
a) run()
b) Alive()
c) isAlive()
d) checkRun()

Answer: c
Clarification: The isAlive( ) method returns true if the thread upon which it is called is still running. It returns false otherwise.

3. What is the default value of priority variable MIN_PRIORITY AND MAX_PRIORITY?
a) 0 & 256
b) 0 & 1
c) 1 & 10
d) 1 & 256

Answer: c
Clarification: None.

4. Which of these method waits for the thread to terminate?
a) sleep()
b) isAlive()
c) join()
d) stop()

Answer: c
Clarification: None.

5. Which of these method is used to explicitly set the priority of a thread?
a) set()
b) make()
c) setPriority()
d) makePriority()

Answer: c
Clarification: The default value of priority given to a thread is 5 but we can explicitly change that value between the permitted values 1 & 10, this is done by using the method setPriority().

6. What is synchronization in reference to a thread?
a) It’s a process of handling situations when two or more threads need access to a shared resource
b) It’s a process by which many thread are able to access same shared resource simultaneously
c) It’s a process by which a method is able to access many different threads simultaneously
d) It’s a method that allow too many threads to access any information require

Answer: a
Clarification: When two or more threads need to access the same shared resource, they need some way to ensure that the resource will be used by only one thread at a time, the process by which this is achieved is called synchronization

7. What will be the output of the following Java code?

  1.     class newthread extends Thread 
  2.     {
  3. 	newthread()
  4.         {
  5. 	    super("My Thread");
  6. 	    start();
  7. 	}
  8. 	public void run()
  9.         {
  10. 	    System.out.println(this);
  11. 	}
  12.     {
    1
  13.     {
    2
  14.     {
  15.     {
    4
  16.         {
  17.     {
    6
  18.     {
    7
  19.     {
    1

a) My Thread
b) Thread[My Thread,5,main]
c) Compilation Error
d) Runtime Error

Answer: b
Clarification: Although we have not created any object of thread class still we can make a thread pointing to main method, we can refer it by using this.
Output:

    {
9

8. What will be the output of the following Java code?

  1.     class newthread extends Thread 
  2.     {
  3. 	newthread()
    2
  4. 	newthread()
  5.         {
  6. 	newthread()
    5
  7. 	newthread()
    6
  8. 	}
  9. 	public void run()
  10.         {
  11.         {
    0
  12.         {
    1
  13.         {
    2
  14.         {
    3
  15.         {
    4
  16.         {
    5
  17.         {
    1
  18.         {
    7
  19.         {
    4
  20. 	}
  21.     {
    1
  22.     {
    2
  23.     {
  24.     {
    4
  25.         {
  26.     {
    6
  27.     {
    7
  28.     {
    1

a) My Thread
b) Thread[My Thread,5,main]
c) Exception
d) Runtime Error

Answer: d
Clarification: join() method of Thread class waits for thread being called to finish or terminate, but here we have no condition which can terminate the thread, hence code ‘t.join()’ leads to runtime error and nothing will be printed on the screen.
Output:

	    super("My Thread");
8

Which method is used to hold the execution of a thread?

sleep() Method: Method Whenever Thread. sleep() functions to execute, it always pauses the current thread execution. If any other thread interrupts when the thread is sleeping, then InterruptedException will be thrown.

Which method is used to make main thread?

A thread can be created by implementing the Runnable interface and overriding the run() method. The Main thread in Java is the one that begins executing when the program starts.

How can we make sure main () is the last thread to finish in Java program?

You need to use thread. join() . Your main should join with the thread that it spawns and the sub-thread needs to join with the threads that it spawns as well.

Which method is used in thread class to starts the execution of the thread Mcq?

start() method is used to begin execution of the thread that is execution of run().