Java Exception handling Quiz part 3 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
What all method1 method declarations are valid for TestChild class at the line number 11?
class TestException extends Exception{}
class TestChild1Exception extends TestException{}
class TestChild2Exception extends TestChild1Exception{}
public class Test{
public void method1() throws TestChild1Exception{
}
}
class TestChild extends Test{
//method declaration here
}
Correct answer.
Option 1, 2 and 3 are the correct choices. Overriding methods in a subclass can either throw the same exceptions as the method in the parent class, child exceptions of the exceptions thrown by the method in the parent class, or throw no exceptions at all. It cannot throw different exceptions or parent class of the exceptions thrown in the parent class method.
Option 1 is correct because it is not throwing any exception at all. Option 2 is correct because it is throwing same exception as those of the parent method. Option 3 is also correct because it is throwing child exception (i.e. TestChild2Exception) of the exception thrown by the method in the parent class (i.e. TestChild1Exception).
Option 4 is incorrect as it is throwing Exception which is indirect parent class of the TestChild1Exception class. Option 5 is also incorrect for the same reason, TestException being the parent class of the TestChild1Exception class.
Incorrect answer.
Option 1, 2 and 3 are the correct choices. Overriding methods in a subclass can either throw the same exceptions as the method in the parent class, child exceptions of the exceptions thrown by the method in the parent class, or throw no exceptions at all. It cannot throw different exceptions or parent class of the exceptions thrown in the parent class method.
Option 1 is correct because it is not throwing any exception at all. Option 2 is correct because it is throwing same exception as those of the parent method. Option 3 is also correct because it is throwing child exception (i.e. TestChild2Exception) of the exception thrown by the method in the parent class (i.e. TestChild1Exception).
Option 4 is incorrect as it is throwing Exception which is indirect parent class of the TestChild1Exception class. Option 5 is also incorrect for the same reason, TestException being the parent class of the TestChild1Exception class.
Question 2 of 10
2. Question
Will this class compile?
public class Test{
public static void main(String[] args){
Test t = new Test();
t.method1();
}
public void method1(){
method2();
}
public void method2(){
try{
throw new NullPointerException();
}catch(NullPointerException e){
throw e;
}
}
}
Correct answer.
Yes is the correct choice. We can re-throw the same exception in the catch block. Remember, if you are re-throwing the checked exception, the caller method must either surround it with the try catch block or declare it using the throws clause.
Since we are re-throwing the unchecked exception (NullPointerException), method1 does not have to surround method2() call using the try catch block.
Incorrect answer.
Yes is the correct choice. We can re-throw the same exception in the catch block. Remember, if you are re-throwing the checked exception, the caller method must either surround it with the try catch block or declare it using the throws clause.
Since we are re-throwing the unchecked exception (NullPointerException), method1 does not have to surround method2() call using the try catch block.
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){
Test t = new Test();
t.method1();
System.out.print("5");
}
public void method1(){
try{
method2();
System.out.print("1");
}catch(NullPointerException ne){
System.out.println("2");
}finally{
System.out.println("3");
}
System.out.print("4");
}
public void method2(){
throw new ArrayIndexOutOfBoundsException();
}
}
Correct answer.
Option 5 is the correct choice. The code calls method1 from the main method which calls method2. The method method2 is throwing ArrayIndexOutOfBoundsException which is not handled in the method2, so it will be propagated back to the caller i.e. method1.
The method method1 has a try catch block but it does not have any matching catch block for the ArrayIndexOutOfBoundsException exception. Hence the ArrayIndexOutOfBoundsException is propagated back to the caller which is the main method. But before that, the control goes to the finally block of the method1, which will print 3.
The main method does not have try catch, so the ArrayIndexOutOfBoundsException exception will cause the program to halt and stack trace will be printed. So output of the code will be “3” followed by the exception stack trace.
Incorrect answer.
Option 5 is the correct choice. The code calls method1 from the main method which calls method2. The method method2 is throwing ArrayIndexOutOfBoundsException which is not handled in the method2, so it will be propagated back to the caller i.e. method1.
The method method1 has a try catch block but it does not have any matching catch block for the ArrayIndexOutOfBoundsException exception. Hence the ArrayIndexOutOfBoundsException is propagated back to the caller which is the main method. But before that, the control goes to the finally block of the method1, which will print 3.
The main method does not have try catch, so the ArrayIndexOutOfBoundsException exception will cause the program to halt and stack trace will be printed. So output of the code will be “3” followed by the exception stack trace.
Question 4 of 10
4. Question
Error (and its subclasses) cannot be caught, only Exception (and its subclasses) can be caught using the catch block.
Correct answer.
False is the correct choice. Error and its subclasses can be caught using the try catch blocks.
Incorrect answer.
False is the correct choice. Error and its subclasses can be caught using the try catch blocks.
Question 5 of 10
5. Question
Will this code compile?
public class Test{
public static void main(String[] args){
try{
Test t = new Test();
t.method1();
}catch(NullPointerException ne){
}
}
public void method1() throws Exception{
}
}
Correct answer.
No is the correct choice. The Exception class and all its subclasses (except for the RuntimeException and its subclasses) are checked exceptions. That means that they must be either handled by the try catch block or declared using throws clause in the method signature.
The method method1 declares Exception in the throws clause but the calling method (i.e. main method) does not have matching catch block for the Exception class. Neither the main method declares the Exception using throws clause. Thus compilation will fail with an error “Unhandled exception type Exception”.
Incorrect answer.
No is the correct choice. The Exception class and all its subclasses (except for the RuntimeException and its subclasses) are checked exceptions. That means that they must be either handled by the try catch block or declared using throws clause in the method signature.
The method method1 declares Exception in the throws clause but the calling method (i.e. main method) does not have matching catch block for the Exception class. Neither the main method declares the Exception using throws clause. Thus compilation will fail with an error “Unhandled exception type Exception”.
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{
System.out.print("1");
try{
System.out.print("a");
throw new Exception();
}catch(Exception e){
System.out.print("b");
throw e;
}finally{
System.out.print("c");
}
}catch(Exception e){
System.out.print("3");
}finally{
System.out.print("4");
}
}
}
Correct answer.
Option 2 is the correct choice. The code has two nested try catch finally blocks.
Here is the sequence of the events
1) The System.out.print statement is executed of the outer try block and prints “1”
2) The control goes to the inner try block and prints “a”.
3) Next statement throws new Exception object which will get caught in the catch block. So “b” will be printed.
4) The same exception is re-thrown in the next statement using throw clause.
5) The control goes to the finally block of the inner try and prints “c”
6) Since the exception was re-thrown in the inner catch block, the control goes to the catch block of the outer try catch and prints “3”
7) The finally block of the outer try catch block is executed and it will print “4”
Incorrect answer.
Option 2 is the correct choice. The code has two nested try catch finally blocks.
Here is the sequence of the events
1) The System.out.print statement is executed of the outer try block and prints “1”
2) The control goes to the inner try block and prints “a”.
3) Next statement throws new Exception object which will get caught in the catch block. So “b” will be printed.
4) The same exception is re-thrown in the next statement using throw clause.
5) The control goes to the finally block of the inner try and prints “c”
6) Since the exception was re-thrown in the inner catch block, the control goes to the catch block of the outer try catch and prints “3”
7) The finally block of the outer try catch block is executed and it will print “4”
Question 7 of 10
7. Question
What will you write at the line number 16, provided that method name is method1?
class TestException extends Exception{}
public class Test{
private int i = 0;
public static void main(String[] args){
try{
Test t = new Test();
t.method1();
}catch(Exception e){
}
}
//method declaration here
{
if( i == 0 )
throw new TestException();
}
}
Correct answer.
Option 2 and 5 are the correct choices. Code creates a checked custom exception TestException by extending the Exception class. The same exception is thrown inside the method method1.
Since the TestException is a checked exception, it must be either handled by the try catch block or declared in the throws clause of the method signature. Since there is no try catch block in the method1, we must use throws clause to throw unhandled exception to the caller. Option 1 is invalid because it does not declare the TestException using throws.
Option 2 is the correct syntax of throws. Option 3 and 4 and not using throws keywords and hence they are incorrect. Option 5 is throwing Exception instead of TestException. Since the Exception class is a parent class of the TestException class, it is valid.
Incorrect answer.
Option 2 and 5 are the correct choices. Code creates a checked custom exception TestException by extending the Exception class. The same exception is thrown inside the method method1.
Since the TestException is a checked exception, it must be either handled by the try catch block or declared in the throws clause of the method signature. Since there is no try catch block in the method1, we must use throws clause to throw unhandled exception to the caller. Option 1 is invalid because it does not declare the TestException using throws.
Option 2 is the correct syntax of throws. Option 3 and 4 and not using throws keywords and hence they are incorrect. Option 5 is throwing Exception instead of TestException. Since the Exception class is a parent class of the TestException class, it is valid.
Question 8 of 10
8. Question
Throwable class is the parent class of both Error and Exception classes.
Correct answer.
True is the correct choice. Throwable class is a super class of the Exception and Error classes. The Exception class and its subclasses represent any application event which disrupts the normal flow of the execution, for example, IOException. Error class and its subclasses represent abnormal conditions which should never occur during the program execution, for example, OutOfMemoryError error.
Incorrect answer.
True is the correct choice. Throwable class is a super class of the Exception and Error classes. The Exception class and its subclasses represent any application event which disrupts the normal flow of the execution, for example, IOException. Error class and its subclasses represent abnormal conditions which should never occur during the program execution, for example, OutOfMemoryError error.
Question 9 of 10
9. Question
All subclasses of the RuntimeException class and Error class are unchecked exceptions.
Correct answer.
True is the correct choice. The RuntimeException class, the Error class and all of their subclasses are unchecked exceptions, that means you are not required to catch them or declare them using throws clause.
Incorrect answer.
True is the correct choice. The RuntimeException class, the Error class and all of their subclasses are unchecked exceptions, that means you are not required to catch them or declare them using throws clause.
Question 10 of 10
10. Question
What will happen when you compile and run the following code?
public class Test{
private int i = 0;
public static void main(String[] args){
try{
System.out.print("1");
System.exit(1);
System.out.print("2");
}catch(Exception e){
System.out.print("3");
}finally{
System.out.print("4");
}
System.out.print("5");
}
}
Correct answer.
Option 2 is the correct choice. Code will print 1 when run. This is one of the rare situations where the finally block is not executed. The exit method of the System class causes the virtual machine to terminate. Since the virtual machine which runs the code itself is terminated, rest of the code will not be executed.
Apart from the call to the System.exit method, here are some of the other conditions which prevents the finally block to execute.
– JVM Crash
– Power Failure
– Fetal errors due to the corrupt disk
– Fetal errors due to the corrupt memory
Incorrect answer.
Option 2 is the correct choice. Code will print 1 when run. This is one of the rare situations where the finally block is not executed. The exit method of the System class causes the virtual machine to terminate. Since the virtual machine which runs the code itself is terminated, rest of the code will not be executed.
Apart from the call to the System.exit method, here are some of the other conditions which prevents the finally block to execute.
– JVM Crash
– Power Failure
– Fetal errors due to the corrupt disk
– Fetal errors due to the corrupt memory