Java finally Block MCQ-1

Exception Handling MCQ Part – 9 | Java try/catch/finally Block MCQ-1 | Also see:- Exception Handling Interview Questions in JavaMCQ in Java

Q1) Which option can we use in the try block to stop the execution of the “finally” block?

try {
   // option
} finally {
   // code
}

a) return
b) System.exit(0)
c) break
d) continue

View Answer Answer:- b) System.exit(0)

Whenever a program encounters “System.exit(0)” then the program will be terminated immediately without executing the finally block.

Q2) 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);
      } catch(ArithmeticException ae) {
         System.out.print("B");
      } finally {
         System.out.print("C");
      }
      System.out.print("D");
   }
}

a) ACD
b) ABD
c) BCD
d) ABCD

View Answer Answer:- d) ABCD

ArithmeticException raised in the try block and it is matched with the catch block parameter, therefore exception will be handled by the catch block. After that finally block & remaining lines of code will be executed.

Q3) 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);
      } catch(NumberFormatException nfe) {
         System.out.print("B");
      } finally {
         System.out.print("C");
      }
      System.out.print("D");
   }
}

a) ABCD
b) AD
c) AC
d) AC; Exception

View Answer Answer:- d) AC; Exception

The exception is raised in try block but catch block parameter is not matched, hence exception is propagated to the JVM after executing the finally block.

Q4) Find the output of the below program?

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

a) Compile-time error
b) ABC
c) AC
d) BC

View Answer Answer:- b) ABC

Q5) 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);
      } finally {
         System.out.print("B");
      }
      System.out.print("C");
   }
}

a) ABC
b) Compile-time error
c) AB
d) AB; Exception

View Answer Answer:- d) AB; Exception

Q6) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      for (int i=1; i<=5; i++) {
         System.out.print(i);
         try {
            System.out.print("A");
            if(i==2) break;
         }
         finally {
            System.out.print("B");
         }
         System.out.print("C");
      }
      System.out.print("D");
   }
}

a) 1ABC2ABC3ABD
b) 1ABC2ABD
c) 1ABCBD
d) None of these

View Answer Answer:- b) 1ABC2ABD

Q7) Find the output of the below program?

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

a) AB
b) ABC
c) Compile-time error
d) Any exception

View Answer Answer:- c) Compile-time error

After the return statement, only the finally block will get a chance to execute. The last line “System.out.print(“C”);” will never get chance to execute therefore this program gives compile time error:- unreachable statement, System.out.print(“C”);

Q8) 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("A");
         return;
      } finally {
         System.out.print("B");
      }
      System.out.print("C");
      return;
   }
}

a) AB
b) ABC
c) Compile-time error
d) Any exception

View Answer Answer:- c) Compile-time error

Similar to the previous program, again in this program also “System.out.print(“C”);” will never get a chance to execute.

Q9) Find the output of the below program?

public class Test {
   public static void main(String[] args) {
      for (int i=1; i<=5; i++) {
         System.out.print(i);
         try {
            System.out.print("A");
            if(i==2) return;
         }
         finally {
            System.out.print("B");
         }
         System.out.print("C");
      }
      System.out.print("D");
   }
}

a) 1ABC2AB
b) 1ABC2ABD
c) 1ABCBD
d) None of these

View Answer Answer:- a) 1ABC2AB

Once control reaches the “return” statement, only the finally block will be executed one time & program completed.

Q10) 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("A");
         System.exit(0);
      } finally {
         System.out.print("B");
      }
      System.out.print("C");
      return;
   }
}

a) A
b) ABC
c) Compile-time error
d) AC

View Answer Answer:- a) A

Whenever a program encounters “System.exit(0)” then the program will be terminated immediately without executing the finally block, JVM will be shutdown.

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 *