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

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!?

    Correct Answer: A

    The original code contains no compilation errors and the method signatures in the subclasses are sufficient for the code to run and print 'Happy Journey!'. However, to explicitly state that method ride() is public in the parent class Vehicle, replacing line n1 with 'public void ride() throws FuelNotAvailException' ensures consistency and clarity in method visibility for subclasses. This modification does not change the functionality but aligns with good coding practices.

Discussion
Svetleto13Option: A

Code doesnt need change,but it works with answer A as well.

JME_CHGOption: A

no change needed in code... tested

steefaandOption: A

It compiles already but you can select A since it will compile too.

duydnOption: B

Code is compilation successfully, no need to change, if change, change the modifier of default -> protected of n1

duydn

or public