ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
// C++17
struct Counter { int n = 0; int operator()() { return ++n; } };
int main() {
Counter c;
c(); c();
Counter d = c;
d();
cout << c.n << d.n << c();
}- A. 333
- B. 232
- C. 233
- D. 223
Correct answer: C. 233
d is a copy of c taken when c.n was 2, so they count independently: d() makes d.n = 3. Since C++17 the operands of << are evaluated left to right, so c.n (2) is read before c() returns 3.
More OOP with C++ and Java questions
- Which line fails to compile? cpp class B { public: void f() { cout << "f"; } void g() { cout << "g"; } }; class D : private B { public…
- What is printed? cpp struct A { void hi() { cout << "A"; } }; struct B { void hi(int) { cout << "B"; } }; struct C : A, B { }; int main()…
- What is printed? cpp struct A { virtual void f() { cout << "A"; } }; struct B { virtual void f() { cout << "B"; } }; struct C : A, B {…
- How many of these statements about C++ are correct? (i) A constructor cannot be declared virtual. (ii) A static member function cannot be…
- What is printed? cpp template <class T> void f(T) { cout << "1"; } template <> void f(int) { cout << "2"; } // explicit specialisation…
- What is printed? cpp template <int N> struct F { static const int v = N F<N - 1>::v; }; template <> struct F<0> { static const int v = 1…