Threads(Java Program ) Questions and Answers

Question 1. Analyze the following code:
public class Test implements Runnable{
public static void main(String[] args){
Test t = new Test();
}
public Test(){
Thread t = new Thread(this);
t.start();
}
public void run(){
System.out.println("test");
}
}
  1.    The program has a compilation error because t is defined in both the main() method and the constructor Test().
  2.    The program compiles fine, but it does not run because you cannot use the keyword this in the constructor.
  3.    The program compiles and runs and displays nothing.
  4.    The program compiles and runs and displays test.
Explanation:-
Answer: Option D. -> The program compiles and runs and displays test.

Question 2. What will happen when you attempt to compile and run the following code?
1. public class Test extends Thread{
2. public static void main(String argv[]){
3. Test t = new Test();
4. t.run();
5. t.start();
6. }
7. public void run(){
8. System.out.println("run-test");
9. }
10. }
  1.    run-test run-test
  2.    run-test
  3.    Compilation fails due to an error on line 4
  4.    Compilation fails due to an error on line 7
  5.    None of these
Explanation:-
Answer: Option A. -> run-test run-test
t.run() Legal, but does not start a new thread , it is like a method call of a class Test BUT t.start() creates a thread and call run() method.

Question 3. What will be the output?
class One extends Thread{
public void run(){
for(int i=0; i
  1.    0 0
  2.    Compilation Error
  3.    0 1
  4.    None of these
Explanation:-
Answer: Option C. -> 0 1

Question 4. Analyze the following code:
public class Test implements Runnable{
public static void main(String[] args){
Test t = new Test();
t.start();
}
public void run() { }
}
  1.    The program does not compile because the start() method is not defined in the Test class.
  2.    The program compiles, but it does not run because the start() method is not defined.
  3.    The program compiles, but it does not run because the run() method is not implemented.
  4.    The program compiles and runs fine.
Explanation:-
Answer: Option A. -> The program does not compile because the start() method is not defined in the Test class.

Question 5. What will happen after compiling and running following code?
class A implements Runnable{
public void run(){
System.out.println("run-a");
}
}
1. public class Test{
2. public static void main(String... args){
3. A a = new A();
4. Thread t = new Thread(a);
5. t.start();
6. t.start();
7. }
8. }
  1.    run-a
  2.    run-a run-a
  3.    Compilation fails with an error at line 6
  4.    Compilation succeed but Runtime Exception
  5.    None of these
Explanation:-
Answer: Option D. -> Compilation succeed but Runtime Exception
Once a thread has been started, it can never be started again. 2nd time t.start() throws java.lang.IllegalThreadStateException.

Question 6. 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);
}
}
}
  1.    A compile time error indicating that no run method is defined for the Thread class
  2.    A run time error indicating that no run method is defined for the Thread class
  3.    Clean compile and at run time the values 0 to 9 are printed out
  4.    Clean compile but no output at runtime
  5.    None of these
Explanation:-
Answer: Option D. -> Clean compile but no output at runtime

Question 7. 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");
}
}
  1.    Compilation fails.
  2.    An exception is thrown at runtime.
  3.    "BeginRunEnd" is printed.
  4.    "BeginEndRun" is printed.
  5.    "BeginEnd" is printed.
Explanation:-
Answer: Option C. -> "BeginRunEnd" is printed.

Question 8. What is the output for the below code ?
class A implements Runnable{
public void run(){
System.out.println(Thread.currentThread().getName());
}
}
1. public class Test{
2. public static void main(String... args){
3. A a = new A();
4. Thread t = new Thread(a);
5. t.setName("good");
6. t.start();
7. }
8. }
  1.    good
  2.    null
  3.    Compilation fails with an error at line 5
  4.    Compilation succeed but Runtime Exception
  5.    None of these
Explanation:-
Answer: Option A. -> good
Thread.currentThread().getName() return name of the current thread.

Question 9. Which keyword when applied on a method indicates that only one thread should execute the method at a time.
  1.    volatile
  2.    synchronized
  3.    native
  4.    static
  5.    final
Explanation:-
Answer: Option B. -> synchronized

Question 10. 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);
}
}
  1.    Prints
  2.    Prints
  3.    Prints
  4.    Compiler error
  5.    IllegalThreadStateException is thrown
Explanation:-
Answer: Option C. -> Prints
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.