Thursday, 10 September 2009

Java Certification Question 0091

What will happen when you attempt to compile and run the following code

import java.io.*;

class Base{

public static void amethod()throws FileNotFoundException{}
}

public class ExcepDemo extends Base{

public static void main(String argv[]){
ExcepDemo e = new ExcepDemo();
}

public static void amethod(){}

protected ExcepDemo(){
try{
DataInputStream din = new DataInputStream(System.in);
System.out.println("Pausing");
din.readChar();
System.out.println("Continuing");
this.amethod();
}catch(IOException ioe) {}
}
}

1)Compile time error caused by protected constructor
2) Compile time error caused by amethod not declaring Exception
3) Runtime error caused by amethod not declaring Exception
4) Compile and run with output of "Pausing" and "Continuing" after a key is hit




Answer )

4) Compile and run with output of "Pausing" and "Continuing" after a key is hit

An overriden method in a sub class must not throw Exceptions not thrown in the base class. In the case of the method amethod it throws no exceptions and will thus compile without complain. There is no reason that a constructor cannot be protected.

No comments:

Post a Comment