Multithreading Questions with Answers

65 practice questions on Multithreading (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 65 free in the AptiNest app.

Sample questions

Question 1Medium

What will be the output of the program?
and assuming that data must be protected from corruption, what if anything can you add to the preceding code to ensure the integrity of data?

public class SyncTest
{
    public static void main (String [] args)
    {
        Thread t = new Thread()
        {
            Foo f = new Foo();
            public void run()
            {
                f.increase(20);
            }
        };
    t.start();
    }
}
class Foo
{
    private int data = 23;
    public void increase(int amt)
    {
        int x = data;
        data = x + amt;
    }
}
  1. Synchronize the run method.
  2. Wrap a synchronize(this) around the call to f.increase().
  3. The existing code will cause a runtime exception.
  4. Synchronize the increase() method

Answer: D. Synchronize the increase() method

Explanation

D is correct because synchronizing the code that actually does the increase will protect the code from being accessed by more than one thread at a time.
A is incorrect because synchronizing the run() method would stop other threads from running the run() method (a bad idea) but still would not prevent other threads with other runnables from accessing the increase() method.
B is incorrect for virtually the same reason as synchronizing the code that calls the increase() method does not prevent other code from calling the increase() method.

Step-by-step solution

  1. Foo.increase reads data into x, adds amt and writes the result back in separate steps.
  2. Two threads can both read the same value before either writes, so one update is lost.
  3. Marking increase() as synchronized lets only one thread run it on the Foo object at a time.
  4. So the fix is option D: synchronize the increase() method.

Why the other options are wrong

  • A. Synchronizing run() stops other threads from running run(), but not other calls to increase().
  • B. Locking the call site in run() does not stop other code from calling increase() directly.
  • C. No runtime exception occurs; the problem is a race condition that loses an update.

Tip: Put the lock on the method or code that reads and writes the shared data, inside the object that owns it.

Question 2Medium

Which of these method waits for the thread to treminate?

  1. sleep()
  2. isAlive()
  3. join()
  4. stop()

Answer: C. join()

Explanation

Join() is non static method of Thread class per my understanding what it does is, it let one thread join at to the end of another Thread.

Step-by-step solution

  1. join() makes current thread wait until the target thread finishes.
  2. sleep() just pauses current thread for given time, doesn't wait for others.
  3. isAlive() checks if thread is still running, does not wait.
  4. stop() forcefully stops a thread, does not ensure completion.
  5. So join() is the only method that waits for thread to terminate.

Why the other options are wrong

  • A. sleep() pauses the current thread for a set time and does not wait for another thread to finish.
  • B. isAlive() only reports whether a thread is still running; it returns at once and does not wait.
  • D. stop() forcefully ends a thread; it does not wait for another thread to finish.

Tip: Remember: join() blocks until target thread dies.

Question 3Medium

The following block of code creates a Thread using a Runnable target:

Runnable target = new MyRunnable();
Thread myThread = new Thread(target);

Which of the following classes can be used to create the target, so that the preceding code compiles correctly?

  1. public class MyRunnable extends Runnable{public void run(){}}
  2. public class MyRunnable extends Object{public void run(){}}
  3. public class MyRunnable implements Runnable{public void run(){}}
  4. public class MyRunnable implements Runnable{void run(){}}

Answer: C. public class MyRunnable implements Runnable{public void run(){}}

Explanation

The class correctly implements the Runnable interface with a legal public void run() method.
A is incorrect because interfaces are not extended; they are implemented.
B is incorrect because even though the class would compile and it has a valid public void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it.
D is incorrect because the run() method must be public.

Step-by-step solution

  1. MyRunnable must implement Runnable to match the code's usage.
  2. Option C correctly implements Runnable with a public void run() method.
  3. Other options either extend instead of implement or have incorrect method access modifiers.
  4. Only option C satisfies all compiler requirements for the Thread creation.
  5. Final answer: public class MyRunnable implements Runnable{public void run(){}}

Why the other options are wrong

  • A. Runnable is an interface, so a class cannot extend it; this declaration does not compile.
  • B. Extending Object does not implement Runnable, so MyRunnable cannot be passed to Thread.
  • D. A package-private run() does not implement the public run() required by Runnable, so it will not compile.

Tip: Always implement Runnable with a public void run() method when creating threads.

Question 4Medium

Which two can be used to create a new Thread?

1) Extend java.lang.Thread and override the run() method.
2) Extend java.lang.Runnable and override the start() method.
3) Implement java.lang.Thread and implement the run() method.
4) Implement java.lang.Runnable and implement the run() method.
5) Implement java.lang.Thread and implement the start() method.

  1. 1 and 2
  2. 2 and 3
  3. 1 and 4
  4. 3 and 4

