250+ TOP MCQs on isAlive(), Join() & Thread Synchronization and Answers

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.     }
  13.     class multithreaded_programing
  14.     {
  15.         public static void main(String args[])
  16.         {
  17.             new newthread();        
  18.         }
  19.     }

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:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Thread[My Thread,5,main].

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

  1.     class newthread extends Thread 
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t = new Thread(this,"My Thread");
  7. 	    t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11.             try
  12.             {
  13.                 t.join()   
  14. 	        System.out.println(t.getName());
  15.             }
  16.             catch(Exception e)
  17.             {
  18.             System.out.print("Exception");
  19.             }
  20. 	}
  21.     }
  22.     class multithreaded_programing
  23.     {
  24.         public static void main(String args[])
  25.         {
  26.             new newthread();        
  27.         }
  28.     }

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:

$ javac multithreaded_programing.java
$ java multithreaded_programing

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

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	newthread()
  5.         {
  6. 	    t = new Thread(this,"New Thread");
  7. 	    t.start();
  8. 	}
  9. 	public void run()
  10.         {
  11. 	   System.out.println(t.isAlive());
  12. 	}
  13.     }
  14.     class multithreaded_programing
  15.     {
  16.         public static void main(String args[])
  17.         {
  18.             new newthread();        
  19.         }
  20.     }

a) 0
b) 1
c) true
d) false

Answer: c
Clarification: isAlive() method is used to check whether the thread being called is running or not, here thread is the main() method which is running till the program is terminated hence it returns true.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
true

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

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t1,t2;
  4. 	newthread()
  5.         {
  6. 	    t1 = new Thread(this,"Thread_1");
  7. 	    t2 = new Thread(this,"Thread_2");
  8. 	    t1.start();
  9. 	    t2.start();
  10. 	}
  11. 	public void run()
  12.         {
  13. 	    t2.setPriority(Thread.MAX_PRIORITY);	
  14. 	    System.out.print(t1.equals(t2));
  15.         }    
  16.     }
  17.     class multithreaded_programing
  18.     {
  19.         public static void main(String args[])
  20.         {
  21.             new newthread();        
  22.         }
  23.     }

a) true
b) false
c) truetrue
d) falsefalse

Answer: d
Clarification: This program was previously done by using Runnable interface, here we have used Thread class. This shows both the method are equivalent, we can use any of them to create a thread.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
falsefalse

Leave a Reply

Your email address will not be published. Required fields are marked *