Java multithreading quiz

A program can be divided into a number of small processes. Each small process can be addressed as a single thread (a lightweight process). Multithreaded programs contain two or more threads that can run concurrently. This means that a single program can perform two or more tasks simultaneously. For example, one thread is writing content on a file at the same time another thread is performing spelling check.

This quiz collection covers most basic to advanced aspect in multithreading implementation in Java.

This quiz is in Java quiz collection.

Start quiz
  • 1.

    Which two are valid constructors for Thread?

    1. Thread(Runnable r, String name)
    2. Thread()
    3. Thread(int priority)
    4. Thread(Runnable r, ThreadGroup g)
    5. Thread(Runnable r, int priority)
  • 2.

    Which two of the following methods are defined in class Thread?

    1. start()
    2. wait()
    3. notify()
    4. run()
    5. terminate()
  • 3.

    Under which conditions will a currently executing thread stop?

    1. When an interrupted exception occurs.
    2. When a thread of higher priority is ready (becomes runnable).
    3. When the thread creates a new thread.
    4. When the stop() method is called.
  • 4.

    Which three guarantee that a thread will leave the running state?

    1. yield()
    2. wait()
    3. notify()
    4. notifyAll()
    5. sleep(1000)
    6. aLiveThread.join()
    7. Thread.killThread()
  • 5.

    Which will contain the body of the thread?

  • 6.

    Which of the following will not directly cause a thread to stop?

  • 7.

    Which statement is true?

    1. If only one thread is blocked in the wait method of an object, and another thread executes the modify on that same object, then the first thread immediately resumes execution.
    2. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, it is still possible that the first thread might never resume execution.
    3. If a thread is blocked in the wait method of an object, and another thread executes the notify method on the same object, then the first thread definitely resumes execution as a direct and sole consequence of the notify call.
    4. If two threads are blocked in the wait method of one object, and another thread executes the notify method on the same object, then the first thread that executed the wait call first definitely resumes execution as a direct and sole consequence of the notify call.
  • 8.

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

    Which two statements are true for any concrete class implementing the java.lang.Runnable interface?

    1. You can extend the Runnable interface as long as you override the public run() method.
    2. The class must contain a method called run() from which all code for that thread will be initiated.
    3. The class must contain an empty public void method named run().
    4. The class must contain a public void method named runnable().
    5. The class definition must include the words implements Threads and contain a method called run().
    6. The mandatory method must be public, with a return type of void, must be called run(), and cannot take any arguments.
  • 10.

    Which method registers a thread in a thread scheduler?

  • 11.

    which of these will create and start this thread?

    public class MyRunnable implements Runnable 
    {
        public void run() 
        {
            // some code here
        }
    }
  • 12.

    What will be the output of the program?

    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 ");
        }
    }
  • 13.

    What will be the output of the program?

    class MyThread extends Thread 
    { 
        MyThread() {} 
        MyThread(Runnable r) {super(r); } 
        public void run() 
        { 
            System.out.print("Inside Thread ");
        } 
    } 
    class MyRunnable implements Runnable 
    { 
        public void run() 
        { 
            System.out.print(" Inside Runnable"); 
        } 
    } 
    class Test 
    {  
        public static void main(String[] args) 
        { 
            new MyThread().start(); 
            new MyThread(new MyRunnable()).start(); 
        } 
    }
  • 14.

    What will be the output of the program?

    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(); 
        } 
    }
  • 15.

    What will be the output of the program?

    class Happy extends Thread 
    { 
        final StringBuffer sb1 = new StringBuffer(); 
        final StringBuffer sb2 = new StringBuffer(); 
    
        public static void main(String args[]) 
        { 
            final Happy h = new Happy(); 
    
            new Thread() 
            { 
                public void run() 
                { 
                    synchronized(this) 
                    { 
                        h.sb1.append("A"); 
                        h.sb2.append("B"); 
                        System.out.println(h.sb1); 
                        System.out.println(h.sb2); 
                    } 
                } 
            }.start(); 
    
            new Thread() 
            { 
                public void run() 
                { 
                    synchronized(this) 
                    { 
                        h.sb1.append("D"); 
                        h.sb2.append("C"); 
                        System.out.println(h.sb2); 
                        System.out.println(h.sb1); 
                    } 
                } 
            }.start(); 
        } 
    }
  • 16.

    What will be the output of the program?

    class MyThread extends Thread 
    {
        public static void main(String [] args) 
        {
            MyThread t = new MyThread();
            Thread x = new Thread(t);
            x.start(); /* Line 7 */
        }
        public void run() 
        {
            for(int i = 0; i < 3; ++i) 
            {
                System.out.print(i + "..");
            }
        }
    }
  • 17.

    What will be the output of the program?

    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;
        }
    }

    and assuming that data must be protected from corruption, what—if anything—can you add to the preceding code to ensure the integrity of data?

  • 18.

    What will be the output of the program?

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

© 2017 QuizBucket.org