ProgrammingHardMicrosoft/Google SDE MCQ · GATE CS
What is printed?
What is printed?
struct T {
T() { }
T(const T&) { cout << "C"; }
};
struct W { T t; };
int main() {
W a;
W b = std::move(a);
}- A. C
- B. Nothing
- C. M
- D. Compile error: W has no move constructor
Correct answer: A. C
W gets an implicit move constructor that moves t. T has a user-declared copy constructor, so it has no implicit move constructor, and the rvalue binds to T(const T&): C.
More OOP with C++ and Java questions
- What is printed? cpp struct A { virtual void f() = 0; }; void A::f() { cout << "A"; } struct B : A { void f() override { A::f(); cout <<…
- What is printed? cpp struct B { virtual ~B() { } }; struct D1 : B { }; struct D2 : B { }; int main() { B p = new D1; cout <<…
- What is printed? cpp struct B { }; // no virtual functions struct D : B { }; struct VB { virtual ~VB() { } }; struct VD : VB { }; int…
- What is printed (C++17)? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); auto g = f; f(); cout << f() << g() <<…
- What is printed? cpp struct B { }; struct D : B { }; void f(B&) { cout << "B"; } void f(D&) { cout << "D"; } int main() { D d; B& b = d…
- What is printed? cpp struct T { int id; T(int i) : id(i) { cout << "+" << id; } ~T() { cout << "-" << id; } }; T g(1); void f() { static T…