Java try-catch MCQ – 1

Exception Handling MCQ Part – 4 | Java try-catch MCQ – 1 | Also see:- Exception Handling Interview Questions in Java, MCQ in Java

Q1) Find the output of the below Java program?

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

a) AB
b) CD
c) ABCDE
d) ArithmeticException

View Answer Answer:- d) ArithmeticException

Exception in thread “main” java.lang.ArithmeticException: / by zero; at Test.main(Test.java:3). The “System.out.print(9/0);” line is outside of the try-catch block.

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("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(9/0);
      System.out.print("F");
   }
}

a) ArithmeticException
b) ACEFDB
c) ACEDB
d) ACE; ArithmeticException

View Answer Answer:- d) ACE; ArithmeticException

The exception is raised in the m2() method, after raising the exception program will be terminated abnormally.

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

a) ACD
b) ACDE
c) ABCDE
d) ArithmeticException

View Answer Answer:- b) ACDE

The exception is raised in the try block and it is matched with the catch block. Therefore, the program will be completed successfully.

Q4) Find the output of the below program?

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

a) A
b) A0
c) Compile-time error
d) Runtime exception

View Answer Answer:- a) A

Exception is raised in the try block, and the catch block is also matched but it isn’t performing any operation. Hence the program will run successfully.

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

a) ACD
b) ACDE
c) ABCDE
d) ArithmeticException

View Answer Answer:- c) ABCDE

Q6) Find the output of the below program?

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

   public static void m1() {
      System.out.print("C");
      m2();
      System.out.print(9/0);
      System.out.print("D");
   }

   public static void m2() {
      System.out.print("E");
      System.out.print("F");
   }
}

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

View Answer Answer:- a) ACEF; ArithmeticException

Q7) 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");
      catch (Exception e) {
         System.out.print("D");
      }
   }
}

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

View Answer Answer:- c) Compile-time error

The catch block must be placed immediately after the try block else it leads to compile time error: catch without try.

Q8) 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("C");
         System.out.print("D");
      }
      System.out.print("E");
   }
}

a) ABCDE
b) NumberFormatException
c) AB; NumberFormatException
d) ABE

View Answer Answer:- c) AB; NumberFormatException

The NumberFormatException raised in the try block but the catch block is not handling it therefore the program will be terminated abnormally, catch block & remaining lines won’t execute.

Q9) Find the output of the below program?

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

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

View Answer Answer:- d) Compile-time error

The try-catch blocks are not allowed at class level directly.

Q10) 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(9/0);
         System.out.print("D");
      }
      System.out.print("E");
   }
}

a) ABCE
b) ABC; ArithmeticException
c) ABCDE
d) None of these

View Answer Answer:- a) ABCE

In this program, exceptions will not be raised in the try block hence the catch block will not be executed. Program will complete successfully without executing a catch block.

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 *