ProgrammingVery hardWipro Elite · TCS NQT Prime
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 C {
static int n; int id;
C() : id(++n) {}
~C() { cout << id; }
};
int C::n = 0;
int main() {
C a;
{ C b; C c; }
C d;
cout << "|";
}- A. 23|14
- B. 32|14
- C. 23|41
- D. 32|41
Correct answer: D. 32|41
The inner block ends first, destroying c(3) then b(2); after the bar, d(4) and a(1) are destroyed in reverse order of construction.
More OOP with C++ and Java questions
- What is the result? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { void f(int) { cout << "Ai"; }…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual int g(int x = 10) { return…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct V { V(int x) { cout << "V" << x; } V()…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { A() { cout << "A"; } }; struct B {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct I { int v; I& operator++() { ++v…
- What is printed? (also assume <typeinfo>) (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { virtual…