Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 155

Given the code fragments:

and

What is the result?

    Correct Answer: B

    The code will result in a compilation error because in Java, when you override a method in a subclass, the overridden method cannot declare a broader exception than the method in the superclass. In this case, the superclass method in the Video class declares an IOException, while the subclass method in the Game class declares a broader Exception, which is not allowed. Therefore, the code will not compile.

Discussion
ChirajOption: B

Rule: If the superclass method declares an exception, the subclass overridden method can declare the same, subclass exception or no exception but cannot declare parent exception.

jduarteOption: B

Answer B. Tested. Exception Exception is not compatible with throws clause in test.printValues()

Abdullah_RahahleahOption: B

Answer is B, tested class test{ public void printValues() throws IOException { System.out.println("Hi"); } } class test2 extends test{ public void printValues() throws Exception{ super.printValues(); System.out.println("hi2"); } }

steefaandOption: B

B is true because when overriding method you must use that same Exception class or its child.

iSnoverOption: B

Answer is B, tested class test{ public void printValues() throws IOException { System.out.println("Hi"); } } class test2 extends test{ public void printValues() throws Exception{ super.printValues(); System.out.println("hi2"); } }

WilsonKKerllOption: B

Answer is B.

mevltOption: A

The answer is A(tested) public class Video{ public void play() throws IOException{ System.out.print("Vidoe played."); } } class Game extends Video{ public void play() throws IOException{ super.play(); System.out.print("Game played."); } public static void main(String[] args) { try { new Game().play(); }catch (Exception e){ System.out.println(e.getClass()); } } }

mevlt

Nevermind.

Svetleto13Option: B

Answer is compilation error.Both classes shoult throw Exception to compile and get answer A.