Java Exception Handling TRY CATCH FINALLY MCQ Questions and Answers 1

Study and learn Interview MCQ Questions and Answers on Java Exception Handling with TRY, CATCH and FINALLY blocks. Attend job interviews easily with these Multiple Choice Questions. You can print these Questions in default mode to conduct exams directly. You can download these MCQs in PDF format by Choosing Print Option first and Save as PDF option next using any Web Browser.

Go through Java Theory Notes on Exception Handling before reading these objective questions.



1) What is an Exception in Java?
A) An exception is like a generic error error occurring during the time of execution of program
B) An exception may occur due to memory or hardware issues
C) An exception leads to bad experience for an End user of the software
D) All the above
Answer [=]
D
2) All Java exceptions can be handled gracefully. State TRUE or FALSE.
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

Yes. Simply surround all the code within TRY and CATCH.

3) Are the classes Error and Exception similar in Java?
A) No
B) YES
C) -
D) -
Answer [=]
A
Explanation:

No. An error can not be handled smooth without breaking the flow of execution.

4) Which is the super class in Java that bundles all classes to deal with exceptions and errors?
A) Error
B) Exception
C) Throwable
D) Throw
Answer [=]
C
5) Which are the keywords used to handle exceptions in Java?
A) TRY, CATCH
B) FINALLY
C) THROW, THROWS
D) All the above
Answer [=]
D
Explanation:

The 5 exception handling keywords are try, catch, finally, throw and throws.

6) Where should you keep your Java code for checking against runtime exceptions?
A) Inside TRY block
B) Inside CATCH block
C) inside FINALLY block
D) All the above
Answer [=]
A
Explanation:
try{
  //Java code with expected exceptions
}
7) Where should you keep your Java code that helps to take action on the exceptions raised inside TRY block?
A) One more TRY block
B) CATCH block
C) FINALLY block
D) None of the above
Answer [=]
B
Explanation:

CATCH block follows TRY block. Based on the type of Exception raised or thrown, you can write some fall back code inside CATCH block.



8) What is the output of the below Java code with Exceptions?
public class ExceptionTest1
{
  public static void main(String[] args)
  {
    try
    {
      int a=9, b=0;
      int c = a/b;
      System.out.println("Exception occurred.");
    }
    catch(Exception e)
    {
      System.out.println("Catching an Exception.");
    }
  }
}
A)
Exception occurred.
Catching an Exception.
B)
Exception occurred.
C)
Catching an Exception.
D) No output
Answer [=]
C
Explanation:

The first PRINTF with "Exception occurred." is not printed as the program flow already broke because of exception caused by division by zero.

9) If an exception occurs, all the lines of Java code that is below it stops executing. State TRUE or FALSE. 
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
10) Java exceptions are handled by ____.
A) Java Compiler
B) Java Runtime
C) -
D) -
Answer [=]
B
Explanation:

Yes. Exceptions may or may not occur. It is the Java Runtime that monitors and creates an Exception object that will be handed over to the suitable CATCH block.

11) Can you add more than one CATCH block when handling exceptions in Java?
A) YES
B) NO
C) -
D) -
Answer [=]
A
Explanation:

Yes. You can add as many CATCH blocks as you want with each Block catching one unique Exception type object.

12) A TRY block is the reason for generating a manual exception or an unexpected Runtime exception. State TRUE or FALSE. 
A) TRUE
B) FALSE
C) -
D) -
Answer [=]
A
Explanation:

In practice, exceptions are unexpected bugs which spoil the user experience.

13) What is the output of the below Java code with Exceptions?
public class ExceptionTest2
{
  public static void main(String[] args)
  {
    try
    {
      int ary[] = {10, 20, 30};
      int tempt = ary[4];
    }
    catch(ArrayIndexOutOfBoundsException e1)
    {
      System.out.println(e1.getMessage());
    }
    catch(Exception e2)
    {
      System.out.println("Some exception");
    }
  }
}
A)
Index 4 out of bounds for length 3
B)
Index 4 out of bounds for length
Some exception
C)
Some exception
D) No exception occurs
Answer [=]
A
Explanation:

IndexOutOfBoundsException is raised by TRY block. Observe the order of catching the exceptions. Always catch a Subclass exception before a Superclass exception.

14) Choose the correct way of writing multiple CATCH blocks in Java.
A)
try {
  int num = 10/0;	
}
catch(Exception e1)
{
  System.out.println("EXCEPTION");
}
catch(ArithmeticException e2)
{
  System.out.println("ARITHMETIC EXCEPTION");
}
B)
try {
  int num = 10/0;	
}
catch(ArithmeticException e2)
{
  System.out.println("ARITHMETIC EXCEPTION");
}
catch(Exception e1)
{
  System.out.println("EXCEPTION");
}
C) -
D) -
Answer [=]
B
Explanation:

