Java MCQ – Multithreading

This section of our 1000+ Java MCQs focuses on multithreading of Java Programming Language.

1. What is multithreaded programming?
a) It’s a process in which two different processes run simultaneously
b) It’s a process in which two or more parts of same process run simultaneously
c) It’s a process in which many different process are able to access same information
d) It’s a process in which a single process can access information from many sources
View Answer

Answer: b
Explanation: Multithreaded programming a process in which two or more parts of the same process run simultaneously.

2. Which of these are types of multitasking?
a) Process based
b) Thread based
c) Process and Thread based
d) None of the mentioned
View Answer

Answer: c
Explanation: There are two types of multitasking: Process based multitasking and Thread based multitasking.

3. Thread priority in Java is?
a) Integer
b) Float
c) double
d) long
View Answer

Answer: a
Explanation: Java assigns to each thread a priority that determines hoe that thread should be treated with respect to others. Thread priority is integers that specify relative priority of one thread to another.
advertisement
advertisement

4. What will happen if two thread of the same priority are called to be processed simultaneously?
a) Anyone will be executed first lexographically
b) Both of them will be executed simultaneously
c) None of them will be executed
d) It is dependent on the operating system
View Answer

Answer: d
Explanation: In cases where two or more thread with same priority are competing for CPU cycles, different operating system handle this situation differently. Some execute them in time sliced manner some depending on the thread they call.

5. Which of these statements is incorrect?
a) By multithreading CPU idle time is minimized, and we can take maximum use of it
b) By multitasking CPU idle time is minimized, and we can take maximum use of it
c) Two thread in Java can have the same priority
d) A thread can exist only in two states, running and blocked
View Answer

Answer: d
Explanation: Thread exist in several states, a thread can be running, suspended, blocked, terminated & ready to run.

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

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

a) Thread[5,main]
b) Thread[main,5]
c) Thread[main,0]
d) Thread[main,5,main]
View Answer

Answer: d
Explanation: None.
Output:

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

7. What is the priority of the thread in the following Java Program?

advertisement
  1.     class multithreaded_programing 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

a) 4
b) 5
c) 0
d) 1
View Answer

Answer: b
Explanation: The output of program is Thread[main,5,main], in this priority assigned to the thread is 5. It’s the default value. Since we have not named the thread they are named by the group to they belong i:e main method.
Output:

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

8. What is the name of the thread in the following Java Program?

  1.     class multithreaded_programing
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             Thread t = Thread.currentThread();
  6.             System.out.println(t);        
  7.         }
  8.     }

a) main
b) Thread
c) System
d) None of the mentioned
View Answer

Answer: a
Explanation: The output of program is Thread[main,5,main], Since we have not explicitly named the thread they are named by the group to they belong i:e main method. Hence they are named ‘main’.
Output:

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

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.