Answer: C. 1 and 4

Explanation

Extending Thread and overriding run() is valid, and so is implementing Runnable and its run() method. Statements 2, 3 and 5 are wrong because Runnable is an interface, not a class to extend, and Thread is a class, so it cannot be implemented.

Step-by-step solution

  1. Item 1 is valid: a class can extend Thread and override run().
  2. Item 4 is valid: a class can implement Runnable and implement run(), then be passed to a Thread.
  3. Item 2 is wrong: Runnable is an interface, so it is implemented, not extended.
  4. Item 3 is wrong: Thread is a class and cannot be implemented. So the answer is items 1 and 4, option C.

Why the other options are wrong

  • A. Item 2 is wrong: Runnable is an interface and is implemented, not extended.
  • B. Items 2 and 3 are both wrong, because Runnable cannot be extended and Thread cannot be implemented.
  • D. Item 3 is wrong: Thread is a class and cannot be implemented.

Tip: Use extends for classes and implements for interfaces; to start a thread you override run() and call start().

Question 5Medium

What is the name of the method used to start a thread execution?

  1. init();
  2. start();
  3. run();
  4. resume();

Answer: B. start();

Explanation

B is Correct. The start() method causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
A is wrong. There is no init() method in the Thread class.
C is wrong. The run() method of a thread is like the main() method to an application. Starting the thread causes the object's run method to be called in that separately executing thread.
D is wrong. The resume() method is deprecated. It resumes a suspended thread.

Step-by-step solution

  1. start() method begins thread execution.
  2. Java VM calls run() when start() is invoked.
  3. run() is like main() for the thread's task.
  4. resume() is outdated and not used now.
  5. init() is not a Thread class method.
  6. Correct answer is start().

Why the other options are wrong

  • A. init() is not a Thread method, so calling it does not start any thread execution.
  • C. run() holds the thread's task, but calling it directly runs in the current thread, not a new one.
  • D. resume() is an old method for restarting a suspended thread, not a method that starts a new thread.

Tip: Remember: start() starts a thread, run() runs its code.

Question 6Medium

What is the output of this program?

class newthread extends Thread {
	Thread t;
	String name;
	newthread(String threadname) {
	    name = threadname;
	    t = new Thread(this,name);
	    t.start();
	}
	public void run() {
        }

    }
    class multithreaded_programing {
        public static void main(String args[]) {
	    newthread obj1 = 	 new newthread("one");
	    newthread obj2 =	 new newthread("two");
            try {
                obj1.t.wait();
                System.out.print(obj1.t.isAlive());
            }
            catch(Exception e) {
	    System.out.print("Main thread interrupted");
            }
        }
    }
  1. true
  2. false
  3. Main thread interrupted
  4. None of the mentioned

Answer: C. Main thread interrupted

Explanation

obj1.t.wait() causes main thread to go out of processing in sleep state hence causes exception and 'Main thread interrupted' is printed.

Output:

$ javac multithreaded_programing.java
$ java multithreaded_programing
Main thread interrupted

Step-by-step solution

  1. Each newthread constructor creates a Thread and starts it; its run() method is empty.
  2. Main calls obj1.t.wait() without holding the lock (monitor) of that thread.
  3. Java throws IllegalMonitorStateException, a RuntimeException, which the catch(Exception) block catches.
  4. The catch block prints Main thread interrupted. Option C is the answer.

Why the other options are wrong

  • A. wait() throws before isAlive() is reached, so true is never printed.
  • B. The exception stops the try block before the print, so false is not printed.
  • D. The catch block does run, so the answer is not none of the mentioned.

Tip: wait() and notify() must be called while holding the object's lock, or Java throws IllegalMonitorStateException.

Question 7Medium

class MyThread extends Thread
{
    public static void main(String [] args)
    {
        MyThread t = new MyThread();
        t.start();
        System.out.print("one. ");
        t.start();
        System.out.print("two. ");
    }
    public void run()
    {
        System.out.print("Thread ");
    }
}
  1. Compilation fails
  2. An exception occurs at runtime.
  3. It prints "Thread one. Thread two."
  4. The output cannot be determined.

Answer: B. An exception occurs at runtime.

Explanation

When the start() method is attempted a second time on a single Thread object, the method will throw an IllegalThreadStateException (you will not need to know this exception name for the exam). Even if the thread has finished running, it is still illegal to call start() again.

