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.

12 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

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?

  1. int activeGroupCount()
  2. final void destroy()
  3. final int getMaxPriority()
  4. int activeCount()
  5. none of these
Question 2 Multiple Choice (Single Answer)

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);
       }
}
  1. The program will fail to compile.
  2. The program will compile without any errors and will print 'Hello' and 'World' everytime the program is run.
  3. The program will compile without errors and will print 'Hello' and 'World' but the order is unpredictable.
  4. The program will compile without any errors and will terminate without any output when run.
Question 3 Multiple Choice (Single Answer)

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?

  1. activeGroupCount()
  2. activeCount()
  3. destroy()
  4. getMaxPriority()
  5. paint()
Question 4 Multiple Choice (Single Answer)

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 + ..);
        }
    }
}
  1. This code will not compile due to line 5.
  2. This code will not compile due to line 6.
  3. 1..2..
  4. 1..2..3..
Question 5 Multiple Choice (Single Answer)

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?

  1. Execute notify(thrd1); from synchronized code of abc.
  2. Execute abc.notify(thrd1); from synchronized code of abc.
  3. Execute thrd1.notify(); from synchronized code of any object.
  4. You cannot specify which thread will get notified.
Question 6 Multiple Choice (Single Answer)
public class MyRunnable implements Runnable 
{
    public void run() 
    {
        // some code here
    }
}

Which of these will create and start this thread?

  1. new Runnable(MyRunnable).start();
  2. new Thread(MyRunnable).run();
  3. new Thread(new MyRunnable()).start();
  4. new MyRunnable().start();
Question 7 Multiple Choice (Single Answer)

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(); 
    } 
}
  1. The code fails to compile in the main() method
  2. The code fails to compile in the run() method
  3. Only the text "In run" will be displayed
  4. The text "In run" followed by "Leaving run" will be displayed
Question 8 Multiple Choice (Single Answer)

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
Question 9 Multiple Choice (Single Answer)

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(){}}
Question 10 Multiple Choice (Single Answer)

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(); 
    } 
}
  1. ACCBAD
  2. ABBCAD
  3. CDDACB
  4. Indeterminate output
Question 11 Multiple Choice (Single Answer)

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 */
        } 
    } 
}
  1. Compilation error.
  2. 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.
  3. 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.
  4. Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
Question 12 Multiple Choice (Single Answer)

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(); 
    } 
}
  1. It will print the numbers 0 to 19 sequentially.
  2. It will print the numbers 1 to 20 sequentially.
  3. It will print the numbers 1 to 20, but the order cannot be determined.
  4. The code will not compile.