Java OCA OCP Practice Question 2185

Question

What will be the result of attempting to compile and run the following program?

public class Threader extends Thread {
  Threader(String name) {/*  w  w  w. ja  va  2 s  . co m*/
    super(name);
  }
  public void run() throws IllegalStateException {
    System.out.println(Thread.currentThread().getName());
    throw new IllegalStateException();
  }
  public static void main(String[] args) {
    new Threader("|T1|").start();
  }
}

Select the one correct answer.

  • (a) The program will fail to compile.
  • (b) The program will compile without errors, will print |T1|, and terminate normally every time the program is run.
  • (c) The program will compile without errors, will print |T1|, and throw an IllegalStateException, every time the program is run.
  • (d) None of the above.


(c)

Note

Note that the complete signature of the run() method does not specify a throws clause, meaning it does not throw any checked exceptions.

However, it can always be implemented with a throws clause containing unchecked exceptions, as is the case in the code above.




PreviousNext

Related