Step-by-step solution

  1. The first t.start() runs run() in a new thread, and main prints one. then calls t.start() again.
  2. A thread object can be started only once; a second start() throws IllegalThreadStateException at run time.
  3. So the program throws an exception at run time, which is option B.

Why the other options are wrong

  • A. The code compiles; the problem happens at run time.
  • C. The second start() throws an exception, so the program does not finish normally with that output.
  • D. The exception is certain, so the output is not undetermined.

Tip: Each Thread object can be started only once; create a new object to run it again.

Question 8Medium

Which statement is true?

  1. The notifyAll() method must be called from a synchronized context.
  2. To call wait(), an object must own the lock on the thread.
  3. The notify() method is defined in class java.lang.Thread.
  4. The notify() method causes a thread to immediately release its locks.

Answer: A. The notifyAll() method must be called from a synchronized context.

Explanation

A is correct because the notifyAll() method (along with wait() and notify()) must always be called from within a synchronized context.
B is incorrect because to call wait(), the thread must own the lock on the object that wait() is being invoked on, not the other way around.
C is wrong because notify() is defined in java.lang.Object.
D is wrong because notify() will not cause a thread to release its locks. The thread can only release its locks by exiting the synchronized code.

Step-by-step solution

  1. notifyAll() must be called from a synchronized context, as per Java's concurrency rules.
  2. wait() requires the thread to hold the lock on the object it is waiting on.
  3. notify() is part of java.lang.Object, not java.lang.Thread.
  4. notify() does not release locks; the thread must exit the synchronized block to release them.
  5. Only option A correctly describes a requirement for notifyAll().
  6. Final answer: A.

Why the other options are wrong

  • B. A thread must own the lock on the object it waits on; the statement wrongly says the lock is on the thread.
  • C. notify() is defined in java.lang.Object, not in java.lang.Thread.
  • D. notify() does not release locks at once; the thread keeps its lock until it leaves the synchronized block.

Tip: Remember: notifyAll(), wait(), and notify() require a synchronized context.

Question 9Medium

Which two are valid constructors for Thread?

Thread(Runnable r, String name)
Thread()
Thread(int priority)
Thread(Runnable r, ThreadGroup g)
Thread(Runnable r, int priority)
  1. 1 and 3
  2. 2 and 4
  3. 1 and 2
  4. 2 and 5

Answer: C. 1 and 2

Explanation

- (1) and (2) are both valid constructors for Thread.

- (3), (4), and (5) are not legal Thread constructors, although (4) is close.

If you reverse the arguments in (4), you'd have a valid constructor

Step-by-step solution

  1. Thread() is a valid constructor (item 2).
  2. Thread(Runnable r, String name) is a valid constructor (item 1).
  3. Thread(int priority) does not exist (item 3), and Thread(Runnable r, int priority) does not exist either (item 5).
  4. Thread(Runnable r, ThreadGroup g) has its arguments in the wrong order (item 4), so it is not valid.
  5. So the two valid constructors are items 1 and 2, option C.

Why the other options are wrong

  • A. Item 3, Thread(int priority), does not exist.
  • B. Item 4 has its arguments reversed, so it is not a valid constructor.
  • D. Item 5, Thread(Runnable r, int priority), does not exist.

Tip: Check the argument order and types of Thread constructors; the valid form is ThreadGroup then Runnable, not the reverse.

Question 10Medium

What is the output of this program?

    class newthread implements Runnable {
	Thread t;
	newthread() {
	    t = new Thread(this,"My Thread");
	    t.start();
	}
    }
    class multithreaded_programing {
        public static void main(String args[]) {
            new newthread();
        }
    }
  1. My Thread
  2. Thread[My Thread,5,main]
  3. Compilation Error
  4. Runtime Error

Answer: C. Compilation Error

Explanation

The class newthread says it implements Runnable but does not write a run method. Java requires every method of the interface to be implemented, so the code does not compile.

Step-by-step solution

  1. Class newthread implements Runnable but does not define run() method.
  2. Java requires all abstract methods of interfaces to be implemented.
  3. Missing run() method causes compilation error.
  4. Compilation fails with message about missing run() method.
  5. No runtime error occurs because code doesn't compile.
  6. Final answer: Compilation Error.

Why the other options are wrong

  • A. The program never prints My Thread, because it does not compile, so there is no output.
  • B. The Thread toString is not printed, since the code fails to compile before running.
  • D. No runtime error happens, because the code is rejected at compile time.

Tip: Always implement all abstract methods from interfaces you use.

Practise all 65 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics