ProgrammingVery hardProduct-company placement · Java

Which line fails to compile?

Which line fails to compile?
class P { void run() throws java.io.IOException { } }
class C extends P { @Override void run() { } }
// in main, which declares no throws clause:
new C().run();            // L1
P p = new C(); p.run();   // L2
  1. A. L1 only
  2. B. Both
  3. C. L2 only
  4. D. Neither

Correct answer: C. L2 only

An override may drop checked exceptions. The compiler checks exceptions against the STATIC type: through C, run() throws nothing; through a P reference it may throw IOException, which must be caught or declared.

More OOP with C++ and Java questions

Exams that ask this topic