Threads Questions

Practice Threads MCQs with answers and explanations. Page 1 of 2.

Category
Java Programming
Topic
Threads
Page
1 / 2
Mode
Practice

Questions

Open any question to view the answer and explanation.

In core Java concurrency, which three of the following are methods declared by java.lang.Object (select exactly three)? notify() notifyAll() isInterrupted() synchronized() interrupt() wait(long msecs) sleep(long msecs) yield()
Open
View answer
Java threads — starting a thread that implements Runnable. Given: class X implements Runnable { public static void main(String args[]) { // Which line starts a new thread correctly? } public void run() {} }
Open
View answer
Threads — which three operations guarantee that the <em>current</em> thread will leave the running state? Choose exactly three: yield(), wait(), notify(), notifyAll(), sleep(1000), aLiveThread.join(), Thread.killThread()
Open
View answer
Java Thread API — which two are valid constructors for java.lang.Thread?
Open
View answer
Which of the following cannot directly cause a Java thread to stop executing (i.e., leave the running state) at the moment of the call?
Open
View answer
Which two of the following methods are members of java.lang.Thread (not inherited from Object or declared elsewhere)?
Open
View answer
Thread lifecycle — which method actually registers a new thread with the scheduler and starts it?
Open
View answer
Runnable contract — which method signature must a class provide to correctly implement java.lang.Runnable?
Open
View answer
Which of the following will NOT directly cause the calling thread to stop executing at that moment?
Open
View answer
In Java, which type declares the monitor methods wait(), notify(), and notifyAll() that coordinate threads?
Open
View answer
In Java multithreading, which method contains the actual body of work executed by a Thread (i.e., the code that runs on the thread's stack when scheduled by the JVM scheduler)?
Open
View answer
Java concurrency: A thread A calls wait(2000) inside a synchronized method on object B. When does thread A become eligible (a candidate) to run again?
Open
View answer
In Java, which method is used to start a thread's execution on a new call stack?
Open
View answer
Java threads: Which of the following will directly cause a thread to cease executing (stop running) at that moment?
Open
View answer
Which statement correctly creates a Java thread from a class that implements Runnable and starts it running?
Open
View answer
Java concurrency and synchronization scope: predict the output and determinism of this program. class Happy extends Thread { final StringBuffer sb1 = new StringBuffer(); final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { final Happy h = new Happy(); new Thread() { public void run() { synchronized (this) { h.sb1.append("A"); h.sb2.append("B"); System.out.println(h.sb1); System.out.println(h.sb2); } } }.start(); new Thread() { public void run() { synchronized (this) { h.sb1.append("D"); h.sb2.append("C"); System.out.println(h.sb2); System.out.println(h.sb1); } } }.start(); } }
Open
View answer
Java threads: calling run() directly vs starting a new thread. class MyThread extends Thread { public static void main(String[] args) { MyThread t = new MyThread(); t.run(); } public void run() { for (int i = 1; i < 3; ++i) { System.out.print(i + ".."); } } }
Open
View answer
Java synchronization on a shared Runnable instance: how will two threads print shared counters? public class Test107 implements Runnable { private int x; private int y; public static void main(String[] args) { Test107 that = new Test107(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for (int i = 0; i < 10; i++) { x++; y++; System.out.println("x = " + x + ", y = " + y); } } }
Open
View answer
Thread vs Runnable target when subclass overrides run(): what prints? class MyThread extends Thread { MyThread() {} MyThread(Runnable r) { super(r); } public void run() { System.out.print("Inside Thread "); } } class MyRunnable implements Runnable { public void run() { System.out.print(" Inside Runnable"); } } class Test { public static void main(String[] args) { new MyThread().start(); new MyThread(new MyRunnable()).start(); } }
Open
View answer
Wrapping a Thread around a Thread (as a Runnable): what does this program print? class MyThread extends Thread { public static void main(String[] args) { MyThread t = new MyThread(); Thread x = new Thread(t); x.start(); } public void run() { for (int i = 0; i < 3; ++i) { System.out.print(i + ".."); } } }
Open
View answer

Practice smarter

Solve a few questions daily and revisit weak topics regularly to improve accuracy.