ProgrammingHardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
struct R {
const char* n;
R(const char* s) : n(s) { cout << "+" << n; }
~R() { cout << "-" << n; }
};
void f() {
R a("a");
throw 1;
R b("b");
}
int main() {
try {
R c("c");
f();
} catch (int) {
cout << "X";
}
cout << "Y";
}- A. +c+a-a-cXY
- B. +c+a-aX-cY
- C. +c+a-a-cYX
- D. +c+a-c-aXY
Correct answer: A. +c+a-a-cXY
Unwinding destroys fully constructed automatic objects in reverse order of construction (a, then c) before the handler runs; b is never constructed because the throw precedes it.
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp template <class T> void f(T) { cout << "T"; } template <class T>…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct V { V() { cout << "V"; } }; struct A : virtual V { A() {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct C { int v = 0; C& operator++() { ++v; return this; } C…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() { super()…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(i) + " " + l.equals(127L) + " " + (l == 127) + " " +…