SCJP Module-8 Question-6


 

SCJP Module-8 Question-6

This example will check your understanding about the Thread class and their execution in Java.

This example will check your understanding about the Thread class and their execution in Java.

Given a sample code:

1    public class Test extends Thread {

2    public static void main(String[] args) throws Exception {
3    Test t = new Test();
4    t.start();
5    t.method();
    }

6    public void run() {
7    System.out.println("run");
    }

8    public void method() {
9    System.out.println("method");
}}

Which of the following statement is correct ?

(A) Print "run method"
(B) Print "method run"
(C) Raise runtime exception
(D) Compilation error at line no 3.

Answer

(B) Print "method run"

Reason

At first the stat() method is called and this will start the thread but just after the start method the method() is called which will call the "method" and after this the run() method will be called automatically. Therefore the output is "method run".

Ads