Java Exception Handling Quiz Online Test – Part 2

Time limit: 0

Quiz-summary

0 of 10 questions completed

Questions:

1

2

3

4

5

6

7

8

9

10

Information

Java Exception handling Quiz part 2 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

Which of these statements are true?

For each try block there must be at least one corresponding catch block

A try block must be followed by at least one corresponding finally or catch block

A try block must be followed by at least one corresponding finally block

All of the above

Correct answer.

Option 1 is the correct choice. Every try block must have at least one associated catch block. It may also have exactly one finally block after catch block, it is not mandatory though.

Option 2 is incorrect because try must be followed by one or more catch block. Option 3 is also incorrect because try may or may not have the finally block.

Incorrect answer.

Option 1 is the correct choice. Every try block must have at least one associated catch block. It may also have exactly one finally block after catch block, it is not mandatory though.

Option 2 is incorrect because try must be followed by one or more catch block. Option 3 is also incorrect because try may or may not have the finally block.

Option 2 is the correct choice. Finally block is always executed whether or not there is any exception. The code calls method1 from the main method and method1 calls method2.

The method2 method throws NullPointerException which is not surrounded by try catch, so it will be propagated to the caller method method1. The method1 method has a catch block for the Exception class which is the parent class of the NullPointerException, so it will be caught in that catch block and “Method1 ” will be printed.

Method method1 also has a finally block followed by the catch block, so it will be executed as well and “Finally ” will be printed. Since the NullPointerException is already caught in method1, it will not be propagated further to the main method.

Incorrect answer.

Option 2 is the correct choice. Finally block is always executed whether or not there is any exception. The code calls method1 from the main method and method1 calls method2.

The method2 method throws NullPointerException which is not surrounded by try catch, so it will be propagated to the caller method method1. The method1 method has a catch block for the Exception class which is the parent class of the NullPointerException, so it will be caught in that catch block and “Method1 ” will be printed.

Method method1 also has a finally block followed by the catch block, so it will be executed as well and “Finally ” will be printed. Since the NullPointerException is already caught in method1, it will not be propagated further to the main method.

Question 3 of 10

3. Question

What will happen when you compile and run the following code with “java Test one two three” command?

Option 3 is the correct choice. Array index starts from 0 and goes up to (length – 1) index. Here, the for loop goes up to the array length, so the code will throw ArrayIndexOutOfBoundsException for the last iteration of the for loop.

Since there is a catch block for the Exception, ArrayIndexOutOfBoundsException will be caught in the catch block. The code will print one, two and three for the first three iterations respectively and will go to the catch block in the last iteration which will print “Exception “.

The finally block is always executed, so it will print “Finally ” at the end.

Incorrect answer.

Option 3 is the correct choice. Array index starts from 0 and goes up to (length – 1) index. Here, the for loop goes up to the array length, so the code will throw ArrayIndexOutOfBoundsException for the last iteration of the for loop.

Since there is a catch block for the Exception, ArrayIndexOutOfBoundsException will be caught in the catch block. The code will print one, two and three for the first three iterations respectively and will go to the catch block in the last iteration which will print “Exception “.

The finally block is always executed, so it will print “Finally ” at the end.

Option 3 is the correct choice. The RuntimeException class is a subclass of the Exception class and can be caught like any other exceptions.

Since the code has catch block for the Exception class, RuntimeException will be caught there and print “Exception “. There is also a finally block which will be executed after it and will print “Finally “.

Incorrect answer.

Option 3 is the correct choice. The RuntimeException class is a subclass of the Exception class and can be caught like any other exceptions.

Since the code has catch block for the Exception class, RuntimeException will be caught there and print “Exception “. There is also a finally block which will be executed after it and will print “Finally “.

Option 3 is the the correct choice. The AssertionError class is a subclass of the Error class, which is subclass of the Throwable class. Since the AssertionError class is not a direct or indirect child of Exception class, it cannot be caught using catch block for the Exception class.

Since the code does not have any appropriate catch block for the AssertionError, code will throw it and stacktrace will be printed. Finally block is always executed regardless of exception condition, so “Finally ” will also be printed.

Incorrect answer.

Option 3 is the the correct choice. The AssertionError class is a subclass of the Error class, which is subclass of the Throwable class. Since the AssertionError class is not a direct or indirect child of Exception class, it cannot be caught using catch block for the Exception class.

