ProgrammingVery hardProduct-company MCQ pattern
What is printed?
What is printed?
class R implements AutoCloseable {
String n;
R(String n) { this.n = n; System.out.print("o" + n + " "); }
public void close() { System.out.print("c" + n + " "); }
}
// in main:
try (R a = new R("A"); R b = new R("B")) {
System.out.print("body ");
throw new RuntimeException();
} catch (RuntimeException e) {
System.out.print("catch ");
} finally {
System.out.print("fin");
}- A. oA oB body catch cB cA fin
- B. oA oB body catch fin cB cA
- C. oA oB body cB cA catch fin
- D. oA oB body cA cB catch fin
Correct answer: C. oA oB body cB cA catch fin
Resources open in order and close in reverse order (B then A), and they are closed before the catch block runs. finally runs last.
More OOP with C++ and Java questions
- What is printed? cpp struct B { virtual void f(int x = 1) { cout << "B" << x; } }; struct D : B { void f(int x = 2) override { cout << "D"…
- What is printed? java class T { static String hi() { return "hi"; } } // in main: T t = null; System.out.print(t.hi());
- What is printed? java char x = 'A'; int i = 0; System.out.print(true ? 1 : 2.0); System.out.print(" "); System.out.print(true ? x : i);
- What is printed? cpp struct B { void f(int) { cout << "Bi"; } void f(double) { cout << "Bd"; } }; struct D : B { void f(double) { cout <<…
- How many of these are correct about C++ virtual functions on a typical implementation? (i) Each class with virtual functions has a table…
- A method call such as shape.area() runs different code depending on whether shape refers to a Circle or a Square object at run time. Which…