Java Exception handling Quiz part 1 contains 10 single choice questions. Java Exception handling quiz questions are designed in such a way that it will help you understand how exception handling works in Java. At the end of the quiz, result will be displayed along with your score and exception handling quiz answers.
There is no time limit to complete the quiz. Click Start Quiz button to start the Java Exception Handling quiz online.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading...
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
Result
0 out of 10 questions were answered correctly.
Time has elapsed
Your score is 0 out of 0, (0)
Average score
Your score
Category Score
Exception Handling0%
Review Answers
1
2
3
4
5
6
7
8
9
10
Answered
Review
Question 1 of 10
1. Question
Will this code compile?
public class Test extends Exception{
public static void main(String[] args){
try{
System.out.println("My Custom Exception test");
throw new Test();
}catch(Exception e){
System.out.println("Test Exception");
}
}
}
Correct answer.
Yes is the correct choice. Since the Test class extends the Exception class we can throw the instance of Test class using throw statement. The code will compile and print My Custom Exception test followed by Test Exception.
Incorrect answer.
Yes is the correct choice. Since the Test class extends the Exception class we can throw the instance of Test class using throw statement. The code will compile and print My Custom Exception test followed by Test Exception.
Question 2 of 10
2. Question
What will happen when you compile and run the following code?
public class Test extends Exception{
private String message;
public Test(String message){
this.message = message;
}
public static void main(String[] args){
int a = 5, b = 3;
try{
if( a % b > 0 )
throw new Test();
}catch(Exception e){
System.out.println("Test Exception");
}
}
}
Correct answer.
Option 4 is the correct choice. Test class extends Exception class and has one argument constructor. However, if any constructor with the argument(s) is provided for the class, default constructor without any arguments is not provided by the compiler, we must write it if we want to use it.
The code creates the Test object using no argument constructor which will give compilation error “The constructor Test() is undefined”.
Incorrect answer.
Option 4 is the correct choice. Test class extends Exception class and has one argument constructor. However, if any constructor with the argument(s) is provided for the class, default constructor without any arguments is not provided by the compiler, we must write it if we want to use it.
The code creates the Test object using no argument constructor which will give compilation error “The constructor Test() is undefined”.
Question 3 of 10
3. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
try{
Test t = new Test();
int d = t.getNum(2, 5);
System.out.println(d);
}catch(Exception e){
System.out.print("Exception 1 ");
}
}
public int getNum(int a, int b){
int c = 0;
try{
c = a * b;
if(c > 10)
throw new String("Cannot be more than 10 ");
}catch(Exception e){
System.out.print("Exception 2 ");
}
return c;
}
}
Correct answer.
Option 4 is the correct choice. Object of Throwable class or its subclasses can be thrown using throw. We cannot throw any other type of objects.
Here the code is trying to throw the String object, hence the code will give compilation error “no exception of type String can be thrown; an exception type must be a subclass of Throwable”.
Incorrect answer.
Option 4 is the correct choice. Object of Throwable class or its subclasses can be thrown using throw. We cannot throw any other type of objects.
Here the code is trying to throw the String object, hence the code will give compilation error “no exception of type String can be thrown; an exception type must be a subclass of Throwable”.
Question 4 of 10
4. Question
What will happen when you compile and run the following code?
public class Test{
String className;
public static void main(String[] args){
try{
Test t = new Test();
if(t.className.equals("Test"))
System.out.print("Test ");
else
System.out.print("Other ");
}catch(Exception e){
System.out.print("Exception ");
}catch(NullPointerException ne){
System.out.print("Null ");
}
}
}
Correct answer.
Option 4 is the correct choice. The catch block catching child class must come first in order before the catch block catching the parent class.
NullPointerException is child class of the Exception class (Exception > RuntimeException > NullPointerException), so the catch block with NullPointerException must come before the catch block with Exception class.
The code will give compilation error “Unreachable catch block for NullPointerException. It is already handled by the catch block for Exception”.
Incorrect answer.
Option 4 is the correct choice. The catch block catching child class must come first in order before the catch block catching the parent class.
NullPointerException is child class of the Exception class (Exception > RuntimeException > NullPointerException), so the catch block with NullPointerException must come before the catch block with Exception class.
The code will give compilation error “Unreachable catch block for NullPointerException. It is already handled by the catch block for Exception”.
Question 5 of 10
5. Question
RuntimeException and its sub-classes can be caught by the catch block.
Correct answer.
True is the correct choice. We can catch RuntimeException and its subclasses like any other exception using catch block.
Incorrect answer.
True is the correct choice. We can catch RuntimeException and its subclasses like any other exception using catch block.
Question 6 of 10
6. Question
What will happen when you compile and run the following code?
public class Test{
public static void main(String[] args){
try{
String[] names = {"Jack", "John", "Jill"};
printNames(names);
}catch(NameIsJohnException e){
System.out.print("NameIsJohnException ");
}
}
private static void printNames(String[] names) throws NameIsJohnException{
for(String name : names){
if(name.equals("John"))
throw new NameIsJohnException("Name cannot be John ");
System.out.print(name + " ");
}
}
}
class NameIsJohnException{
String message;
public NameIsJohnException(){
}
public NameIsJohnException(String message){
this.message = message;
}
public String getMessage(){
return this.message;
}
}
Correct answer.
Option 4 is correct choice. Custom exception class must extend the Exception class or one of its subclasses. Here, NameIsJohnException class does not extend any of that so it cannot be treated as a custom exception.
Only classes of the type Throwable or its subclasses can be thrown using throw so code will give the compilation error “No exception of type NameIsJohnException can be thrown; an exception type must be a subclass of Throwable”.
Incorrect answer.
Option 4 is correct choice. Custom exception class must extend the Exception class or one of its subclasses. Here, NameIsJohnException class does not extend any of that so it cannot be treated as a custom exception.
Only classes of the type Throwable or its subclasses can be thrown using throw so code will give the compilation error “No exception of type NameIsJohnException can be thrown; an exception type must be a subclass of Throwable”.
Question 7 of 10
7. Question
It is not required for the caller method to catch or re-throw the checked exception.
Correct answer.
False is the correct choice. If the method is throwing any of the checked exceptions, the caller must either catch it using try catch block or re-throw it using throws clause. Compilation fails otherwise.
Incorrect answer.
False is the correct choice. If the method is throwing any of the checked exceptions, the caller must either catch it using try catch block or re-throw it using throws clause. Compilation fails otherwise.
Question 8 of 10
8. Question
What will happen when you compile and run the following code?
import java.io.IOException;
public class Test{
public static void main(String[] args){
try{
Test t = new Test();
t.doNothing();
System.out.println("I have done nothing");
}catch(IOException e){
System.out.println("Exception1");
}
}
private void doNothing(){
for(int i = 0 ; i < 10; i++){
}
}
}
Correct answer.
Option 4 is correct choice. IOException is a checked exception, that means it is checked by compiler at the compile time. Exception class and all its subclasses (except for the RuntimeException and its subclasses) are checked exceptions.
Since the checked exceptions are checked at the compile time, compiler can determine whether any given exception can be thrown by the code or not. Here, the code tries to catch an IOException but it is not being thrown by the code in the try block. Hence it will give compilation error “Unreachable catch block for IOException. This exception is never thrown from the try statement body”.
Incorrect answer.
Option 4 is correct choice. IOException is a checked exception, that means it is checked by compiler at the compile time. Exception class and all its subclasses (except for the RuntimeException and its subclasses) are checked exceptions.
Since the checked exceptions are checked at the compile time, compiler can determine whether any given exception can be thrown by the code or not. Here, the code tries to catch an IOException but it is not being thrown by the code in the try block. Hence it will give compilation error “Unreachable catch block for IOException. This exception is never thrown from the try statement body”.
Question 9 of 10
9. Question
What will happen when you compile and run the following code?
public class Test extends Exception{
public Test(){}
public Test(String str){
super(str);
}
int importantData = 5;
public static void main(String[] args){
Test t = new Test();
t.importantMethod();
}
private void importantMethod(){
if( importantData > 5)
throw new Test("Important data is invalid");
else
System.out.println(importantData);
}
}
Correct answer.
Option 4 is the correct choice. Since the Test class extends the Exception class, it becomes a custom checked exception. However, if the code is throwing any checked exception, the caller must either catch it using try catch block or re-throw it using throws clause.
Since the code has not caught the Test exception as well as declared it in the throws clause in the method signature, compiler will give compilation error “Unhandled exception type Test”.
Incorrect answer.
Option 4 is the correct choice. Since the Test class extends the Exception class, it becomes a custom checked exception. However, if the code is throwing any checked exception, the caller must either catch it using try catch block or re-throw it using throws clause.
Since the code has not caught the Test exception as well as declared it in the throws clause in the method signature, compiler will give compilation error “Unhandled exception type Test”.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test {
public static void main(String[] args){
try{
AnotherClass obj = new AnotherClass();
obj.method1();
System.out.println("Main Completed");
}catch (Exception e){}
}
}
class AnotherClass{
public void method2(){
throw new ArrayIndexOutOfBoundsException();
}
public void method1(){
try{
method2();
}catch (NullPointerException ae){
System.out.println("Exception caught");
}finally{
System.out.println("Method 1 ends");
}
}
}
Correct answer.
Option 2 is the correct choice. Program will print “Method 1 ends”.
method1() calls the method2() which throws ArrayIndexOutOfBoundsException. This exception is not caught in the method2() as well as in method1(). So, this exception will be thrown again to the main method and will be caught in main method’s catch block.
However, before that, control goes to the finally block of method1 and prints “Method 1 ends”. Remember, the finally block is ALWAYS executed regardless of the occurrence of the exception.
Incorrect answer.
Option 2 is the correct choice. Program will print “Method 1 ends”.
method1() calls the method2() which throws ArrayIndexOutOfBoundsException. This exception is not caught in the method2() as well as in method1(). So, this exception will be thrown again to the main method and will be caught in main method’s catch block.
However, before that, control goes to the finally block of method1 and prints “Method 1 ends”. Remember, the finally block is ALWAYS executed regardless of the occurrence of the exception.