Java try-catch MCQ – 2

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

Q1) Find the output of the below Java program?

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

a) AB; ArithmeticException
b) ABC; ArithmeticException
c) ABD; ArithmeticException
d) None of these

View Answer Answer:- b) ABC; ArithmeticException

Exception is raised in the try block, therefore the catch block will be executed. The exception is also raised in the catch block. Now, the program will be terminated without executing the remaining lines of code.

Q2) Find the output of the below program?

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

a) ACEF; ArithmeticException
b) ACEFD; ArithmeticException
c) ABC; ArithmeticException
d) None of these

View Answer Answer:- b) ACEFD; ArithmeticException

Q3) Find the output of the below Java program?

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

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

View Answer Answer:- d) AB; NumberFormatException

The exception is raised in the try block, but the catch block is not matched hence the program will be terminated without executing the catch block and remaining code.

Q4) Find the output of the below Java program?

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

a) ABCF
b) Compile-time error
c) ArithmeticException
d) None of these

View Answer Answer:- b) Compile-time error

In this program, the 1st line of the catch block contains the “d” variable which is not declared. If an exception is not raised in the try block then the catch block will not be executed. But it doesn’t mean the compiler will also ignore the errors in the catch block.

Q5) Find the output of the below Java program?

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

a) ABCDE; ArithmeticException
b) ABC; ArithmeticException
c) ArithmeticException
d) ABC

View Answer Answer:- b) ABC; ArithmeticException

Exception is not raised in the try block, therefore the catch block will not be executed. But the exception is raised at the last line which is not handled.

Q6) Find the output of the below Java program?

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

a) AB; NumberFormatException
b) ABDEF
c) ABF
d) Compile-time error

View Answer Answer:- b) ABDEF

NumberFormatException is raised in the try block, the catch block is handling “Exception” which is a super class of NumberFormatException. Hence exceptions will be handled by the catch block, and the program will be completed successfully.

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(9/0);
         System.out.print(Integer.parseInt("B"));
         System.out.print("C");
      } catch(ArithmeticException e) {
         System.out.println("D");
      } catch(NumberFormatException e) {
         System.out.println("E");
      }
   }
}

a) ADE
b) A
c) AD
d) AE

View Answer Answer:- c) AD

Q8) Find the output of the below program?

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

a) ACE
b) A; NumberFormatException
c) AC; NumberFormatException
d) ACE; NumberFormatException

View Answer Answer:- c) AC; NumberFormatException

Q9) Find the output of the below program?

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

a) AC
b) AD
c) ACD
d) Compile-time error

View Answer Answer:- d) Compile-time error

If we are using a try with multiple catch blocks then the order of the catch block is very important. We have to take the child first & then parent otherwise we will get a compile time error saying:- Exception has already been caught.

Q10) Find the output of the below program?

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

a) AC
b) AD
c) ACD
d) Compile-time error

View Answer Answer:- d) Compile-time error

We can’t declare more than one catch block to handle the same exception. Both catch blocks want to handle ArithmeticException, which is invalid.

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 *