An exception of Subclass type should be caught before an exception of Superclass type. The ArithmeticException is a subclass of superclass Exception. So, you should first catch the ArithmeticException. Otherwise, all the next CATCH blocks will become unreachable and compiler throws error.



15) Where do you keep the Java code that should be executed at all times even if an exception occurs?
A) inside TRY block
B) inside CATCH block
C) inside FINALLY block
D) All the above
Answer [=]
C
Explanation:

A FINALLY block in Java contains the code that will be executed whether an exception occurs or not. It usually contains the code to close the resources like files and devices. So, this FINALLY block is fail-safe.

16) What is the output of the below java code with exception handling blocks?
public class ExceptionTest4
{
  public static void main(String[] args)
  {
    try
    {
    }
    catch(Exception e)
    {
    }
    finally
    {
      System.out.println("FINALLY block executed");
    }
  }
}
A) No output
B) FINALLY block executed
C) Compiler error
D) None
Answer [=]
B
Explanation:

Even the TRY block contains no code to execute, the finally block will be called.

17) What is the output of the below Java code with exception handling blocks?
public class ExceptionTest5
{
  public static void main(String[] args)
  {
    int ary[] = new int[2];
    ary[10] = 5;
    try
    {
      int number= 2/0;
    }
    catch(Exception e)
    {
      System.out.println("Divide by Zero");
    }
    finally
    {
      System.out.println("Inside FINALLY block");
    }
  }
}
A) Inside FINALLY block
B)
Divide by Zero
Inside FINALLY block
C) Compiler error
D) No output
Answer [=]
C
Explanation:

As the exception (ArrayIndexOutOfBoundsException) occurs even before the TRY block, program execution halts and all the remaining code including FINALLY block will not be executed. Even if a single line of TRY block code is executed, its corresponding FINALLY block will be executed.

18) What is the output of the Java code with FINALLY block and RETURN statement?
public class ExceptionTest6
{
  static void show()
  {
    try
    {
    System.out.println("inside TRY");
    return;
    }
    finally
    {
    System.out.println("inside FINALLY");
    }
  }
  public static void main(String[] args)
  {
    show();
  }
}
A)
inside TRY
B)
inside TRY
inside FINALLY
C)
inside FINALLY
D) Compiler error
Answer [=]
B
Explanation:

Even if a RETURN statement is present at the last line of TRY block, the control is not returned to the calling method. The JVM searches for the suitable FINALLY block and executes it before returning. So, the FINALLY block has higher priority than a RETURN statement.

19) Choose the correct way of writing TRY CATCH FINALLY blocks in Java.
A)
try{}
catch{}
finally{}
B)
try{}
finally{}
C)
try{}
catch{}
D) All the above
Answer [=]
D
20) The FINALLY block should always be accompanied by a ___ block in Java.
A) TRY
B) CATCH
C) FINALLY
D) None
Answer [=]
A
Explanation:

Yes. To write a FINALLY block, there should be a TRY block always.

21) The CATCH block should always be accompanied by a ____ block in Java.
A) TRY
B) CATCH
C) FINALLY
D) None
Answer [=]
A
Explanation:

Yes. There is no value to the CATCH block without a TRY block. The Java compiler throws error if you write CATCH blocks without a TRY.



22) How many extra TRY blocks may be present with an existing TRY block in Java?
A) 0
B) 1
C) 2
D) 3
Answer [=]
A
Explanation:

There can be only one TRY block present in try-catch-finally series of blocks.

23) How many CATCH blocks may be present in try-catch-finally series of blocks in Java?
A) 1
B) 2
C) 3
D) any number of catch blocks
Answer [=]
D
Explanation:

Yes. You can write as many CATCH blocks as you want without duplicating.

24) What is an Unchecked Exception in Java?
A) An exception that need not be handled
B) An exception that may be declared without compiler error
C) Exceptions which will ignored by the compiler for not catching or for throwing
D) All the above
Answer [=]
D
Explanation:

Yes. Unchecked Java exceptions will not be checked by the compiler for mishandling or throwing.

25) What is a Checked Exception in Java?
A) An exception which should be thrown inside the method along with declaring that the method throws it.
B) An exception which should be handled using TRY CATCH statements.
C) Exceptions for which the compiler throws error without completing compilation for not handling properly
D) All the above
Answer [=]
D
Explanation:

Yes. Checked exceptions must be handled properly.

26) Choose which are Java unchecked exceptions below?
A) NullPointerException, ArithmeticException
B) IndexOutOfBoundException, ArrayIndexOutOfBoundsException
C) IllegalArgumentException, StringIndexOutOfBoundsException
D) All the above
Answer [=]
D
Explanation:

These unchecked exceptions are subclasses of RuntimeException class.

27) Choose which are Java checked exceptions below?
A) ClassNotFoundException
B) NoSuchMethodException, NoSuchFieldException
C) InterrruptedException, IllegalAccessException
D) All the above
Answer [=]
D
Explanation:

These checked exceptions must always accompany with TRY-CATCH blocks and THROW-THROWS statements.