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.

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.

3. Which class is used to create a thread in Java?

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

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.

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.

6. Which method is used to start the execution of a thread?

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

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.

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.

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.

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

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

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

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.

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.

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

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.

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

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare