My Report

Java Multithreading Test – 3


Correct Answer: 2 points | Wrong: -1 point
Grades: A* (100% score) | A (80%-99%) | B (60%-80%) | C (40%-60%) | D (0%-40%)
advertisement

1. Which of these keywords are used to implement synchronization?

2. What will be the output of the following Java program?

 
    class newthread extends Thread
    {
	Thread t;
	String name;
	newthread(String threadname)
        {
	    name = threadname;
	    t = new Thread(this,name);
	    t.start();
	}
	public void run()
        {
        }
     
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
	    newthread obj1 = 	 new newthread("one");
	    newthread obj2 =	 new newthread("two");
            try
            {
                obj1.t.wait();	
                System.out.print(obj1.t.isAlive());
            }
            catch(Exception e)
            {
	    System.out.print("Main thread interrupted");
            }
        }
    }

3. Which of this method is used to avoid polling in Java?

4. Which of these method wakes up all the threads?

5. Which of these method wakes up the first thread that called wait()?

6. Which of these method is used to tell the calling thread to give up a monitor and go to sleep until some other thread enters the same monitor?

7. What is synchronization in reference to a thread?

8. What will be the output of the following Java program?

 
    class newthread extends Thread
    {
	Thread t;
	newthread()
        {
	    t1 = new Thread(this,"Thread_1");
	    t2 = new Thread(this,"Thread_2");
	    t1.start();
	    t2.start();
	}
	public void run()
        {
	    t2.setPriority(Thread.MAX_PRIORITY);	
	    System.out.print(t1.equals(t2));
        }    
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
            new newthread();        
        }
    }

9. What will be the output of the following Java program?

 
    class newthread extends Thread
    {
	Thread t;
	String name;
	newthread(String threadname)
        {
	    name = threadname;
	    t = new Thread(this,name);
	    t.start();
	}
	public void run()
        {
        }
     
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
	    newthread obj1 = 	 new newthread("one");
	    newthread obj2 =	 new newthread("two");
            try
            {
                Thread.sleep(1000);	
                System.out.print(obj1.t.isAlive());
            }
            catch(InterruptedException e)
            {
	    System.out.print("Main thread interrupted");
            }
        }
    }

10. What will be the output of the following Java program?

 
    class newthread extends Thread
    {
	Thread t;
	String name;
	newthread(String threadname)
        {
	    name = threadname;
	    t = new Thread(this,name);
	    t.start();
	}
	public void run()
        {
        }
     
    }
    class multithreaded_programing
    {
        public static void main(String args[])
        {
	    newthread obj1 = 	 new newthread("one");
	    newthread obj2 =	 new newthread("two");
            try
            {
                 System.out.print(obj1.t.equals(obj2.t));
            }
            catch(Exception e)
            {
	    System.out.print("Main thread interrupted");
            }
        }
    }

 

Start practicing “1000 MCQs on Java”, and once you are ready, you can take tests on all topics by attempting our “Java Test Series”.

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.