Exceptions


  1. What is the output of this program?
    public class Exec_Handling_Example 
    {
    public static void main(String args[])
    {
    try
    {
    int k, n;
    n = 5;
    for (k = -2; k < 2 ;++k)
    {
    n = (n / k);
    System.out.print(k);
    }
    }
    catch(ArithmeticException e)
    {
    System.out.print("0");
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    For the 1st iteration -1 is displayed. The 2nd exception is caught in catch block and 0 is displayed.


  1. Which of these keywords is used to generate an exception explicitly?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    throw



  1. Which of these class is related to all the exceptions that are explicitly thrown?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    Throwable


  1. What is the output of this program?
    public class exceptionHandling_Example
    {
    public static void main(String args[])
    {
    try
    {
    int n = args.length;
    int n1 = 9 / n;
    System.out.print(n);
    try
    {
    if (n == 1)
    n = n / n - n;
    if (n == 2)
    {
    int s = {1};
    s[8] = 9;
    }
    }
    catch (ArrayIndexOutOfBoundException e)
    {
    System.out.println("Hello");
    }
    catch (ArithmeticException e)
    {
    System.out.println("Java");
    }
    }
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Compilation Error

    error: 'try' without 'catch', 'finally' or resource declarations
    try
    ^
    1 error



  1. Which of these keywords is used to by the calling function to guard against the exception that is thrown by called function?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    If a method is capable of causing an exception that it does not handle. It must specify this behaviour the behaviour so that callers of the method can guard themselves against that exception. This is done by using throws clause in methods declaration.