ProgrammingModerateProduct-company placement · C++
What is printed?
What is printed?
struct L { L() { cout << "L"; } };
void f() { static L l; cout << "f"; }
int main() { f(); f(); f(); }- A. LfLfLf
- B. fff
- C. LLLfff
- D. Lfff
Correct answer: D. Lfff
A static local object is constructed once, the first time control passes through its declaration, and lives until the program ends.
More OOP with C++ and Java questions
- What is printed? cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int main() { T(); cout << "1"; const T& r = T(); cout <<…
- Which lines fail to compile? cpp struct U { U() { } U(const U&) = delete; }; void take(U u) { } int main() { U a; // L1 U b = a; // L2 U&…
- Which line fails to compile? cpp struct P { int v = 1; int get() { return v; } int cget() const { return v; } }; int main() { const P p{}…
- Which lines fail to compile? cpp struct S { int x = 1; static int y; static int f() { return y; } // L1 static int g() { return x; } // L2…
- Which lines fail to compile? cpp struct B { virtual void f(int) { } virtual void g() const { } void h() { } }; struct D : B { void f(int)…
- Which lines fail to compile? cpp struct B { virtual void f() final { } virtual void g() { } }; struct D : B { void f() { } // L1 void g()…