Oracle Certified Associate Java Se 8 Programmer · Free Practice Question Hard
Question 11
Consider the code:
- public class Test
- {
- public static void main(String[] args)
- {
- System.out.print("A");
- try
- {
- System.out.print("B");
- System.out.print(10/0);
- }
- catch (ArithmeticException e)
- {
- System.out.print("C");
- String s=null;
- System.out.print(s.length());
- }
- finally
- {
- System.out.print("D");
- }
- System.out.print("E");
- }
- }
What is the result?
-
A
Compilation Fails.
-
B
ABCD followed by NullPointerException
-
C
ABC followed by NullPointerException
-
D
ABCD followed by ArithmeticException
Reveal correct answer
Correct answer: B
Explanation
Inside try ArithmeticException raised and hence catch block will be executed. But within the catch block NullPointerException will be raised, which is uncaught. Hence the program will be terminated abnormally. But before abnormal termination, only finally block will be executed. Hence the output is: ABCD followed by NullPointerExceptionDiscussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
You must be logged in to post a comment.
