ProgrammingVery hardTCS NQT Digital · Accenture Advanced
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
struct G { const char* n; G(const char* s) : n(s) {} ~G() { cout << n; } };
void f() { G a("a"); G b("b"); throw 1; }
int main() {
try { G c("c"); f(); }
catch (int) { cout << "X"; }
cout << "Y";
}- A. abcXY
- B. cbaXY
- C. bacXY
- D. XbacY
Correct answer: C. bacXY
Stack unwinding destroys f's locals in reverse (b, a), then c in the try block, before the handler prints X; execution continues with Y.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual void f() { cout << "A"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp void f(bool) { cout << "b"; } void f(string) {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp template <class T> void f(T) { cout << "T"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { static int n; static void p() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp int main() { vector<int> a(3, 1), b{3, 1}…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() {…