Java Nested try-catch MCQ

Exception Handling MCQ Part – 7 | Java Nested try-catch MCQ | Also see:- Exception Handling Interview Questions in JavaMCQ in Java

Q1) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
      } catch(ArithmeticException e) {
         System.out.println("B");
      }

      try {
         System.out.print("C");
      } catch(ArithmeticException e) {
         System.out.println("D");
      } 
   }
}

a) AC
b) BD
c) ABCD
d) Compile-time error

View Answer Answer:- a) AC

We can use more than one try block in a method.

Q2) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
      } catch(String e) {
         System.out.println("B");
      } 
   }
}

a) A
b) B
c) Compile-time error
d) Runtime error

View Answer Answer:- c) Compile-time error

In catch block, parameter must be of type java.lang.Throwable or its subclass else it leads to compile time error. In this program we will get:- incompatible types: String cannot be converted to Throwable.

Q3) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      System.out.print("A");
      try {
         System.out.print("B");
      } 
      System.out.print("C");
   }
}

a) ABC
b) AC
c) Compile-time error
d) Runtime exception

View Answer Answer:- c) Compile-time error

The try block must follow either 0 or “N” number of catch blocks or “1” finally block else it leads to compile time error:- “try without catch or finally”

Q4) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
         try {
            System.out.print("B");
            System.out.print(9/0);
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
      } catch(NumberFormatException e) {
         System.out.print("D");
      } catch(ArithmeticException e) {
         System.out.print("E");
      } catch(Exception e) {
         System.out.print("F");
      } 
   }
}

a) AB; ArithmeticException
b) ABE
c) Compile-time error
d) No output

View Answer Answer:- b) ABE

Q5) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
         try {
            System.out.print("B");
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
         System.out.print("D");
      } catch(NumberFormatException e) {
         System.out.print("E");
      } 
   }
}

a) ABCD
b) AB
c) ABD
d) ABE

View Answer Answer:- c) ABD

Q6) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      System.out.print(9/0);
      try {
         System.out.print("A");
         try {
            System.out.print("B");
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
         System.out.print("D");
      } catch(NumberFormatException e) {
         System.out.print("E");
      } 
   }
}

a) Compile-time error
b) Exception, program terminated
c) ABD
d) None of these

View Answer Answer:- b) Exception, program terminated

Exception is raised before the outer try block.

Q7) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
         System.out.print(Integer.parseInt("X"));
         try {
            System.out.print("B");
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
         System.out.print("D");
      } catch(NumberFormatException e) {
         System.out.print("E");
      } 
   }
}

a) AB
b) ABD
c) AE
d) Exception, program terminated

View Answer Answer:- c) AE

Exception is raised in the outer try before the inner try-catch block, therefore the inner try-catch block won’t do anything for this exception.

Q8) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
         try {
            System.out.print("B");
            System.out.print(Integer.parseInt("X"));
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
         System.out.print("D");
      } catch(NumberFormatException e) {
         System.out.print("E");
      } 
      System.out.print("F");
   }
}

a) ABC
b) ABDE
c) ABCDF
d) Exception, program terminated

View Answer Answer:- c) ABCDF

Exception raised in inner try block. If an exception matches both the inner and outer catch block then the inner block will be executed.

Q9) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
         try {
            System.out.print("B");
            System.out.print(9/0);
         } catch(NumberFormatException nfe) {
            System.out.print("C");
         }
         System.out.print("D");
      } catch(NumberFormatException e) {
         System.out.print("E");
      } 
      System.out.print("F");
   }
}

a) AB; ArithmeticException
b) ABE; ArithmeticException
c) ABDE; ArithmeticException
d) ABE

View Answer Answer:- a) AB; ArithmeticException

Exception is raised in the inner try block but the catch block is not matched in both inner & outer block therefore the program will be terminated abnormally.

Q10) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.print("A");
      } catch(NumberFormatException nfe) {
         System.out.println("B");
      } finally {
         System.out.print("C");
      }
      System.out.print("D");
   }
}

a) AD
b) AC
c) ACD
d) BC

View Answer Answer:- b) ACD

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *