Java try-catch MCQ – 3

Exception Handling MCQ Part – 6 | Java try-catch MCQ – 3 | 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.println("Hello");
      } catch(ArithmeticException e) { }
   }
}

a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output

View Answer Answer:- a) Hello

The ArithmeticException is an unchecked exception therefore if we are catching an ArithmeticException type exception then the compiler will not give any error if the try block will not raise ArithmeticException. It will be compiled successfully and give “Hello” as output.

Q2) Find the output of the below program?

import java.io.*;
public class Test {
   public static void main(String[] args) {
      try {
         System.out.println("Hello");
      } catch(IOException e) { }
   }
}

a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output

View Answer Answer:- b) Compile-time error

The IOException is a fully checked exception therefore we should catch it only when try-block throws IOException. In this program, we will get a compile time error:- exception IOException is never thrown in the body of the corresponding try statement.

Q3) Find the output of the below program?

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

a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output

View Answer Answer:- a) Hello

The Exception is a partially checked exception therefore if we are catching an Exception type exception then the compiler will not give any error when the try block will not raise an Exception type exception.

Q4) Find the output of the below program?

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

a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output

View Answer Answer:- b) Compile-time error

The InterruptedException is also a fully checked exception therefore we should catch it only when try block throw InterruptedException. In this program, we will get a compile time error:- exception InterruptedException is never thrown in the body of the corresponding try statement.

Q5) Find the output of the below program?

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

a) Hello
b) Compile-time error
c) Exception will be raised
d) Compiled Successfully, but no output

View Answer Answer:- a) Hello

The Error is a partially checked exception therefore if we are catching Error type exception then compiler will not give any error when the try block will not raise Error type exception.

Note:- Within the try block if there is no chance of raising an exception then we can’t write a catch block for that exception otherwise we will get a compile-time error:- Exception <exception-name> is never thrown in the body of the corresponding try statement. But this rule is only applicable for fully checked exceptions. The only possible partially checked exceptions in Java are:- Exception, Throwable.

Q6) We can develop checked exceptions deriving from?

a) java.lang.Exception
b) java.lang.Error
c) java.lang.RuntimeException
d) java.lang.Throwable

View Answer Answer:- a) java.lang.Exception

To develop a checked exception in Java the class should be extended from java.lang.Exception class. In its respective post, we discussed why we should not use Error, RuntimeException, and Throwable class to develop custom exceptions.

Also see:- Develop User-defined Custom exception, Java Custom Exception Example

try with resources

Q7) Find the output of the below program?

import java.io.*;
public class Test {
   public static void main(String[] args) {
      try(FileWriter fw = new FileWriter("output.txt")) {
         fw = new FileWriter("file1.txt");
      } catch(IOException e) {
         System.out.println("A");
      } 
   }
}

a) IOException
b) A
c) Compile-time error
d) None of these

View Answer Answer:- c) Compile-time error

All resources reference variables are implemented as final therefore within the try block we can’t perform re-assignment otherwise we will get a compile time error.

Multi catch block

Q8) Which exception will be raised in the below program?

public class Test {
   public static void main(String[] args) {
      try {
         String s = null;
         System.out.println(s.length());
         System.out.println(9/0);
      } catch(ArithmeticException | NullPointerException e) {
         e.printStackTrace();
      } 
   }
}

a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error

View Answer Answer:- b) NullPointerException

In this program, s.length() is called on a null reference variable. Hence it gives NullPointerException.

Q9) What will be the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.println(9/0);
      } catch(ArithmeticException | ArithmeticException e) {
         e.printStackTrace();
      } 
   }
}

a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error

View Answer Answer:- d) Compile-time error

Same exception can’t be placed in multi-catch statements. It gives a compile time error:- Alternatives in a multi-catch statement cannot be related by subclassing.

Q10) What will be the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         System.out.println(9/0);
      } catch(ArithmeticException | Exception e) {
         e.printStackTrace();
      } 
   }
}

a) ArithmeticException
b) NullPointerException
c) Both ArithmeticException & NullPointerException
d) Compile-time error

View Answer Answer:- d) Compile-time error

In multi-catch blocks there should not be any relation between exception types (either child to parent, parent to child or same type) otherwise we will get compile time errors. Above program gives compile time error:- Alternatives in a multi-catch statement cannot be related by subclassing.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *