Java OCA OCP Practice Question 3092

Question

Consider the following program:

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 "); }
}

public class Main {
        public static void main(String []args) {
               Robot pingu = new Robot();
               pingu.walk();/*  w  w w.  j a  va  2s.  c om*/
               pingu.print();
        }
}

Which one of the following options correctly describes the behavior of this program?

  • 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