ProgrammingHardAccenture · Cognizant pattern
Parent declares void read() throws IOException. Which override in a subclass compiles?
- A. void read() throws Throwable
- B. void read() throws Exception
- C. void read() throws FileNotFoundException
- D. void read() throws IOException, SQLException
Correct answer: C. void read() throws FileNotFoundException
An overriding method may throw the same checked exceptions, narrower ones (FileNotFoundException is a subclass of IOException) or none. It may not add broader or new checked exceptions such as Exception or SQLException.
More OOP with C++ and Java questions
- What is printed? java Set<String> t = new TreeSet<>(List.of("apple", "Banana", "cherry")); System.out.print(t);
- What happens when this Java code is compiled? java class P { P(int x) { } } class C extends P { C() { } }
- What is printed? java // static methods of the main class: static void f(int... a) { System.out.print("V"); } static void f(int a, int b)…
- Which member function fails to compile? cpp class C { int a = 0; mutable int hits = 0; public: int get() const { hits++; return a; } void…
- Which C++ operator can be overloaded only as a non-static member function, never as a free function?
- What is printed? java List<Integer> l = Arrays.asList(1, 2, 3); try { l.set(0, 9); System.out.print(l); l.add(4); } catch…