Quiz: Exception Handling

To View Tricks: Login Required

Number of Questions: 11

Question: 6 -

Which exception is thrown when divide by zero statement executes?

Options:
  1. None of these

  2. ArithmeticException

  3. NumberFormatException

  4. NullPointerException

  5. Answer:

    ArithmeticException

    Solution:

    ArithmeticException is thrown when divide by zero statement executes.


Question: 7 -

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 */
    } 
}

Options:
  1. C is printed before exiting with an error message

  2. Compilation fails

  3. ABCD

  4. BC is printed before exiting with an error message

  5. Answer:

    C is printed before exiting with an error message

    Solution:

    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).


Question: 8 -

In which of the following package Exception class exist?

Options:
  1. java.lang

  2. java.file

  3. java.util

  4. java.io

  5. Answer:

    java.lang

    Solution not available.

Question: 9 -

Which of these is a super class of all errors and exceptions in the Java language?

Options:
  1. Throwable

  2. RunTimeExceptions

  3. None of the above

  4. Catchable

  5. Answer:

    Throwable

    Solution:

    Throwable is a super class of all errors and exceptions in the Java language


Question: 10 -

What will be the output of the program?

public class Foo 
{  
    public static void main(String[] args) 
    {
        try 
        { 
            return; 
        } 
        finally 
        {
            System.out.println( "Finally" ); 
        } 
    } 
}

Options:
  1. Compilation fails

  2. The code runs with no output

  3. Finally

  4. An exception is thrown at runtime

  5. Answer:

    Finally

    Solution not available.