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

Given:

class FuelNotAvailException extends Exception { }

class Vehicle {

void ride() throws FuelNotAvailException { //line n1

System.out.println("Happy Journey!");

}

}

class SolarVehicle extends Vehicle {

public void ride () throws Exception { //line n2

super ride ();

}

}

and the code fragment:

public static void main (String[] args) throws FuelNotAvailException, Exception {

Vehicle v = new SolarVehicle ();

v.ride();

}

Which modification enables the code fragment to print Happy Journey!?

    Correct Answer: C

    To enable the code fragment to print 'Happy Journey!', the overriding method in the subclass SolarVehicle must match or be more constrained than the parent class method's exception specification. Since the parent class method ride() throws FuelNotAvailException, the subclass method in SolarVehicle can declare to throw a more specific exception, a broader exception, or even no exception at all. Hence, replacing line n2 with 'void ride() throws Exception { super.ride(); }' violates the constraint that it cannot declare to throw a different checked exception which is broader than FuelNotAvailException specified in the superclass. The correct modification would be to replace line n2 with a method signature that aligns with the overriding rules, ensuring compliance.

Discussion
Tarik2190Option: A

According to overriding rules, if super class / interface method declares to throw a checked exception, then overriding method of sub class / implementer class has following options: 1. May not declare to throw any checked exception. 2. May declare to throw the same checked exception thrown by super class / interface method. 3. May declare to throw the sub class of the exception thrown by super class / interface method. 4. Cannot declare to throw the super class of the exception thrown by super class / interface method. 5. Cannot declare to throw unrelated checked exception. 6. May declare to throw any RuntimeException or Error.

Tarik2190Option: B

Answer is B, tested

asdfjhfgjuaDCVOption: B

B is the Correct answer

steefaandOption: B

B is correct since base method can't declare narrower Exception type than overridden method.

WilsonKKerllOption: B

Answer is B.