J2EEOnline J2EE 





Java Concepts  «Prev  Next»
Creating Starting Java Thread - Quiz

Each question is worth one point. Select the best answer or answers for each question.
 

1. The following code will successfully create and start a new thread
Please select the best answer.
  A. Thread t = new Thread();
  B. Thread t = new Thread(); t.run();
  C. Thread t = new Thread(); t.start();
  D. None of the above.


2. To write a thread class that will do something useful, you should
Please select the best answer.
  A. Make sure to start it
  B. Create a subclass of a thread
  C. Supply a run() method for the thread
  D. None of the above.


3. What will the following code print when run?
public class RunTest {
 public static volatile int counter = 0;
  static class RunnerDec implements Runnable{
   public void run(){
    for(int i=0;i < 5000; i++){
     counter--;
    }
   }
 }
 static class RunnerInc implements Runnable{
  public void run(){
   for(int i=0;i <5000; i++){
    counter++;
   }
  }
 }
 public static void main(String[] args) {
  RunnerDec rd = new RunnerDec();
  RunnerInc ri = new RunnerInc();
  Thread t1 = new Thread(rd);
  Thread t2 = new Thread(ri);
  t1.start();
  t2.start();
  try{
   t1.join();
   t2.join();
  }catch(Exception e){
    e.printStackTrace();
  }
  System.out.println(counter);
 }
} 
Select 1 option: Please select the best answer.
  A. It will always print 0.
  B. It may print any number between -5000 to 5000.
  C. It may print any number between -5000 to 5000 except 0.
  D. The program will keep running for ever.

Score =
Correct answers: