Java Exception Handling MCQ Questions and Answers 2

Study and learn Interview MCQ Questions and Answers on Java Exception Handling with TRY, CATCH, FINALLY, THROW and THROWS. Try With Resources is also covered. 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 and Java Theory Notes on Try With Resource before reading these objective questions.



1) What is the output of the below Java code with exceptions?
public class ExceptionTest8
{
  public static void main(String[] args) throws ArithmeticException
  {
    System.out.println("I am happy.");
    throw new ArithmeticException();
  }
}
A) I am happy.
B) No output
C) Compiler error
D) None of the above
Answer [=]
A
Explanation:

ArithmeticException is an unchecked exception. So, the compiler does not stop with errors.

2) What is the output of the Java code with exceptions?
public class ExceptionTest9
{
  public static void main(String[] args)
  {
    System.out.println("I am going to forest.");
    throw new ClassNotFoundException();
  }
}
A) I am going to forest.
B) No output
C) Compiler error
D) None of the above
Answer [=]
C
Explanation:

ClassNotFoundException is a checked exception in Java. So, the compiler throws error for not handling it using TRY CATCH FINALLY statements.

Unresolved compilation problem: Unhandled exception type ClassNotFoundException
3) What is the output of the below Java code with exception handling keywords?
public class ExceptionTest10
{
  public static void main(String[] args) throws Exception
  {
    System.out.println("Good Morning Cortana.");
    throw new NoSuchMethodException();
  }
}
A) Good Morning Cortana.
B)
Good Morning Cortana.
Exception in thread "main" java.lang.NoSuchMethodException
	at Project1/package1.ExceptionTest10.main(ExceptionTest10.java:8)
C) Compiler error
D) None of the above
Answer [=]
B
Explanation:

NoSuchMethodException is a checked exception in Java. Here, you are telling the compiler that the MAIN method throws some exception. So, the program compiles and outputs. As the code is throwing a newly created exception, the execution stops here.

4) What are the difference between Checked and Unchecked exceptions in Java?
A) Unchecked exceptions produce less number of compiler errors than the code with Checked exceptions.
B) Most of the unchecked exceptions have RuntimeException as the superclass. Checked exceptions have different classes are superclasses.
C) Unchecked exceptions cause more number of runtime exceptions at the time of execution. As the Checked exceptions are handled well with TRY CATCH blocks, you will get less number of exceptions or errors at the time of execution.
D) All the above
Answer [=]
D
5) Can you catch an object of type Error in Java using TRY-CATCH blocks?
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

Yes. You can catch. But do not catch as these indicate some serious problems.

6) Which is the superclass of Error class in Java?
A) RuntimeException
B) Exception
C) Throwable
D) None of the above
Answer [=]
C
Explanation:

Yes. Throwable is the superclass of both Error and Exception classes in Java.

7) An Error is treated like a _____ type of exception by the compiler.
A) Unchecked
B) Checked
C) -
D) -
Answer [=]
A
Explanation:

An Error is treated like an Unchecked exception in Java. So, the compiler does not show any errors for not-handling the errors properly.



8) Which is the exception handling block that closes the used resources inside of it automatically in Java?
A) TRY
B) CATCH
C) FINALLY
D) TRY with resource
Answer [=]
D
Explanation:

Yes. TRY-with-resource block automatically closes the specified resource automatically.

9) Choose the correct syntax of a try-with-resource in Java.
A)
try(var1 initialized; var2 initialized;)
{

}
B)
try(var1; var2;)
{

}
C)
try(var1 initialized, var2 initialized;)
{

}
D)
try(var1 uninitialized, var2 uninitialized;)
{

}
Answer [=]
A
Explanation:

You should always initialize variables used inside TRY-WITH-RESOURCE block in order to close those automatically.

10) Who closes the resources using code in a TRY-with-resources block in Java?
A) Compiler
B) Runtime
C) Programmer
D) None
Answer [=]
A
Explanation:

