Java OCA OCP Practice Question 2512

Question

Select the correct options for the following code.

class MyClass extends Thread {
    String name;/* w  w  w  .ja va 2  s .  co  m*/
    MyClass(String name) { this.name = name; }
    public void run() throws InterruptedException {     //1
        sleep(1000);                                    //2
        System.out.println("executing-" + name);
    }
}
public class Main {
    public static void main(String args[]) {
        MyClass app1 = new MyClass("abc");
        MyClass app2 = new MyClass("def");
        app1.start();
        app2.start();
    }
}
a  The output is/*  w  w w .ja v  a  2  s.  c  o m*/

   executing-abc
   executing-def

b  The output is

   executing-def
   executing-abc

c  The thread appl sleeps for a maximum of 1000 milliseconds before it's rescheduled to run again.
d  The treads app1 and app2 might sleep at the same time.
e  Compilation error
f  Runtime exception


e

Note

Method run() in class MyClass can't override method run() in class Thread by declaring to throw a checked InterruptedException.

The class fails to compile.




PreviousNext

Related