ExamVeda
Login
Home
41
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();
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

t.join(); means Threat t must finish before Thread t1 starts.

42
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();
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Class A extends Thread means the anonymous instance that is passed to check() method has a start method which then calls the run method.

43
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());
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

Once the start() method is called, the thread is considered to be alive.

44
Which of the following are methods of the Thread class?
1) yield()
2) sleep(long msec)
3) go()
4) stop()
Discuss
Answer & Solution
Answer: Option A
No explanation is given for this question. Let's Discuss on Board
45
What notifyAll() method do?
Discuss
Answer & Solution
Answer: Option C
Solution:

notifyAll() : Wakes up all threads that are waiting on this object's monitor.A thread waits on an object's monitor by calling one of the wait methods.

46
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);
            }
      }
}
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
47
Which keyword when applied on a method indicates that only one thread should execute the method at a time.
Discuss
Answer & Solution
Answer: Option B
No explanation is given for this question. Let's Discuss on Board
48
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();
      }
}
Discuss
Answer & Solution
Answer: Option A
Solution:

Thread.currentThread().getName() return name of the current thread.

49
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);
      }
}
Discuss
Answer & Solution
Answer: Option C
Solution:

Here, firstly the run() method of the object referred by a is directly invoked. When the run() method of a thread is invoked instead of the start() method, it is executed by the same thread as a conventional method. So it increments i to 1, now the output is 1. After this, the invocation of the start() method schedules a new thread of execution.

Now it is impossible to predict whether the new thread will run first or the second print statement will be executed by the main thread first. So the result of the second print statement can be 1 or 2 depending on the scheduling of the 2 threads.

50
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");
      }
}
Discuss
Answer & Solution
Answer: Option C
No explanation is given for this question. Let's Discuss on Board