ProgrammingHardAmazon/Microsoft SDE MCQ · Java placement

Which line fails to compile?

Which line fails to compile?
class P { void run() throws java.io.IOException { } }
class A extends P { void run() { } }                                  // line 1
class B extends P { void run() throws java.io.FileNotFoundException { } } // line 2
class C extends P { void run() throws Exception { } }                  // line 3
class D extends P { void run() throws RuntimeException { } }           // line 4
  1. A. Line 1
  2. B. Line 3
  3. C. Line 4
  4. D. Lines 3 and 4

Correct answer: B. Line 3

An overriding method may throw fewer or narrower checked exceptions (lines 1 and 2) and any unchecked ones (line 4), but not a broader checked exception like Exception (line 3).

More OOP with C++ and Java questions

Exams that ask this topic