Since the code does not have any appropriate catch block for the AssertionError, code will throw it and stacktrace will be printed. Finally block is always executed regardless of exception condition, so “Finally ” will also be printed.

Yes is the correct choice. Just like any other exceptions, RuntimeException can be caught using the catch block. The code will compile without any errors and print “Exception ” followed by “Finally ” when executed.

Incorrect answer.

Yes is the correct choice. Just like any other exceptions, RuntimeException can be caught using the catch block. The code will compile without any errors and print “Exception ” followed by “Finally ” when executed.

Option 3 is the correct choice. There are no compilation errors in the code. Since variable i is declared as a class level variable, it will be initialized with default value 0.

Since the variable i is 0, method1 will return 0. However, the control goes to the finally block first and will print “Finally ” before going to the main method and printing return value of the method1 which is 0.

Incorrect answer.

Option 3 is the correct choice. There are no compilation errors in the code. Since variable i is declared as a class level variable, it will be initialized with default value 0.

Since the variable i is 0, method1 will return 0. However, the control goes to the finally block first and will print “Finally ” before going to the main method and printing return value of the method1 which is 0.

Option 3 is the correct choice. The main method calls method1 which in turn calls method2. Variable i counts various checkpoints in the code and it is initialized with default value 0 since it is a class level variable.

The method2 method throws an exception which is caught in the method itself which makes count 1. Since it also has a finally block, control will go there which will make the count 2. Since the exception was caught in the method2 itself, it will not be propagated back to the method1.

The try block of method1 will be executed normally in the absence of any exceptions which makes count 3. However, method1 also has a finally block so the control will go there which will make the count 4.

Same steps will be executed for the main method as well. The finally block of the main method will make the count 5.

Incorrect answer.

Option 3 is the correct choice. The main method calls method1 which in turn calls method2. Variable i counts various checkpoints in the code and it is initialized with default value 0 since it is a class level variable.

The method2 method throws an exception which is caught in the method itself which makes count 1. Since it also has a finally block, control will go there which will make the count 2. Since the exception was caught in the method2 itself, it will not be propagated back to the method1.

The try block of method1 will be executed normally in the absence of any exceptions which makes count 3. However, method1 also has a finally block so the control will go there which will make the count 4.

Same steps will be executed for the main method as well. The finally block of the main method will make the count 5.

Question 9 of 10

9. Question

Which class will compile without any errors out of the below given two classes?

Option 2 is the correct choice. Since IOException is a checked exception, compiler checks whether the code in the try block throws it or not.

Since the code in the try block does not throw IOException, we cannot catch it. Compiler will give error “Unreachable catch block for IOException. This exception is never thrown from the try statement body”.

Same is not the case with the Exception class. We can catch the Exception even if the code in the try block may not throw it. So the Test2 class will compile without any errors.

Incorrect answer.

Option 2 is the correct choice. Since IOException is a checked exception, compiler checks whether the code in the try block throws it or not.

Since the code in the try block does not throw IOException, we cannot catch it. Compiler will give error “Unreachable catch block for IOException. This exception is never thrown from the try statement body”.

Same is not the case with the Exception class. We can catch the Exception even if the code in the try block may not throw it. So the Test2 class will compile without any errors.

Question 10 of 10

10. Question

The class Test defines a method myMethod (float f) as given below. Which of the following declarations of myMethod (float f) are valid in the class TestChild?

Option 2 and 3 are the correct choices. Overriding methods in a subclass can throw the same exceptions, child classes of the exceptions thrown by the method in parent class, or throw no exceptions at all. It cannot throw different exceptions or parent classes of the exceptions thrown in the parent class method.

Option 1 is not valid because it is throwing Exception which is the parent class of the IOException. Option 2 is throwing the same exception as the parent method so it is valid method declaration. Option 3 is also valid because it is not throwing any exceptions at all. Since the option 2 and 3 are valid, option 4 becomes invalid.

Incorrect answer.

Option 2 and 3 are the correct choices. Overriding methods in a subclass can throw the same exceptions, child classes of the exceptions thrown by the method in parent class, or throw no exceptions at all. It cannot throw different exceptions or parent classes of the exceptions thrown in the parent class method.

Option 1 is not valid because it is throwing Exception which is the parent class of the IOException. Option 2 is throwing the same exception as the parent method so it is valid method declaration. Option 3 is also valid because it is not throwing any exceptions at all. Since the option 2 and 3 are valid, option 4 becomes invalid.