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 T {
T() { cout << "T"; }
~T() { cout << "~"; }
};
void g() {
static T t;
cout << "g";
}
int main() {
g();
g();
cout << "m";
}- A. Tggm~
- B. Tg~g~m
- C. ggTm~
- D. TgTgm~~
Correct answer: A. Tggm~
A function-local static is constructed once, the first time control passes its declaration, and destroyed only at program exit after main returns, so the destructor's '~' comes last.
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct R { const char n; R(const char s) : n(s) { cout << "+" <<…
- 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()…