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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]
5) Can you catch an object of type Error in Java using TRY-CATCH blocks?
A) Yes
B) No
C) -
D) -
Answer [=]
Answer[=]
6) Which is the superclass of Error class in Java?
A) RuntimeException
B) Exception
C) Throwable
D) None of the above
Answer [=]
Answer[=]
7) An Error is treated like a _____ type of exception by the compiler.
A) Unchecked
B) Checked
C) -
D) -
Answer [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
10) Who closes the resources using code in a TRY-with-resources block in Java?
A) Compiler
B) Runtime
C) Programmer
D) None
Answer [=]
Answer[=]
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 [=]
Answer[=]
12) Is it possible to write Java code efficiently without using TRY-WITH-RESOURCE blocks?
A) Yes
B) No
C) -
D) -
Answer [=]
Answer[=]
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 [=]
Answer[=]
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 [=]
Answer[=]


 

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 [=]
Answer[=]
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 [=]
Answer[=]
17) An exception of user-generated-type is treated like a ___ exception.
A) Checked
B) Unchecked
C) -
D) -
Answer [=]
Answer[=]
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 [=]
Answer[=]


Like or Share

Show some care. Like or Subscribe. [FB]...[Youtube]

C MCQ App by ExamTray 

Android APP

Java MCQ App by ExamTray 

Android APP
ALL MCQ App by ExamTray Android APP

Ad

 

Try Some Java Books

Book Price
1. Java - The Complete Reference  Check Price
2. Core Java: An Integrated Approach, Black Book Check Price
3. Java All-in-One for Dummies Check Price
4. OCP Java SE 8: Programmer II Check Price
5. Programming with Java Check Price
6. Head First Java Check Price

We may get some affiliate commission for the above purchases.