Java SE 8 Programmer II — Question 137

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 FuelNotAvailException { //line n2 super ride ();
}
}
and the code fragment:
public static void main (String[] args) throws Exception {
Vehicle v = new SolarVehicle ();
v.ride();
}
Which modification enables the code fragment to print Happy Journey!?

Answer options

Correct answer: C

Explanation

The correct answer is C because it allows the ride method in SolarVehicle to declare that it can throw both FuelNotAvailException and Exception, thus ensuring that it can properly call the superclass method. The other options either do not align with the exception handling required or change visibility in a way that does not support the intended functionality.