Introduction
Multithreading is a powerful concept in Java that allows concurrent execution of multiple threads within a single program. This quiz aims to test your knowledge of Java multithreading concepts, coding knowledge, and features. Each question consists of multiple-choice options, and you can view the answers along with detailed explanations.
Learn everything about Java Multithreading: Java Multithreading Tutorial.
Learn and Master Java Programming: Learn Java Programming with Examples.
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills.
1. What is multithreading in Java?
Answer:
Explanation:
Multithreading refers to the ability of a program to execute multiple threads concurrently within a single process.
2. What is a thread in Java?
Answer:
Explanation:
A thread in Java is a lightweight, independent unit of execution that runs a sequence of instructions.
3. Which class is used to create a thread in Java?
Thread
Runnable
Process
Executor
Answer:
Thread
Explanation:
The Thread
class is used to create and control threads in Java.
4. How can synchronization be achieved in Java threads?
synchronized
keyword.volatile
keyword.final
keyword.static
keyword.Answer:
synchronized
keyword.Explanation:
Synchronization in Java threads can be achieved by using the synchronized
keyword to protect critical sections of code from concurrent access.
5. What is the purpose of the wait()
method in Java threads?
Answer:
Explanation:
The wait()
method is used to temporarily pause the execution of a thread and release the lock it holds.
6. Which method is used to start the execution of a thread?
start()
run()
execute()
begin()
Answer:
start()
Explanation:
The start()
method is used to start the execution of a thread. It internally calls the run()
method.
7. What is the maximum number of threads that can be created in a Java program?
Answer:
Explanation:
The number of threads that can be created in a Java program is limited by available system resources, such as memory, rather than a fixed limit.
8. What is the purpose of the join()
method in Java threads?
Answer:
Explanation:
The join()
method is used to wait for a thread to complete its execution before proceeding with the execution of the calling thread.
9. What is the main advantage of multithreading in Java?
Answer:
Explanation:
One of the main advantages of multithreading in Java is improved program performance by utilizing available CPU cores and concurrently executing tasks.
10. Consider the following program and predict the output:
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
start();
System.out.println("in constructor " + getName());
}
@Override
public void start() {
System.out.println("in start " + getName());
}
public void run() {
System.out.println("in run " + getName());
}
}
public class ThreadTest {
public static void main(String[] args) {
new MyThread("oops");
}
}
in constructor oops
in run oops
in constructor oops
in constructor oops
in run oops
in start oops
in run oops
Answer:
in constructor oops
Explanation:
You have overridden the start()
method, so the run()
method is never called!
11. Consider the following program and predict the output:
class MyThread extends Thread {
@Override
public void run() {
System.out.println("In run method; thread name is: " + Thread.currentThread().getName());
}
}
public class ThreadTest {
public static void main(String args[]) {
Thread myThread = new MyThread();
myThread.run(); // #1
System.out.println("In main method; thread name is: " + Thread.currentThread().getName());
}
}
In run method; thread name is: main
In main method; thread name is: main
In the run method; the thread name is: thread-0
In the main method; the thread name is: main
Answer:
In run method; thread name is: main
In main method; thread name is: main
Explanation:
The correct way to invoke a thread is to call the start()
method on a Thread
object. If you directly call the run()
method, the method will run just like any other method (in other words, it will execute sequentially in the same thread without running as a separate thread).
12. Consider the following program and predict the output:
class MyThread implements Runnable {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class ThreadTest {
public static void main(String arg[]) {
Thread thread = new Thread(new MyThread());
thread.run();
thread.run();
thread.start();
}
}
main
Thread-0
main
Thread-1
Thread-0
Thread-1
Thread-1
Thread-2
Answer:
main
Thread-0
Explanation:
Calling run()
directly will not create a new thread. The correct way is to call the start()
method, which in turn will call the run()
method in a new thread.
13. Consider the following program and choose the right option:
class MyThread extends Thread {
public void run() {
System.out.println("Running");
}
}
public class ThreadTest {
public static void main(String args[]) throws InterruptedException {
Runnable r = new MyThread(); // #1
Thread myThread = new Thread(r); // #2
myThread.start();
}
}
Answer:
Explanation:
The class Thread
implements the Runnable
interface, so the assignment in statement #1 is valid. Also, you can create a new thread object by passing a Runnable
reference to a Thread
constructor, so statement #2 is also valid. Hence, the program will compile without errors and print “Running” in the console.
14. Consider the following program and choose the correct answer:
class MyThread extends Thread {
public MyThread(String name) {
this.setName(name);
}
@Override
public void run() {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
play();
}
private void play() {
System.out.print(getName());
System.out.print(getName());
}
}
public class ThreadTest {
public static void main(String args[]) throws InterruptedException {
Thread tableThread = new MyThread("Table");
Thread tennisThread = new MyThread("Tennis");
tableThread.start();
tennisThread.start();
}
}
IllegalStateException
.Answer:
Explanation:
Since the threads are not synchronized in this program, the output of this program cannot be determined. Depending on how the threads are scheduled, it may even generate output such as "Table Tennis Tennis Table".
15. Which of the following state(s) is/are NOT legitimate thread state(s)? (Select all that apply.)
Answer:
Explanation:
A thread can be in one of the following states (as defined in the java.lang.Thread.State
enumeration): NEW
, RUNNABLE
, BLOCKED
, WAITING
, TIMED_WAITING
, and TERMINATED
.
16. Consider the following program:
class Worker extends Thread {
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
public class Master {
public static void main(String[] args) throws InterruptedException {
Thread.currentThread().setName("Master ");
Thread worker = new Worker();
worker.setName("Worker ");
worker.start();
Thread.currentThread().join();
System.out.println(Thread.currentThread().getName());
}
}
Which one of the following options correctly describes the behavior of this program?
IllegalMonitorStateException
.Answer:
Explanation:
The statement Thread.currentThread()
in the main()
method refers to the “Master” thread. Calling the join()
method on itself means that the thread waits for itself to complete, which would never happen, so this program hangs (and does not terminate).
Conclusion
Java Multithreading enables the concurrent execution of multiple threads, offering benefits such as increased program performance and efficient utilization of system resources. By understanding the concepts and features of Java Multithreading, you can develop applications that effectively leverage parallelism and improve overall responsiveness.
Keep exploring and practicing Multithreading techniques to enhance your Java programming skills.
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
Related Posts
- Java String Quiz
- Java Arrays Quiz
- Java Loops Quiz
- Java OOPS Quiz
- Java OOPS Quiz - Part 1
- Java OOPS Quiz - Part 2
- Java Exception Handling Quiz
- Java Collections Quiz
- Java Generics Quiz
- JDBC Quiz
- Java Lambda Expressions Quiz
- Java Functional Interfaces Quiz
- Java Streams API Quiz
- Java Date Time Quiz
- Java 8 Quiz
Comments
Post a Comment
Leave Comment