ProgrammingVery hardProduct-company placement · Java
What happens when this method is compiled (Java 7 or later)?
What happens when this method is compiled (Java 7 or later)?
static void f() throws java.io.IOException {
try {
if (Math.random() < 2) throw new java.io.IOException();
} catch (Exception e) {
throw e;
}
}- A. Compile-time error: unreported exception Exception
- B. Compile-time error: e must be declared final
- C. It compiles: e is rethrown precisely as the IOException the try block can throw
- D. It compiles only if f() declares throws Exception
Correct answer: C. It compiles: e is rethrown precisely as the IOException the try block can throw
With precise rethrow, rethrowing an effectively final catch parameter is checked against the exceptions the try block can actually throw (IOException plus unchecked ones), not against the declared catch type.
More OOP with C++ and Java questions
- What is printed? java try { System.out.print("T"); System.exit(0); } finally { System.out.print("F"); }
- What is printed? java class N { public Object clone() throws CloneNotSupportedException { return super.clone(); } } // in main: try { new…
- What is printed? java System.out.print((List.of(1, 2).hashCode() == List.of(2, 1).hashCode()) + " " + (Set.of(1, 2).hashCode() ==…
- What is printed? java // static methods of the main class: static void f(Object o) { System.out.print("O"); } static void f(Object[] a) {…
- What is printed? java class Range implements Iterable<Integer> { public Iterator<Integer> iterator() { return List.of(1, 2, 3).iterator()…
- Which line fails to compile (C++17)? cpp struct A { A() = default; A(const A&) = delete; A(A&&) = default; }; A make() { A a; return a; }…