Java Questions & Answers – Creating Threads

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: None.

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

Answer: d
Explanation: 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()
View Answer

Answer: a
Explanation: 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.
advertisement
advertisement

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

Answer: b
Explanation: None.

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

Answer: d
Explanation: notifyAll() wakes up all the threads that called wait() on the same object. The highest priority thread will run first.
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

Answer: a
Explanation: 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?

advertisement
  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
View Answer

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

advertisement
$ 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: 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

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.