Java OCA OCP Practice Question 2670

Question

Consider the following program and choose a right option:

class MyThread extends Thread {
        public void run(){
                System.out.println("Running");
        }/*from  ww w .j  a va  2s  . com*/
        public static void main(String args[]) throws InterruptedException  {
                Runnable r = new MyThread();      //#1
                Thread myThread = new Thread(r);  //#2
                myThread.start();
        }
}
  • a) The program will result in a compilation error at statement #1.
  • b) The program will result in a compilation error at statement #2.
  • c) The program will compile with no errors and will print "Running" in the console.
  • d) The program will compile with no errors but does not print any output in the console.


c)

Note

The class Thread implements the Runnable interface, so the assignment in statement #1 is valid.

Also, you can create a new thread object by passing a Runnable reference to a Thread constructor, so statement #2 is also valid.

Hence, the program will compile without errors and print "Running" in the console.




PreviousNext

Related