Threads(Computer Science > Java Program ) Questions and Answers

Question 1. In java a thread can be created by ..........
  1.    Extending the thread class.
  2.    Implementing Runnable interface.
  3.    Both of the above
  4.    None of these
Explanation:-
Answer: Option C. -> Both of the above

Question 2. When a class extends the Thread class ,it should override ............ method of Thread class to start that thread.
  1.    start()
  2.    run()
  3.    init()
  4.    go()
Explanation:-
Answer: Option B. -> run()

Question 3. What will be the output of the following program code?
public class Test implements Runnable{
public static void main(String[] args){
Thread t = new Thread(this);
t.start();
}
public void run(){
System.out.println("test");
}
}
  1.    The program does not compile because this cannot be referenced in a static method.
  2.    The program compiles fine, but it does not print anything because t does not invoke the run() method
  3.    The program compiles and runs fine and displays test on the console.
  4.    None of the above
Explanation:-
Answer: Option A. -> The program does not compile because this cannot be referenced in a static method.

Question 4. Which of the following constructor of class Thread is valid one?
  1.    Thread(Runnable threadOb, int priority)
  2.    Thread(int priority)
  3.    Thread(Runnable threadOb, String threadName)
  4.    Thread(String threadName, int priority)
  5.    None of these
Explanation:-
Answer: Option C. -> Thread(Runnable threadOb, String threadName)

Question 5. Analyze the following code:
public abstract class Test implements Runnable{
public void doSomething() { };
}
  1.    The program will not compile because it does not implement the run() method.
  2.    The program will not compile because it does not contain abstract methods.
  3.    The program compiles fine.
  4.    None of the above
Explanation:-
Answer: Option C. -> The program compiles fine.

Question 6. 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 7. 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 8. 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 9. 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

Question 10. 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