ProgrammingVery hardGATE CS · TCS NQT Prime · Product Company SDE
Class A's constructor declares `throws IOException`. Class B extends A. What must every constructor of B do about this checked exception?
- A. Checked exceptions can never legally be thrown from a constructor
- B. Either also declare `throws IOException` (or a broader checked exception) in its own signature, or avoid it entirely; it cannot be caught around the implicit super() call in the same constructor
- C. B cannot be compiled at all while A's constructor throws a checked exception
- D. Nothing; subclass constructors are automatically exempt from a superclass constructor's checked exceptions
Correct answer: B. Either also declare `throws IOException` (or a broader checked exception) in its own signature, or avoid it entirely; it cannot be caught around the implicit super() call in the same constructor
Because the super(...) call must be the first statement of B's constructor, it cannot be wrapped in a try/catch there, so B's constructor must itself declare that checked exception (or a supertype of it).
More OOP with C++ and Java questions
- What access modifier and finality does a field declared directly inside a Java interface implicitly have?
- java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo =…
- What does the 'Rule of Five' in modern C++ say?
- java Function<Integer,Integer> sq = x -> x x; Function<Integer,Integer> addOne = x -> x + 1; Function<Integer,Integer> combo =…
- java class Base { int v = 1; } class Derived extends Base { int v = 2; } // main: Base b = new Derived(); System.out.print(b.v); What is…
- In C++, a base class constructor calls a function that is overridden in a derived class. Which version actually runs while the base…