Threads Questions

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

Category
Java Programming
Topic
Threads
Page
2 / 2
Mode
Practice

Questions

Open any question to view the answer and explanation.

Two threads, one synchronized run(), infinite loop: what behavior is guaranteed? public class Q126 implements Runnable { private int x; private int y; public static void main(String[] args) { Q126 that = new Q126(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for (;;) { x++; y++; System.out.println("x = " + x + "y = " + y); } } }
Open
View answer
wait/notify preconditions: calling wait() while holding the monitor but without a notifier. public class WaitTest { public static void main(String[] args) { System.out.print("1 "); synchronized (args) { System.out.print("2 "); try { args.wait(); } catch (InterruptedException e) { } } System.out.print("3 "); } }
Open
View answer
Protecting shared state: which change ensures Foo.data remains consistent under concurrency? 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; } }
Open
View answer
Two threads calling a synchronized instance method on the same object: what sequence of numbers prints? public class ThreadDemo { private int count = 1; public synchronized void doSomething() { for (int i = 0; i < 10; i++) System.out.println(count++); } public static void main(String[] args) { ThreadDemo demo = new ThreadDemo(); Thread a1 = new A(demo); Thread a2 = new A(demo); a1.start(); a2.start(); } } class A extends Thread { ThreadDemo demo; public A(ThreadDemo td) { demo = td; } public void run() { demo.doSomething(); } }
Open
View answer
Using wait/notify correctly: why does this code fail at runtime? public class Test { public static void main(String[] args) { final Foo f = new Foo(); Thread t = new Thread(new Runnable() { public void run() { f.doStuff(); } }); Thread g = new Thread() { public void run() { f.doStuff(); } }; t.start(); g.start(); } } class Foo { int x = 5; public void doStuff() { if (x < 10) { try { wait(); } catch (InterruptedException ex) { } } else { System.out.println("x is " + x++); if (x >= 10) { notify(); } } } }
Open
View answer
Java threads without join — what is printed by main when two worker threads update shared buffers? class Test116 { static final StringBuffer sb1 = new StringBuffer(); static final StringBuffer sb2 = new StringBuffer(); public static void main(String args[]) { new Thread() { public void run() { synchronized (sb1) { sb1.append("A"); sb2.append("B"); } } }.start(); new Thread() { public void run() { synchronized (sb1) { sb1.append("C"); sb2.append("D"); } } }.start(); // Line 28 System.out.println(sb1 + " " + sb2); } } No join or additional synchronization is used before the println.
Open
View answer
Java Thread.start() invoked twice on the same thread — what happens? 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 "); } } Choose the correct outcome.
Open
View answer
Two Runnable instances and two threads — what ordering will the printed numbers follow? class s1 implements Runnable { int x = 0, y = 0; int addX() { x++; return x; } int addY() { y++; return y; } public void run() { for (int i = 0; i < 10; i++) System.out.println(addX() + " " + addY()); } public static void main(String args[]) { s1 run1 = new s1(); s1 run2 = new s1(); Thread t1 = new Thread(run1); Thread t2 = new Thread(run2); t1.start(); t2.start(); } } Select the most accurate description.
Open
View answer
One shared Runnable with internal synchronization — what will the two threads print? class s implements Runnable { int x, y; public void run() { for (int i = 0; i < 1000; i++) synchronized (this) { x = 12; y = 12; } System.out.print(x + " " + y + " "); } public static void main(String args[]) { s run = new s(); Thread t1 = new Thread(run); Thread t2 = new Thread(run); t1.start(); t2.start(); } } Predict the console output.
Open
View answer
Thread.currentThread().sleep(...) usage — does this code compile and what happens? class Test { public static void main(String [] args) { printAll(args); } public static void printAll(String[] lines) { for (int i = 0; i < lines.length; i++) { System.out.println(lines[i]); Thread.currentThread().sleep(1000); } } } Assume standard Java SE rules.
Open
View answer
Calling yield() inside run() — what will this single-thread program display? public class ThreadTest extends Thread { public void run() { System.out.println("In run"); yield(); System.out.println("Leaving run"); } public static void main(String [] argv) { (new ThreadTest()).start(); } } Choose the best answer.
Open
View answer
Two Thread subclasses running concurrently — can you predict the exact print order? class s1 extends Thread { public void run() { for (int i = 0; i < 3; i++) { System.out.println("A"); System.out.println("B"); } } } class Test120 extends Thread { public void run() { for (int i = 0; i < 3; i++) { System.out.println("C"); System.out.println("D"); } } public static void main(String args[]) { s1 t1 = new s1(); Test120 t2 = new Test120(); t1.start(); t2.start(); } } Select the correct statement.
Open
View answer
Anonymous Thread subclass overriding run() vs superclass constructor side effects — what prints? class MyThread extends Thread { MyThread() { System.out.print(" MyThread"); } public void run() { System.out.print(" bar"); } public void run(String s) { System.out.println(" baz"); } } public class TestThreads { public static void main (String [] args) { Thread t = new MyThread() { public void run() { System.out.println(" foo"); } }; t.start(); } } Choose the exact output.
Open
View answer
Java wait/notify facts — select the two true statements from the list provided.
Open
View answer
Creating a Thread with a Runnable target — which class definitions compile for "new Thread(target)"? Given: Runnable target = new MyRunnable(); Thread myThread = new Thread(target); Choose the valid implementation for MyRunnable.
Open
View answer
In Java concurrency, which statement about wait()/notify() scheduling and resumption is actually true?
Open
View answer
Java monitors: which statement about notify/notifyAll/wait and synchronization requirements is true?
Open
View answer
Java synchronization basics: identify the single true statement about synchronized methods/blocks and thread behavior.
Open
View answer
Thread creation in Java: which two techniques correctly start work on a new thread of execution?
Open
View answer

Practice smarter

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