ProgrammingVery hardProduct-company placement · Java
What happens?
What happens?
class W {
{ if (true) throw new java.io.IOException("io"); }
W() throws java.io.IOException { }
}- A. It compiles; new W() throws IOException
- B. Compile-time error: an initializer cannot throw a checked exception
- C. It compiles; the exception is wrapped in ExceptionInInitializerError
- D. Compile-time error: W() must catch the IOException
Correct answer: A. It compiles; new W() throws IOException
An instance initializer may throw a checked exception if EVERY constructor of the class declares it, because the initializer runs as part of each constructor.
More OOP with C++ and Java questions
- Which line fails to compile? cpp // C++17 struct S { int v; S(int x) : v(x) { } bool operator==(const S& o) const { return v == o.v; } }…
- What is printed? cpp struct B { void run() { step(); } virtual void step() { cout << "B"; } }; struct D : B { void step() override { cout…
- What is printed? cpp class A { int x = 3; friend int peek(const A& a) { return a.x; } }; int main() { A a; cout << peek(a); }
- Which function returns a reference that is SAFE to use after the call returns?
- Why does a C++ copy assignment operator conventionally return T& (that is, this)?
- What is printed? cpp // C++17 struct V { int d[2] = {1, 2}; int& operator[](int i) { cout << "N"; return d[i]; } int operator[](int i)…