250+ TOP MCQs on Creating Threads and Answers

Java MCQs on creating threads in Java Programming Language.

1. Which of these keywords are used to implement synchronization?
a) synchronize
b) syn
c) synch
d) synchronized

Answer: d
Clarification: None.

2. Which of this method is used to avoid polling in Java?
a) wait()
b) notify()
c) notifyAll()
d) all of the mentioned

Answer: d
Clarification: Polling is a usually implemented by looping in CPU is wastes CPU time, one thread being executed depends on other thread output and the other thread depends on the response on the data given to the first thread. In such situation CPU time is wasted, in Java this is avoided by using methods wait(), notify() and notifyAll().

3. Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor?
a) wait()
b) notify()
c) notifyAll()
d) sleep()

Answer: a
Clarification: wait() method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor. This helps in avoiding polling and minimizes CPU idle time.

4. Which of these method wakes up the first thread that called wait()?
a) wake()
b) notify()
c) start()
d) notifyAll()

Answer: b
Clarification: None.

5. Which of these method wakes up all the threads?
a) wakeAll()
b) notify()
c) start()
d) notifyAll()

Answer: d
Clarification: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.

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 the 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 program?

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                 obj1.t.wait();	
  25.                 System.out.print(obj1.t.isAlive());
  26.             }
  27.             catch(Exception e)
  28.             {
  29. 	    System.out.print("Main thread interrupted");
  30.             }
  31.         }
  32.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: c
Clarification: obj1.t.wait() causes main thread to go out of processing in sleep state hence causes exception and “Main thread interrupted” is printed.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Main thread interrupted

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

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                 Thread.sleep(1000);	
  25.                 System.out.print(obj1.t.isAlive());
  26.             }
  27.             catch(InterruptedException e)
  28.             {
  29. 	    System.out.print("Main thread interrupted");
  30.             }
  31.         }
  32.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: b
Clarification: Thread.sleep(1000) has caused all the threads to be suspended for some time, hence onj1.t.isAlive() returns false.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
false

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

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  4. 	String name;
  5. 	newthread(String threadname)
  6.         {
  7. 	    name = threadname;
  8. 	    t = new Thread(this,name);
  9. 	    t.start();
  10. 	}
  11. 	public void run()
  12.         {
  13.         }
  14.  
  15.     }
  16.     class multithreaded_programing
  17.     {
  18.         public static void main(String args[])
  19.         {
  20. 	    newthread obj1 = 	 new newthread("one");
  21. 	    newthread obj2 =	 new newthread("two");
  22.             try
  23.             {
  24.                  System.out.print(obj1.t.equals(obj2.t));
  25.             }
  26.             catch(Exception e)
  27.             {
  28. 	    System.out.print("Main thread interrupted");
  29.             }
  30.         }
  31.     }

a) true
b) false
c) Main thread interrupted
d) None of the mentioned

Answer: b
Clarification: Both obj1 and obj2 have threads with different name that is “one” and “two” hence obj1.t.equals(obj2.t) returns false.
Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
false

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

  1.     class newthread extends Thread
  2.     {
  3. 	Thread t;
  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 *