Skip to main content

Brainstorming: Exception Handling

1. What is the output of this program?

class MyException extends Exception
{
   MyException()
     {
    super("Exception Occured");
     }
   public String toString()
     {
    return "Problem";
     }
}

class Output
{
   public static void main(String args[])
     {
    try {
        throw new MyException();
        }
        catch(MyException e)
        {
        System.out.print(e);
        }
     }
}

A) Exception Occured
B) MyException: Exception Occured
C) Problem
D) None of These

2. Identify the incorrect statement among the following.

A) throw is used to explicitly throw a user defined exception.
B) throws is used to specify that the method can throw exception
c) Throwable is an abstract class which is parent of Exception.
D) None of the above.

3. What will be the Output of the following Program?
public class Test
{
   public static void main(String[] args)
     {
    int[] array = new int[2];
    for (int i=0; i<2; i++)
      {
       try{
        array[i] = i;
        System.out.println(i);
          }
       catch(NullPointerException e)
          {
        System.out.println("NullPointer Exception");
          }
       finally
          {
        System.out.println("Finally Executed");
        array = null;
          }
      }
     }
}

A) 0
   Finally Executed
   NullPointer Exception
   Finally Executed

B) 0
   Finally Executed
   1
   Fially Executed   

C) 0
   Finally Executed

D) None of These


4. What will be the Output of the following Program?

class exception_handling {
public static void main(String args[]) {
    try {    throw new Error(); }
    catch(Throwable e) {
        System.out.print("Hello");}
    catch(Exception e) {
        System.out.print("Hi");}
    }
  }

A) Hello
B) Hi
C) Compilation Error
D) None of These

5. What will be the Output of the following Program?


class exception_handling
   {
     public static void main(String args[])
    {
       try  {   
          throw new Error();
        }
       catch(Throwable e)
        {
          System.out.print("Throwable");
        }
    catch(Exception e)
        {
          System.out.print("Exception");
        }
    }
  }

A) Throwable
B) Exception
C) Compilation Error
D) None of These

6. What happens in a method if an exception is thrown in a try{} and there is NO MATCHING catch block?
A) Program will not Compile.
B) The program halts immediately.
C) The program ignores that exception.
D) The method passes the exception to its caller.

7. Which statement is FALSE about catch block?

A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) If there is no catch block, there must be a finally{}.
D) Both A & B

8. What will be the Output of following Program?
class Myexception extends Exception
   {
     int detail;
     Myexception(int a) {  detail = a; }
     public String toString() { return "detail";}
   }

class Output {
    static void compute (int a) throws Myexception
          {   throw new Myexception(a); }
    public static void main(String args[]) {
        try {    compute(3); }
        catch(Myexception e) {
            System.out.print("Exception " + e);}
        }
   }

A) Exception detail
B) Exception 3
C) 3
D) Compile time Error

9. When a method can throw an exception then it is specified by _____ keyword.

A) throws
B) throw
C) finally
D) catch

10. What change(s) should be made to the program to get the output like below?
Output:    0
                 1

class ExceptionHandling{                
   public static void main(String[] args){       
    int[] intarray = new int[2];
    int i=0;           
    try{                                 
       for ( ; i<2; i++){            
       intarray[i] = i;                  //Statement 1          
       System.out.println(i);              
       }                                              
    }                                                 
    
    finally{                               
             intarray = null;
         System.out.println(i);       //Statement 2                                        
           }                       
       } 
    }

A)    Removing just finally block.
B)    Removing just Statement 1.
C)    Removing just Statement 2.
D)    Removing finally block and Statement 1.

11. Which exception is thrown When an array element is accessed beyond the array size?
A) ArrayElementsOutOfLimitException
B) ArrayIndexOutOfBoundsException
C) ArrayOutOfBoundsException
D) ArrayElementOutOfBoundsException

12. Which statement is FALSE about try{} block?
A) Some of the statements in a try{} will never throw an exception.
B) The statements in a try{} may throw several types of exception.
C) The statements in a try{} can not include a loop.
D) The try{} can be followed by only a finally block.

13. How many finally{} may be there in a try/catch structure?
A) There must be one, following the last catch block.
B) There can be zero or one, following the last catch block.
C) There can be maximum one, following every catch block.
D) There can be any number of catch blocks after each catch block.

14. Which statement is FALSE about catch block?
A) There must be only one catch block in a try/catch structure.
B) The catch block for a child exception class must FOLLOW that of a parent exception class.
C) The catch block for a child exception class must PRECEED that of a parent exception class.
D) Both A & B

15. RuntimeException is Parent of _______________.
A) InterruptedException
B) NumberFormatException
C) IOException
D) Exception

Comments

  1. Good Post! Thank you so much for sharing the post, it was so good to read and useful to improve
    Java Training in Electronic City

    ReplyDelete

Post a Comment

Popular posts from this blog

Practice Set

1. Write a program to create a class Voter which contains attributes name, date_of_birth and voter_id and voter has a Voter_Card. Provide appropriate constructor to initialize all the attributes of the Voter but voter id must be assigned automatically only when the age of the voter is greater than or equal to 18 years. VoterCard is a nested class with attributes voter_id and Voter_name. Make sure that voter card is created only when user is a valid voter and if it is already created then must not be assigned the new voter id. 2. Write a program to define two interfaces UGC and AICTE both having a default method int getAdmission() to take the admission and an abstract method String payFee(). getAdmission() in UGC must ask the percentage in qualifying exam and if the percentage >= 60 then generate the registration number and return. getAdmission() is AICTE must ask the user to join the counseling after 5 days and display the date of counseling and return the counseling token numb...

Practice Set: Unit-4

                                                                                                                Practice Set: Unit 4 1. What are the uses of super keyword in Java? Explain with a suitable example. 2. What are the various uses of final keyword in java? An abstract method can not be declared final. Why? 3. Differentiate between Abstract class and Interface. What do you mean by Functional interface and Lambda Expression?  ...

ETE Practice Set-1

                                                                 Mock Test-1 -----------------------------------------------------------------------------------------------------------------------------   1 (a) Write a program that reads 10 integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0.                                                         ...