Vidyalelo
Java Programming · all questions

Threads
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

105

Questions

3/6

Page

Pick an option on a question to see the right answer and solution.

Predict the output:
class A implements Runnable{
      public void run(){
            try{
                  for(int i=0;i<4;i++){
                        Thread.sleep(100);
                        System.out.println(Thread.currentThread().getName());
                  }
            }catch(InterruptedException e){
            }
      }
}

public class Test{
      public static void main(String argv[]) throws Exception{
            A a = new A();
            Thread t = new Thread(a, "A");
            Thread t1 = new Thread(a, "B");
            t.start();
            t.join();
            t1.start();
      }
}

Select an option to see the answer and solution.

What will be the output?
class A extends Thread{
      public void run(){
            for(int i=0; i<2; i++){
                  System.out.println(i);
            }
      }
}

public class Test{
      public static void main(String argv[]){
            Test t = new Test();
            t.check(new A(){});
      }
      public void check(A a){
            a.start();
      }
}

Select an option to see the answer and solution.

What will happen when you attempt to compile and run the following code?
class A implements Runnable{
      public void run(){
            System.out.println("run-A");
      }
}

public class Test{
      public static void main(String argv[]){
            A a = new A();
            Thread t = new Thread(a);
            System.out.println(t.isAlive());
            t.start();
            System.out.println(t.isAlive());
      }
}

Select an option to see the answer and solution.

Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()

Select an option to see the answer and solution.

What notifyAll() method do?

Select an option to see the answer and solution.

What is the output for the below code?
public class Test extends Thread{
      public static void main(String argv[]){
            Test t = new Test();
            t.run();
      }

      public void start(){
            for(int i = 0; i < 10; i++){
                  System.out.println("Value of i = " + i);
            }
      }
}

Select an option to see the answer and solution.

Which keyword when applied on a method indicates that only one thread should execute the method at a time.

Select an option to see the answer and solution.

What is the output for the below code?
class A implements Runnable{
      public void run(){
            System.out.println(Thread.currentThread().getName());
      }
}

public class Test{		
      public static void main(String... args){	
            A a = new A();
       Thread t = new Thread(a);
       t.setName("good");
       t.start();
      }
}

Select an option to see the answer and solution.

Predict the output:
public class Test extends Thread{
      private int i;
      public void run(){
            i++;
      }

      public static void main(String[] args){
            Test a = new Test();
            a.run();
            System.out.print(a.i);
            a.start();
            System.out.print(a.i);
      }
}

Select an option to see the answer and solution.

What will be the output after compiling and executing the following code?
public class Test implements Runnable{
      public static void main(String[] args) throws InterruptedException{
            Thread a = new Thread(new Test());
            a.start();
            System.out.print("Begin");
            a.join();
            System.out.print("End");
      }
    
      public void run(){
            System.out.print("Run");
      }
}

Select an option to see the answer and solution.

What will be output of the following program code?
public class Test implements Runnable{
      public void run(){
            System.out.print("go");
      }
    
      public static void main(String arg[]) {
            Thread t = new Thread(new Test());
            t.run();
            t.run();
            t.start();
      }
}

Select an option to see the answer and solution.

Given the code. What will be the result?
public class Test implements Runnable{
      public static void main(String[] args) throws InterruptedException{
            Thread a = new Thread(new Test());
            a.start();
        
            System.out.print("Begin");
            a.join();
            System.out.print("End");
      }
    
      public void run(){
            System.out.print("Run");
      }
}

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following Java code?
class newthread implements Runnable
{
Thread t;
newthread()
    {
  t = new Thread(this,"My Thread");
  t.start();
}
public void run()
    {
  System.out.println(t);
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class newthread extends Thread
{
Thread t;
newthread()
    {
  t = new Thread(this,"New Thread");
  t.start();
}
public void run()
    {
 System.out.println(t.isAlive());
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class newthread extends Thread
{
Thread t1,t2;
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();        
    }
}

Select an option to see the answer and solution.

What is true about threading?

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following Java code?
class newthread implements Runnable
{
Thread t;
newthread()
    {
  t = new Thread(this,"My Thread");
  t.start();
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}

Select an option to see the answer and solution.

What will be the output of the following Java code?
class newthread implements Runnable 
{
Thread t;
newthread() 
    {
  t = new Thread(this,"My Thread");
  t.start();
}
public void run()
    {
  System.out.println(t.getName());
}
}
class multithreaded_programing
{
    public static void main(String args[])
    {
        new newthread();        
    }
}

Select an option to see the answer and solution.