Java OCA OCP Practice Question 3083

Question

Given the following class and interface definitions:

class Exception1 extends Exception {}

interface Printable {
    public abstract void print() throws Exception1;
}

interface Walkable {
    public void walk();
}

abstract class Shape {
    public void print() { System.out.print("cannot print ");  }  // LINE A
}

class Robot extends Shape implements Printable, Walkable {    // LINE B
    public void walk() { System.out.print("walk "); }
}

Select the correct option for this code segment:

Robot pingu = new Robot();
pingu.walk();
pingu.print();
  • a) Compiler error in line with comment LINe a because print() does not declare to throw Exception1
  • b) Compiler error in line with comment LINe B because print() is not defined and hence need to declare it abstract
  • c) It crashes after throwing the exception Exception1
  • d) When executed, the program prints "walk cannot print"


d)

Note

In order to override a method, it is not necessary for the overridden method to specify an exception.

however, if the exception is specified, then the specified exception must be the same or a subclass of the specified exception in the method defined in the super class (or interface).




PreviousNext

Related