Java Multithreading Quiz - Multiple Choice Questions


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?

a) A programming language for concurrent execution.
b) A technique to execute multiple programs simultaneously.
c) A concept of executing multiple threads concurrently within a single program.
d) A method for parallel processing of data.

Answer:

c) A concept of executing multiple threads concurrently within a single program.

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?

a) A lightweight process that executes a sequence of instructions.
b) A memory allocation unit in Java.
c) A Java keyword to define a loop construct.
d) A data structure to store multiple values.

Answer:

a) A lightweight process that executes a sequence of instructions.

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?

a) Thread
b) Runnable
c) Process
d) Executor

Answer:

a) Thread

Explanation:

The Thread class is used to create and control threads in Java.

4. How can synchronization be achieved in Java threads?

a) Using the synchronized keyword.
b) Using the volatile keyword.
c) Using the final keyword.
d) Using the static keyword.

Answer:

a) Using the 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?

a) To pause the execution of a thread.
b) To terminate a thread.
c) To notify other threads to resume execution.
d) To release the lock held by the thread.

Answer:

a) To pause the execution of a thread.

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?

a) start()
b) run()
c) execute()
d) begin()

Answer:

a) 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?

a) There is no fixed limit.
b) It depends on the amount of available memory.
c) It depends on the number of CPU cores.
d) It is limited to 100 threads.

Answer:

a) There is no fixed limit.

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?

a) To interrupt the execution of a thread.
b) To wait for a thread to finish its execution.
c) To schedule the execution of a thread.
d) To notify a thread to stop execution.

Answer:

b) To wait for a thread to finish its execution.

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?

a) Improved program performance.
b) Simplified program structure.
c) Reduced memory usage.
d) Elimination of runtime errors.

Answer:

a) Improved program performance.

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");
    }
}
a) in start oops
in constructor oops
b) in start oops
in run oops
in constructor oops
c) in start oops
in constructor oops
in run oops
d) in constructor oops
in start oops
in run oops

Answer:

a) in start oops
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());
    }
}
a) The program results in a compiler error at statement #1.
b) The program results in a runtime exception.
c) The program prints the following:
In run method; thread name is: main
In main method; thread name is: main
d) The program prints:
In the run method; the thread name is: thread-0
In the main method; the thread name is: main

Answer:

c) The program prints the following:
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();
    }
}
a) main
main
Thread-0
b) Thread-0
main
Thread-1
c) main
Thread-0
Thread-1
d) Thread-0
Thread-1
Thread-2

Answer:

a) main
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();
    }
}
a) The program will result in a compilation error at statement #1.
b) The program will result in a compilation error at statement #2.
c) The program will compile with no errors and will print “Running” in the console.
d) The program will compile with no errors but does not print any output in the console.

Answer:

c) The program will compile with no errors and will print “Running” in the console.

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();
    }
}
a) This program will throw an IllegalStateException.
b) This program will always print the following: Tennis Tennis Table Table.
c) This program will always print the following: Table Table Tennis Tennis.
d) The output of this program cannot be predicted; it depends on thread scheduling.

Answer:

d) The output of this program cannot be predicted; it depends on thread scheduling.

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.)

a) NEW
b) EXECUTING
c) WAITING
d) TERMINATED
e) RUNNABLE

Answer:

b) EXECUTING

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?

a) When executed, the program prints the following: “Worker Master ”.
b) When executed, the program prints “Worker ”, and after that the program hangs (i.e., does not terminate).
c) When executed, the program prints “Worker ” and then terminates.
d) When executed, the program throws an IllegalMonitorStateException.
e) The program does not compile and fails with multiple compiler errors.

Answer:

b) When executed, the program prints “Worker ”, and after that the program hangs (i.e., does not terminate).

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

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. JDBC Quiz
  11. Java Lambda Expressions Quiz
  12. Java Functional Interfaces Quiz
  13. Java Streams API Quiz
  14. Java Date Time Quiz
  15. Java 8 Quiz

Comments