Java Multithreading Quiz - Multiple Choice Questions

In this post, I have included a few useful Java multithreading programming questions and answers (code snippets with output). I suggest you guys try these code snippets in eclipse IDE and understand how the program works (However, the answer with the explanation given at end of this post). These questions may ask in interviews or similar questions may appear in interviews so prepare yourself.

The answer and explanation of each question have given at end of this post.

Q1 - Which of the following two definitions of Sync (when compiled in separate files) will compile without errors?

A)
class Sync {
    public synchronized void foo() {}
}
B)
abstract class Sync {
    public synchronized void foo() {}
}
C)
abstract class Sync {
    public abstract synchronized void foo();
}
D)
interface Sync {
    public synchronized void foo();
}

Q2 - 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

Q3 - 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

Q4 - 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

Q5 - Consider the following program and choose a 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.

Q6 - Which one of the following constructor is NOT a valid constructor of Thread class?

a)
Thread()
b)
Thread(String name)
c)
Thread(Runnable target, Object obj)
d)
Thread(Runnable target, String name)
e)
Thread(ThreadGroup group, String name)

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

Q8 - 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

Q9 - 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 IllegalMonitorStateException.
e) The program does not compile and fails with multiple compiler errors.

Q10 - Consider the following program and choose the correct option describing its behavior.

public class ThreadTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() - > {
            System.out.print("t1 ");
        });

        Thread t2 = new Thread(() - > {
            System.out.print("t2 ");
        });
        t1.start();
        t1.sleep(5000);
        t2.start();
        t2.sleep(5000);
        System.out.println("main ");
    }
}
A. t1 t2 main
B. t1 main t2
C. main t2 t1
D. This program results in a compiler error.
E. This program throws a runtime error.

Answers

Q1

Answer: A. and B.
Explanation: (Abstract methods (in abstract classes or interfaces) cannot be declared synchronized, hence the options C and D are incorrect.)

Q2

Answer:
in start oops
in constructor oops
You have overridden the start() method, so the run() method is never called!

Q3

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

Q4

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.

Q5

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.

Q6

Answer:
c) Thread(Runnable target, Object obj)
The other constructors are valid Thread constructors.

Q7

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

Q8

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.

Q9

Answer:
b) When executed, the program prints "Worker" and then 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).

Q10

A. t1 t2 main




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

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course