Java finally Block MCQ-2

Exception Handling MCQ Part – 10 | Java try/catch/finally Block MCQ-2 | 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) {
      System.out.println(m1());
   }

   public static int m1() {
      try {
         System.out.print("A");
         return 1;
      } finally {
         System.out.print("B");
         return 2;
      }
   }
}

a) A1
b) AB1
c) AB2
d) A2

View Answer Answer:- c) AB2

The value returned from try block or from catch block is replaced by finally block returning value.

Q2) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      System.out.println(m1());
   }

   public static int m1() {
      try {
         System.out.print(10/0);
      } finally {
         System.out.print("B");
         return 2;
      }
   }
}

a) ArithmeticException
b) B2
c) 2
d) B; ArithmeticException

View Answer Answer:- b) B2

If we place the return statement in the finally block, then the exception raised in the try block is never propagated to the calling method, because the returned value replaces the exception object in JDH (Java default handler).

Q3) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      System.out.println(m1());
   }

   public static int m1() {
      try {
         System.out.print(10/0);
      } finally {
         System.out.print("A");
      }
      return 20;
   }
}

a) ArithmeticException
b) A2
c) 2
d) A; ArithmeticException

View Answer Answer:- d) A; ArithmeticException

This time the return statement is outside of the finally block, Exception is raised before returning the value.

Q4) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      m1();
   }

   public static void m1() {
      try {
         System.out.print(10/0);
      } finally {
         System.out.print("A");
         return;
      }
   }
}

a) ArithmeticException
b) Compile-time error
c) A
d) A; ArithmeticException

View Answer Answer:- c) A

Exception raised in try block is never propagated to the calling method even though it’s return type is void.

Q5) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      m1();
      System.out.println("A");
   }

   public static void m1() {
      try {
         System.out.print(10/0);
      } finally {
         int num = Integer.parseInt("two");
      }
   }
}

a) ArithmeticException
b) NumberFormatException
c) A
d) None of these

View Answer Answer:- b) NumberFormatException

The value returned from try block or the exception throwing from try catch block will be replaced with finally block throwing exception, and then finally block exception will be propagated to the main() method.

Q6) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      System.out.print(m1());;
   }
   public static int m1() {
      try {
         System.out.print("A");
         return 10;
      } finally {
         System.out.print("B");
         return 20;
      }
      return 30;
  }
}

a) AB10
b) AB20
c) AB30
d) Compile-time error

View Answer Answer:- d) Compile-time error

Since the finally block contains a return statement therefore the remaining line of m1() method will never get a chance of execution. Hence it gives compile time error:- unreachable statement, return 30;

Q7) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      System.out.print(m1());;
   }

   public static int m1() {
      try {
         System.out.print("A");
         return 10;
      } catch(Exception e) {
         System.out.println("Z");
      } finally {
         System.out.print("B");
         if(true) return 20;
      }
      System.out.print("C");
      return 30;
  }
}

a) AB10
b) AB20
c) ABC30
d) Compile-time error or Any Exception

View Answer Answer:- b) AB20

The finally block return statement will replace all existing return values. The statements after the finally block will not be executed. Since the finally block contains a return statement inside the if block therefore the compiler doesn’t give compile time error because if block executes only when condition becomes true. The compiler only takes care of syntaxes, it doesn’t check whether the if block condition is true or not.

Q8) Find the output of the below program?

public class Test {

   public static void main(String[] args) {
      System.out.print(m1());;
   }

   public static int m1() {
      try {
         System.out.print("A");
         return 10;
      } catch(Exception e) {
         System.out.println("Z");
      } finally {
         System.out.print("B");
         if(false) return 20;
      }
      System.out.print("C");
      return 30;
  }
}

a) AB10
b) AB20
c) ABC30
d) Compile-time error or Any Exception

View Answer Answer:- a) AB10

The condition of if block inside finally block is false therefore existing returned value 10 will be returned to the main() method.

Q9) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      try {
         int a = 10;
      } catch(Exception e) {
         System.out.println(a);
      }
   }
}

a) 10
b) 0
c) Compile-time error
d) Exception

View Answer Answer:- c) Compile-time error

Variables declared in one block are accessible only inside that block. In this program, a variable is declared in try block therefore it is not accessible in catch block.

Q10) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      throw new ArithmeticException("divided by zero");
   }
}

a) ArithmeticException: divided by zero
b) ArithmeticException: / by zero
c) Compile-time error
d) None of these

View Answer Answer:- a) ArithmeticException: divided by zero

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 *