The compiler inserts the necessary code inside the FINALLY block (whether explicitly written or not by the programmer) to close the resources. 

11) What is the output of the below Java program with Try With Resource block?
import java.io.*;

public class ExceptionTest12
{
  public static void main(String[] args)
  {
    try(BufferedWriter bw = new BufferedWriter(new FileWriter("abc.txt"));)
    {
      System.out.println("Inside Try With Resource block.");
    }
    catch(Exception e)
    {	
    }
  }
}
A) Inside Try With Resource block.
B) No output
C) Compiler error
D) None of the above
Answer [=]
A
Explanation:

The resource variable "bw" referring BufferedWriter will be closed automatically. Otherwise, the memory will be filled and wasted.

12) Is it possible to write Java code efficiently without using TRY-WITH-RESOURCE blocks?
A) Yes
B) No
C) -
D) -
Answer [=]
A
Explanation:

Yes. Use the normal TRY, CATCH and FINALLY blocks. Keep the code that closes resources inside FINALLY block.

13) A try-with-resource supports only the objects of classes to close automatically that implement ___ Interface in Java.
A) java.lang.Closeable
B) java.lang.AutoCloseable
C) Both A and B
D) None
Answer [=]
C
Explanation:

Yes. You can use the objects of classes that implement java.lang.Closeable or java.lang.AutoCloseable inside TRY-WITH-RESOURCE.

14) What is the output of the below Java program with nested exceptions?
public class ExceptionTest14
{
  public static void main(String[] args)
  {
    try
    {
      int a=10/1;
      try
      {int b=20/1;}
      catch(Exception e1)
      { System.out.println("b=20"); }
    }
    catch(Exception e2)
    {System.out.println("a=10");}
  }
}
A)
a=10
b=20
B)
b=20
a=10
C) Compiler error
D) No output
Answer [=]
C
Explanation:

As no exception is thrown, no output is produced.



15) Choose the right statement about nesting of try-catch-finally in Java?
A)
try
{
  try{}
  catch(Exception e1){}
  finally{}
}
catch(Exception e2)
{}
B)
try
{
  try{}
  catch(Exception e1){}
  finally{}
}
catch(Exception e2)
{
  try{}
  catch(Exception e3){}
  finally{}
}
C)
try
{
}
catch(Exception e2)
{
}
finally
{
  try{}
  catch(Exception e3){}
  finally{}
}
D) All the above
Answer [=]
D
Explanation:

Nesting of exception handling blocks is allowed in any order. So, a try-catch-finally can be kept inside another TRY or CATCH or FINALLY block without errors.

16) The variables initialized inside a TRY-with-resource are treated like ____ variables in Java.
A) static
B) instance
C) final
D) None of the above
Answer [=]
C
Explanation:

A try-with-resource statement treats the variables like FINAL. So, reinitializing is not possible and the compiler throws error.

17) An exception of user-generated-type is treated like a ___ exception.
A) Checked
B) Unchecked
C) -
D) -
Answer [=]
A
Explanation:

You can create a custom exception class simply by subclassing the class Exception. You can throw or catch this user-generated exception like any predefined exception.

18) What is the output of the Java code with custom exceptions?
public class ExceptionTest15
{
  void show(int a) throws MyException
  {
    System.out.println("Hello Custom Exception");
    int b = a/0;
  }
  public static void main(String[] args)
  {
    ExceptionTest15 obj = new ExceptionTest15();
    obj.show(5);
    System.out.println("Bye Custom Exception");
  }
}

class MyException extends Exception
{
  MyException(){ super();}
  MyException(String name){ super(name); }
}
A)
Hello Custom Exception
Bye Custom Exception
B)
Hello Custom Exception
C)
Bye Custom Exception
D) Compiler error
Answer [=]
D
Explanation:

You need to catch or throw the custom exception as it is treated like checked. So, you can not simply call the method throwing a checked exception. If it is non-checked, the compilation completes fine.