Oracle Certified Professional Java Se 11 Developer · Free Practice Question Hard
Question 19
Question ID: UKOCP19357
Given code of Test.java file:
- package com.udayankhattry.ocp;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- abstract class Super {
- public abstract void m1() throws IOException;
- }
- class Sub extends Super {
- @Override
- public void m1() throws IOException {
- throw new FileNotFoundException();
- }
- }
- public class Test {
- public static void main(String[] args) {
- Super s = new Sub();
- try {
- s.m1();
- } catch (IOException e) {
- System.out.print("A");
- } catch(FileNotFoundException e) {
- System.out.print("B");
- } finally {
- System.out.print("C");
- }
- }
- }
What will be the result of compiling and executing Test class?
- A AC
- B BC
- C class Sub causes compilation error
- D class Test causes compilation error
Reveal correct answer
Correct answer: D
Explanation
UKOCP19357:
Method m1() of Sub class correctly overrides the method m1() of Super class, so there is no compilation error in Sub class.
FileNotFoundException extends IOException and hence catch block of FileNotFoundException should appear before the catch block of IOException.
Therefore, class Test causes compilation error.
Discussion
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.
