Curioustab
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Aptitude
General Knowledge
Verbal Reasoning
Computer Science
Interview
Take Free Test
Threads Questions
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()
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() {} }
Threads — which three operations guarantee that the
current
thread will leave the running state? Choose exactly three: yield(), wait(), notify(), notifyAll(), sleep(1000), aLiveThread.join(), Thread.killThread()
Java Thread API — which two are valid constructors for java.lang.Thread?
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?
Which two of the following methods are members of java.lang.Thread (not inherited from Object or declared elsewhere)?
Thread lifecycle — which method actually registers a new thread with the scheduler and starts it?
Runnable contract — which method signature must a class provide to correctly implement java.lang.Runnable?
Which of the following will NOT directly cause the calling thread to stop executing at that moment?
In Java, which type declares the monitor methods wait(), notify(), and notifyAll() that coordinate threads?
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)?
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?
In Java, which method is used to start a thread's execution on a new call stack?
Java threads: Which of the following will directly cause a thread to cease executing (stop running) at that moment?
Which statement correctly creates a Java thread from a class that implements Runnable and starts it running?
Java concurrency and synchronization scope: predict the output and determinism of this program.\n\nclass Happy extends Thread {\n final StringBuffer sb1 = new StringBuffer();\n final StringBuffer sb2 = new StringBuffer();\n\n public static void main(String args[]) {\n final Happy h = new Happy();\n\n new Thread() {\n public void run() {\n synchronized (this) {\n h.sb1.append("A");\n h.sb2.append("B");\n System.out.println(h.sb1);\n System.out.println(h.sb2);\n }\n }\n }.start();\n\n new Thread() {\n public void run() {\n synchronized (this) {\n h.sb1.append("D");\n h.sb2.append("C");\n System.out.println(h.sb2);\n System.out.println(h.sb1);\n }\n }\n }.start();\n }\n}
Java threads: calling run() directly vs starting a new thread.\n\nclass MyThread extends Thread {\n public static void main(String[] args) {\n MyThread t = new MyThread();\n t.run();\n }\n public void run() {\n for (int i = 1; i < 3; ++i) {\n System.out.print(i + "..");\n }\n }\n}
Java synchronization on a shared Runnable instance: how will two threads print shared counters?\n\npublic class Test107 implements Runnable {\n private int x; private int y;\n public static void main(String[] args) {\n Test107 that = new Test107();\n (new Thread(that)).start();\n (new Thread(that)).start();\n }\n public synchronized void run() {\n for (int i = 0; i < 10; i++) {\n x++; y++;\n System.out.println("x = " + x + ", y = " + y);\n }\n }\n}
Thread vs Runnable target when subclass overrides run(): what prints?\n\nclass MyThread extends Thread {\n MyThread() {}\n MyThread(Runnable r) { super(r); }\n public void run() { System.out.print("Inside Thread "); }\n}\nclass MyRunnable implements Runnable {\n public void run() { System.out.print(" Inside Runnable"); }\n}\nclass Test {\n public static void main(String[] args) {\n new MyThread().start();\n new MyThread(new MyRunnable()).start();\n }\n}
Wrapping a Thread around a Thread (as a Runnable): what does this program print?\n\nclass MyThread extends Thread {\n public static void main(String[] args) {\n MyThread t = new MyThread();\n Thread x = new Thread(t);\n x.start();\n }\n public void run() {\n for (int i = 0; i < 3; ++i) {\n System.out.print(i + "..");\n }\n }\n}
1
2