ProgrammingHardProduct-company placement · C++
What is printed?
What is printed?
struct W { W() { cout << "W"; } };
int main() {
W w1;
W w2();
W w3{};
cout << "!";
}- A. WW!
- B. WWW!
- C. W!
- D. Compile-time error at W w2();
Correct answer: A. WW!
W w2(); is the 'most vexing parse': it declares a function named w2 returning W, so no object is created. w1 and w3 (brace value-initialisation) both call the constructor.
More OOP with C++ and Java questions
- Which line fails to compile (C++11 or later)? cpp int a(3.7); // L1 int b = 3.7; // L2 int c{3.7}; // L3
- What is printed? cpp struct A { int v = 1; }; struct B : A { }; struct C : A { }; struct D : B, C { }; int main() { D d; cout << d.v; }
- Which line fails to compile? cpp class B { protected: int x = 1; }; class D : public B { public: int f(D& d) { return d.x; } // L1 int…
- What is printed? cpp struct B { int v = 7; }; class D1 : B { }; struct D2 : B { }; int main() { D1 a; D2 b; cout << b.v << a.v; }
- What is printed? cpp template <typename T> void f(T) { cout << "T"; } void f(int) { cout << "I"; } int main() { f(1); f(1.0); f('a')…
- What is printed? cpp template <typename T> struct Is { static const int v = 0; }; template <> struct Is<int> { static const int v = 1; }…