Wednesday 25 July 2012

java.lang.Thread - Objective Questions - Part 1


1)
class Main implements Runnable {
    public void run() {
        System.out.println("thread");
    }

    public static void main(String args[]) {
        Main m = new Main();
        m.start();
    }
}
// What is the result of attempting to compile and run the program?

  1. Prints thread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error
Answer:Compile-time error.

start() method is declared with in java.lang.Thread class.So here to start the thread use following
Thread t=new Thread(m);
m.start();
It will start a new thread and prints the message 'thread'.


2)
class Main extends Thread{
   
    public void run(String s){
        System.out.println("thread");
    }
    public static void main(String args[]){
        Main m=new Main();
                m.start();
        }
}

What is the result of attempting to compile and run the program?
  1. Prints thread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error

Answer:Prints Nothing

The thread of execution -the new call stack-always begins by invoking the method run().But here overloaded
version of run() is used.Hence nothing will be printed.

3)
class Main extends Thread{
   
    public void run(){
        System.out.println("thread");
    }
    public static void main(String args[]){
        Main m=new Main();
        m.run();
        }
}
What is the result of attempting to compile and run the program?
  1. Prints 'thread',execution happen in a new thread.
  2. Prints 'thread',execution won't happen in a new thread.
  3. Prints Nothing
  4. Compile-time error
Answer :Prints 'thread',execution won't happen in a new thread.

if you call the run() method directly yourself, execution won't happen in a new thread of execution with a separate call stack. It will just happen in the same call stack as the code that you made the call from, just like any other normal method call.To start a new thread use m.start(),it will implicitly call run() method.

4)
class Main extends Thread{
   
    public void run(){
        System.out.println("thread");
    }
    public static void main(String args[]){
        Main m=new Main();
        m.start();
    }
}

What is the result of attempting to compile and run the program?
  1. Prints thread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error
Answer:Prints thread

The above code will start a new thread and prints the message 'thread'.Threads can be created in two different ways,either extends the java.lang.Thread class or implements the java.lang.Runnable interface.

5)
class Main implements Runnable {
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }
    public static void main(String args[]) {
        Main m = new Main();
        Thread t = new Thread(m);
        t.setName("mythread");
        t.start();
    }
}
// What is the result of attempting to compile and run the program?
  1. Prints mythread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error
Answer :Prints mythread

Thread.currentThread(),which returns a reference to the currently executing thread.The getName(),setName(string) are used to get and set the name of a thread.

6)
class Main extends Thread {
    public void run() {
        System.out.print(" mythread ");
    }
    public static void main(String args[]) throws InterruptedException {
        Main m = new Main();
        m.start();
        m.join();
        System.out.println(" end ");
    }
}
// What is the result of attempting to compile and run the program?
  1. Prints mythread end
  2. Prints end mythread
  3. Compile-time error
  4. Run-time error
Answer:Prints mythread end

join() function to start a thread but tell it not to run until some other thread has finished.Here m.join() is used to wait the main thread until thread m is finished.Hence the result 'mythread end'.

7)
class Main extends Thread {
    public void run() {
        System.out.println("mythread");
    }
    public static void main(String args[]) throws InterruptedException {
        Main m = new Main();
        m.start();//Line 1
        m.join(); //Line 2
        m.start();//Line 3
    }
}
// What is the result of attempting to compile and run the program?
  1. Prints mythread
  2. Prints mythread and then run-time exception
  3. Prints Nothing
  4. Compile-time error
Answer:Prints mythread and then run-time exception

If you have a reference to a Thread, and you call start(), it's started. If you call start() a second time, it will cause an exception (an IllegalThreadStateException).

8)
class Main extends Thread{
   
    public void run(){
        System.out.println("thread");
    }
    public static void main(String args[]){
        Main m=new Main();//Line 1
        }
}
What is the result of attempting to compile and run the program?
  1. Prints thread
  2. Prints Nothing
  3. Compile-time error
  4. Run-time error
Answer :Prints Nothing

In order to start a thread we have to use the method 'start()' declared in java.lang.Thread class.So here if you use m.start() after line 1 ,a new thread will create and print the message 'thread'.

9)
class Main extends Thread {
    public void run() {
        System.out.println("mythread");
    }
    public static void main(String args[]) {
        Main m = new Main();
        m.start();
        m.join();
        System.out.println("end");
    }
}
// What is the result of attempting to compile and run the program?

  1. Prints mythread end
  2. Prints end mythread
  3. Compile-time error
  4. Run-time error
Answer:Compile-time error

For the method join() unhandled exception type InterruptedException.Either put join() inside try-catch blockor add throws clause to main method.

10)
class Main extends Thread {
    public void run() {
        System.out.println("mythread");
    }
    public static void main(String args[]) {
        Main m = new Main();
        m.start();
        Thread.sleep(1000);
        System.out.println("end");
    }
}
// What is the result of attempting to compile and run the program?
  1. Prints mythread end
  2. Prints end mythread
  3. Compile-time error
  4. Run-time error
Answer:Compile-time error

For the method sleep(() ,Unhandled exception type InterruptedException.Either put sleep() inside try-catch block or add throws clause to main method.

No comments:

Post a Comment