0

Test 1 Java Threads

Description: This quiz lets you test your skill on jav multi Threading
Number of Questions: 15
Created by:
Tags: Threads Java Programming
Attempted 0/15 Correct 0 Score 0

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


Correct Option: D
Explanation:

This method returns the number of threads in the group, along with a group, for which this thread is a parent.

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.


Correct Option: D
Explanation:

The program will compile without errors and will simply terminate without any output. Two threads are created but they will never be started. The start() method must be called on the thread objects to make the threads execute the run() method asynchronously.

In the following Java program, it was found that these statements have generated an exception of Array Index out of bound Exception.

int b[ ] = {10,20,30};
b[50] = 100 ;

Choose the correct catch statement for the above code.

  1. catch (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
    }
    
  2. catch (ArrayIndexOutOfBoundsException aie) {
     System.out.println("array size is less than what you are trying to access")
    }
    
  3. catch (aie) {
     aie.printStackTrace();
     System.out.println("array size is less than what you are trying to access")
     }
    
  4. throw (ArrayIndexOutOfBoundsException aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access ')
     }
    
  5. throw (aie) {
     aie.printStackTrace();
     System.out.println(array size is less than what you are trying to access)
    }
    

Correct Option: A
Explanation:

Catch routines are defined in this form. 

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()


Correct Option: B
Explanation:

This method in ThreadGroup class returns the number of threads in the group plus any group for which this thread is a parent.

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..


Correct Option: C
Explanation:

Line 6 calls the run() method, so the run() method executes as a normal method should and it prints 1..2.. 1 is incorrect because line 5 is the proper way to create an object. 2 is incorrect because it is legal to call the run() method, even though this will not start a true thread of execution. The code after line 6 will not execute until the run() method is complete. 4 is incorrect because the for loop only does two iterations.

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.


Correct Option: D
Explanation:

This is the right answer as notify() method notifies the thread to move from its waiting state but does not tell which thread is to be notified specifically.

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();


Correct Option: C
Explanation:

Because the class implements Runnable, an instance of it has to be passed to theThread constructor, and then the instance of the Thread has to be started. 1 is incorrect. There is no constructor like this for Runnable because Runnable is an interface, and it is illegal to pass a class or interface name to any constructor. 2 is incorrect for the same reason; you can't pass a class or interface name to any constructor. 4 is incorrect because MyRunnable doesn't have a start() method, and the onlystart() method that can start a thread of execution is the start() in the Threadclass.

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


Correct Option: D
public class ExceptionTest 
{ 
    class TestException extends Exception {} 
    public void runTest() throws TestException {} 
    public void test() /* Point X */ 
    { 
        runTest(); 
    } 
}

At Point X in line 5, which code is necessary to make the code compile?

  1. no code is necessary

  2. throws Exception

  3. catch ( Exception e )

  4. throws RuntimeException


Correct Option: B
Explanation:

Option 2 is correct. This works because it DOES throw an exception if an error occurs. Option 1 is wrong. If you compile the code as given the compiler will complain: unreported exception must be caught or declared to be thrown The class extendsException so we are forced to test for exceptions. Option 3 is wrong. The catch statement belongs in a method body not a method specification. Option 4 is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up theException tree. Throwing RuntimeException is just not on as this belongs in thejava.lang.RuntimeException branch (it is not a superclass of TestException). The compiler complains with the same error as in A above.

What will be the output of the program?

public class X 
{  
    public static void main(String [] args) 
    {
        try 
        {
            badMethod();  
            System.out.print(A);  
        } 
        catch (RuntimeException ex) /* Line 10 */
        { 
            System.out.print(B); 
        } 
        catch (Exception ex1) 
        { 
            System.out.print(C); 
        } 
        finally 
        {
            System.out.print(D); 
        } 
        System.out.print(E); 
    } 
    public static void badMethod() 
    { 
        throw new RuntimeException(); 
    } 
}
  1. BD

  2. BCD

  3. BDE

  4. BCDE


Correct Option: C
Explanation:

A Runtime exception is thrown and caught in the catch statement in line 10. The code after the finally statement is run because the exception has been caught.

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


Correct Option: C
Explanation:

There are two ways of creating a thread; extend (sub-class) the Thread class and implement the Runnable interface. For both of these ways you must implement (override and not overload) the public void run() method. (1) is correct - Extending the Thread class and overriding its run method is a valid procedure. (4) is correct - You must implement interfaces, and runnable is an interface and you must also include the run method. (2) is wrong - Runnable is an interface which implements not Extends. Gives the error: (No interface expected here) (3) is wrong - You cannot implement java.lang.Thread (This is a Class). (Implements Thread, gives the error: Interface expected). Implements expects an interface. (5) is wrong - You cannot implement java.lang.Thread (This is a class). You Extend classes, and Implement interfaces. (Implements Thread, gives the error: Interface expected)

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(){}}


Correct Option: C
Explanation:

The class correctly implements the Runnable interface with a legal public void run()method. Option 1 is incorrect because interfaces are not extended; they are implemented. Option 2 is incorrect because even though the class would compile and it has a validpublic void run() method, it does not implement the Runnable interface, so the compiler would complain when creating a Thread with an instance of it. Option 4 is incorrect because the run() method must be public.

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


Correct Option: D
Explanation:

It gives different output while executing the same compiled code at different times. C:>javac Test.java C:>java Test ABBCAD C:>java Test ACADCB C:>java Test ACBCBAD C:>java Test ABBCAD C:>java Test ACBCBAD C:>java Test ACBCBAD C:>java Test ABBCAD

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...


Correct Option: C
Explanation:

Both threads are operating on the same instance variables. Because the code is synchronized, the first thread will complete before the second thread begins. Modify line 17 in the following way to print the thread name. System.out.println(Thread.currentThread().getName() + x = + x + , y = + y);

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.


Correct Option: B
Explanation:

You have two different threads that share one reference to a common object. The updating and output takes place inside the synchronized code. One thread will run to complete printing the numbers 1-10. The second thread will then run to complete printing the numbers 11-20.

- Hide questions