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 MCQs

  1. What will be the Output of the following code? class X{                  static int x = 1;                        static class Y {                              static int y = x++;                                static class Z                                                                       {                                                                                         static int z = ++y;                                                                        }                                               }                }   class MainClass{     public static void main(String[] rk)    {                             System.out.print(X.x);                                             System.out.print(X.Y.y);                             System.out.print(X.Y.Z.z);         } } A. 113 B. 123 C. 111 D. None of These Ans: A 2.  Which of the following statement is TRUE about t he following code? public class Outer  {    private i

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

Mock Test - 1

1. Which of the following is a byte stream class? A. ObjectInputStream B. Writer C. PrintWriter D. Scanner Ans: A 2. Which exception is thrown by read() method in InputStream class? A. IOException B. InterruptedException C. FileNotFoundException D. None of These Ans: A 3. Which of the following class is subclass of FilterInputStream? A. InputStream B. ObjectOutputStream C. FileInputStream D. BufferedInputStream  Ans: D 4. Which of these class contains the print() and println() methods? A. System B. out C. PrintStream D. BUfferedOutputStream Ans: C 5. Which of these is a method to clear all the data present in output buffer without terminating the stream? A. clear() B. fflush() C. flush() D. close() Ans: C 6. Which of these classes defined in java.io and used for file-handling are abstract? (i) InputStream        (ii) PrintStream    (iii) Reader        (iv) FileInputStream A. Only i     B. i and ii    C. Only iii    D. i and iii Ans: D 7. What is the output of this program? class MyE