Test 1 Java Threads
This quiz tests your knowledge of Java multi-threading concepts including Thread class, Runnable interface, thread creation, synchronization, monitors, wait/notify mechanisms, and thread groups.
Questions
Which of the following thread group methods returns the number of threads in a group, along with the group, for which this thread is a parent?
- int activeGroupCount()
- final void destroy()
- final int getMaxPriority()
- int activeCount()
- none of these
When attempting to compile and run the following code, the result will be
public class MyClass extends Thread{
public MyClass(String s){ msg=s; }
String msg;
public void run(){
System.out.println(msg);
}
public static void main(String[] args) {
new MyClass(Hello);
new MyClass(World);
}
}
- The program will fail to compile.
- The program will compile without any errors and will print 'Hello' and 'World' everytime the program is run.
- The program will compile without errors and will print 'Hello' and 'World' but the order is unpredictable.
- The program will compile without any errors and will terminate without any output when run.
Which of the following methods in ThreadGroup class returns the number of threads in the group plus any group for which this thread is a parent?
- activeGroupCount()
- activeCount()
- destroy()
- getMaxPriority()
- paint()
What will be the output of the program?
class MyThread extends Thread
{
public static void main(String [] args)
{
MyThread t = new MyThread(); /* Line 5 */
t.run(); /* Line 6 */
}
public void run()
{
for(int i=1; i < 3; ++i)
{
System.out.print(i + ..);
}
}
}
- This code will not compile due to line 5.
- This code will not compile due to line 6.
- 1..2..
- 1..2..3..
It is given that a monitor called 'abc' has 4 threads in its waiting pool and all these waiting threads have the same priority. The name of one of the threads is 'thrd1'. How will you notify 'thrd1' so that it alone moves from its waiting state to ready state?
- Execute notify(thrd1); from synchronized code of abc.
- Execute abc.notify(thrd1); from synchronized code of abc.
- Execute thrd1.notify(); from synchronized code of any object.
- You cannot specify which thread will get notified.
public class MyRunnable implements Runnable
{
public void run()
{
// some code here
}
}
Which of these will create and start this thread?
- new Runnable(MyRunnable).start();
- new Thread(MyRunnable).run();
- new Thread(new MyRunnable()).start();
- new MyRunnable().start();
What will be the output of the program?
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();
}
}
- The code fails to compile in the main() method
- The code fails to compile in the run() method
- Only the text "In run" will be displayed
- The text "In run" followed by "Leaving run" will be displayed
Which two can be used to create a new Thread?
- Extend java.lang.Thread and override the run() method.
- Extend java.lang.Runnable and override the start() method.
- Implement java.lang.Thread and implement the run() method.
- Implement java.lang.Runnable and implement the run() method.
- Implement java.lang.Thread and implement the start() method.
- 1 and 2
- 2 and 3
- 1 and 4
- 3 and 4
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?
- public class MyRunnable extends Runnable{public void run(){}}
- public class MyRunnable extends Object{public void run(){}}
- public class MyRunnable implements Runnable{public void run(){}}
- public class MyRunnable implements Runnable{void run(){}}
What will be the output of the program?
public class Test
{
public static void main(String[] args)
{
final StringBuffer a = new StringBuffer();
final StringBuffer b = new StringBuffer();
new Thread()
{
public void run()
{
System.out.print(a.append(A));
synchronized(b)
{
System.out.print(b.append(B));
}
}
}.start();
new Thread()
{
public void run()
{
System.out.print(b.append(C));
synchronized(a)
{
System.out.print(a.append(D));
}
}
}.start();
}
}
- ACCBAD
- ABBCAD
- CDDACB
- Indeterminate output
What will be the output of the program?
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); /* Line 17 */
}
}
}
- Compilation error.
- Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
- Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
- Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
What will be the output of the program?
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();
}
}
- It will print the numbers 0 to 19 sequentially.
- It will print the numbers 1 to 20 sequentially.
- It will print the numbers 1 to 20, but the order cannot be determined.
- The code will not compile.