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- A. Line 1
- B. Line 3
- C. Line 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
- What is printed? java String s = "java"; s.toUpperCase(); s.concat("x"); StringBuilder sb = new StringBuilder("java"); sb.reverse()…
- What is printed? java interface I { default String n() { return "I"; } } class P { public String n() { return "P"; } } class C extends P…
- What is printed (C++17)? cpp struct T { T() { } T(const T&) { cout << "C"; } T(T&&) { cout << "M"; } }; int main() { const T a; T b =…
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } }; struct W { T t; }; int main() { W a; W b = std::move(a); }
- What is printed? cpp struct A { virtual void f() = 0; }; void A::f() { cout << "A"; } struct B : A { void f() override { A::f(); cout <<…
- What is printed? cpp struct B { virtual ~B() { } }; struct D1 : B { }; struct D2 : B { }; int main() { B p = new D1; cout <<…