Tuesday 24 July 2012

Exception Handling -Objective Questions -Part 1

1)

What will be the output of the program?

public class Test
{   public static void aMethod() throws Exception
    {  try
        { throw new Exception();  }
        finally
        {System.out.println("finally");
        } }
    public static void main(String args[])
    {  try
        {  aMethod(); }
        catch (Exception e)
        {System.out.println("exception");}
        System.out.println("finished");
    } }

A. finally
B. exception finished
C. finally exception finished
D. Compilation fails

Answer: C

This is what happens:

In the aMethod() exception is not handling,so the exception will rethrow after executing the finally block.Then the control will jump to try catch block in main() method and execute the catch block.Then the statement below the catch block will be executed.

2)

What will be the output of the program?

try
{
    int x = 0;
    int y = 5 / x;
}
catch (Exception e)
{
    System.out.println("Exception");
}
catch (ArithmeticException ae)
{
    System.out.println(" Arithmetic Exception");
}
System.out.println("finished");

A. finished
B. Exception
C. Compilation fails.
D. Arithmetic Exception

Answer: C

Compilation fails because ArithmeticException  has already been caught. ArithmeticException is a subclass of java.lang.Exception, by time the ArithmeticException has been specified it has already been caught by the Exception class.If ArithmeticException appears before Exception, then the file will compile. When catching exceptions the more specific exceptions must be listed before the more general (the subclasses must be caught before the superclasses).

3)

What will be the output of the program?

public class X
{  public static void main(String [] args)
    {  try {   badMethod();
            System.out.print("A");
        }catch (Exception ex)
        { System.out.print("B");
        } finally
        { System.out.print("C");}
        System.out.print("D");  }
    public static void badMethod()
    {throw new Error(); /* Line 22 */
    } }

A. ABCD
B. Compilation fails.
C. C is printed before exiting with an error message.
D. BC is printed before exiting with an error message.

Answer: C

Error is thrown but not recognised line(22) because the only catch attempts to catch an Exception  and
Exception is not a superclass of Error. Therefore only the code in the finally statement can be run before exiting with a runtime error (Exception in thread "main" java.lang.Error).

4)

What will be the output of the program?

public class X
{   public static void main(String [] args)
    { try {   badMethod();
            System.out.print("A");
        } catch (Exception ex)
        {System.out.print("B");
        } finally
        {System.out.print("C");
        }System.out.print("D"); }
    public static void badMethod() {}
}

A. AC
B. BC
C. ACD
D. ABCD

Answer: C

There is no exception thrown, so all the code with the exception of the catch statement block is run.

5)

Which four can be thrown using the throw statement?

   1. Error
   2. Event
   3. Object
   4. Throwable
   5. Exception
   6. RuntimeException

A. 1, 2, 3 and 4
B. 2, 3, 4 and 5
C. 1, 4, 5 and 6
D. 2, 4, 5 and 6

Answer: C

An Error is a subclass of Throwable .The Throwable class is the superclass of all errors and exceptions in the Java language.The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch (checked exceptions) .RuntimeException is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.

6)

What will be the output of the program?

public class X
{   public static void main(String [] args)
    {try {
            badMethod(); /* Line 7 */
            System.out.print("A");
        } catch (Exception ex) /* Line 10 */
        { System.out.print("B"); /* Line 12 */
        } finally /* Line 14 */
        {System.out.print("C"); /* Line 16 */
        } System.out.print("D"); /* Line 18 */
    } public static void badMethod()
    {throw new RuntimeException(); } }

A. AB
B. BC
C. ABC
D. BCD

Answer: D

(1) A RuntimeException is thrown, this is a subclass of exception.
(2) The exception causes the try to complete abruptly (line 7) therefore line 8 is never executed.
(3) The exception is caught (line 10) and "B" is output (line 12)
(4) The finally block (line 14) is always executed and "C" is output (line 16).
(5) The exception was caught, so the program continues with line 18 and outputs "D".

7)

What will be the output of the program?

public class RTExcept
{  public static void throwit ()
    {  System.out.print("throwit ");
       throw new RuntimeException();
    }  public static void main(String [] args)
    {  try
        {   System.out.print("hello ");
            throwit();
        }catch (Exception re )
        {System.out.print("caught ");}
        finally
        { System.out.print("finally ");
        } System.out.println("after ");
    }}

A. hello throwit caught
B. Compilation fails
C. hello throwit RuntimeException caught after
D. hello throwit caught finally after

Answer: D

The main() method properly catches and handles the RuntimeException in the catch block, finally runs (as it always does), and then the code returns to normal.A, B and C are incorrect based on the program logic described above. Remember that properly handled exceptions do not cause the program to stop executing.

8)

Which statement is true?

A. catch(X x) can catch subclasses of X where X is a subclass of Exception.
B. The Error class is a RuntimeException.
C. Any statement that can throw an Error must be enclosed in a try block.
D. Any statement that can throw an Exception must be enclosed in a try block.

Answer: A

Option A is correct. If the class specified in the catch clause does have subclasses, any exception object that subclasses the specified class will be caught as well.Option B is wrong. The error class is a subclass of Throwable and not Runtime Exception.Option C is wrong. You do not catch this class of error.Option D is wrong.An exception can be thrown to the next method higher up the call stack.

9)

 What will be the output of the program?

public class MyProgram
{
    public static void main(String args[])
    {
        try
        {
            System.out.print("Hello world ");
        }
        finally
        {
            System.out.println("Finally executing ");
        }
    }
}

A. Nothing. The program will not compile because no exceptions are specified.
B. Nothing. The program will not compile because no catch clauses are specified.
C. Hello world.
D. Hello world Finally executing

Answer: D

Finally clauses are always executed. The program will first execute the try block, printing Hello world, and will then execute the finally block, printing Finally executing.Option A, B, and C are incorrect based on the program logic described above. Remember that either a catch or a finally statement must follow a try. Since the finally is present, the catch is not required.

10)

public class ExceptionTest
{
    class TestException extends Exception {}
    public void runTest() throws TestException {}
    public void test() /* Point X */
    {
        runTest();
    }
}

At Point X on line 5, which code is necessary to make the code compile?

A. No code is necessary.
B. throws Exception
C. catch ( Exception e )
D. throws RuntimeException

Answer: B

Option A is wrong. If you compile the code as given the compiler will complain: "unreported exception must be caught or declared to be thrown".Option C is wrong. The catch statement belongs in a method body not a method specification.Option D is wrong. TestException is a subclass of Exception therefore the test method, in this example, must throw TestException or some other class further up the Exception tree.

No comments:

Post a Comment