In which condition finally block is not executed Mcq?

This section of our 1000+ Java MCQs focuses on keyword finally and built in exceptions of Java Programming Language.

1. Which of these clause will be executed even if no exceptions are found?
a) throws
b) finally
c) throw
d) catch
View Answer

Answer: b
Explanation: finally keyword is used to define a set of instructions that will be executed irrespective of the exception found or not.

2. A single try block must be followed by which of these?
a) finally
b) catch
c) finally & catch
d) none of the mentioned
View Answer

Answer: c
Explanation: try block can be followed by any of finally or catch block, try block checks for exceptions and work is performed by finally and catch block as per the exception.

3. Which of these exceptions handles the divide by zero error?
a) ArithmeticException
b) MathException
c) IllegalAccessException
d) IllegarException
View Answer

Answer: a
Explanation: None.

Note: Join free Sanfoundry classes at Telegram or Youtube

advertisement

advertisement

4. Which of these exceptions will occur if we try to access the index of an array beyond its length?
a) ArithmeticException
b) ArrayException
c) ArrayIndexException
d) ArrayIndexOutOfBoundsException
View Answer

Answer: d
Explanation: ArrayIndexOutOfBoundsException is a built in exception that is caused when we try to access an index location which is beyond the length of an array.

5. What will be the output of the following Java code?

Take Java Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.             }
  11.     {
    0
  12.             {
  13.     {
    2
  14.             }
  15.     {
    4
  16.     {
    5

Note : Execution command line : $ java exception_handling
a) 0
b) 1
c) Compilation Error
d) Runtime Error
View Answer

Answer: b
Explanation: None.
Output:

advertisement

    {
6

6. What will be the output of the following Java code?

advertisement

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.         public static void main(String args[]) 
    3
  8.             }
  9.         public static void main(String args[]) 
    5
  10.             {
  11.         public static void main(String args[]) 
    7
  12.             }
  13.     {
    4
  14.     {
    5

a) A
b) B
c) Compilation Error
d) Runtime Error
View Answer

Answer: d
Explanation: Try block is throwing NullPointerException but the catch block is used to counter Arithmetic Exception. Hence NullPointerException occurs since no catch is there which can handle it, runtime error occurs.
Output:

        {
1

7. What will be the output of the following Java code?

  1.         {
    2
  2.         {
  3.         {
    4
  4.             {
  5.         {
    6
  6.         {
    7
  7.         {
    8
  8.         {
    9
  9.             try 
    0
  10.             try 
    1
  11.             try 
    2
  12.             try 
    3
  13.             try 
    4
  14.             try 
    5
  15.             try 
    6
  16.             try 
    7
  17.             try 
    8
  18.             try 
    9
  19.             {
    0
  20.             try 
    1
  21.             {
    2
  22.             try 
    9
  23.             {
    4
  24.             {
    5
  25.         {
    7
  26.             {
    7
  27.             {
    4
  28.             }
  29.     {
    4

a) A
b) B
c) AB
d) BA
View Answer

Answer: a
Explanation: The inner try block does not have a catch which can tackle ArrayIndexOutOfBoundException hence finally is executed which prints ‘A’ the outer try block does have catch for ArrayIndexOutOfBoundException exception but no such exception occurs in it hence its catch is never executed and only ‘A’ is printed.
Output:

                int a = args.length;
1

8. What will be the output of the following Java code?

  1.     class exception_handling 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             try 
  6.             {
  7.                 int a = args.length;
  8.                 int b = 10 / a;
  9.                 System.out.print(a);
  10.         {
    6
  11.         {
    7
  12.                 int b = 10 / a;
    3
  13.                 int b = 10 / a;
    4
  14.                 int b = 10 / a;
    5
  15.                 int b = 10 / a;
    6
  16.                 int b = 10 / a;
    7
  17.                 int b = 10 / a;
    8
  18.                 int b = 10 / a;
    9
  19.             {
    4
  20.                 System.out.print(a);
    1
  21.         {
    7
  22.                 System.out.print(a);
    3
  23.             {
    4
  24.     {
    0
  25.             {
  26.                 System.out.print(a);
    7
  27.             }
  28.     {
    4
  29.     {
    5

Note: Execution command line: $ java exception_handling one two
a) TypeA
b) TypeB
c) Compilation Error
d) Runtime Error
View Answer

Answer: c
Explanation: try without catch or finally
Output:

            }
1

Sanfoundry Global Education & Learning Series – Java Programming Language.

To practice all areas of Java language, here is complete set of 1000+ Multiple Choice Questions and Answers.

In which condition finally block is not executed in C#?

finally. Sometimes the finally block does not get executed if there's no exception thrown in try block. If there's no exception in try block, the code in finally block is not always get executed.

When finally is not executed?

A finally block will not execute due to other conditions like when JVM runs out of memory when our java process is killed forcefully from task manager or console when our machine shuts down due to power failure and deadlock condition in our try block.

In which condition will the finally block?

When the System. exit() method is called in the try block before the execution of finally block, finally block will not be executed. Let's see the different example programs based on the above condition.

Which one of the following is true about the finally block Mcq?

Explanation: finally block is always executed after try block, no matter exception is found or not.