ProgrammingHardProduct-company placement · C++
Which lines fail to compile?
Which lines fail to compile?
struct U { U() { } U(const U&) = delete; };
void take(U u) { }
int main() {
U a; // L1
U b = a; // L2
U& r = a; // L3
take(a); // L4
}- A. L2 only
- B. L2 and L4
- C. L4 only
- D. L2, L3 and L4
Correct answer: B. L2 and L4
With the copy constructor deleted, copying an lvalue is ill-formed: both copy-initialising b and passing a by value need it. Binding a reference copies nothing.
More OOP with C++ and Java questions
- 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()…
- What is printed? cpp struct B { virtual ~B() { } }; struct D : B { }; struct E : B { }; int main() { B p = new D; cout <<…
- What is printed? cpp struct A { }; struct B : A { }; struct P { virtual ~P() { } }; struct Q : P { }; int main() { B b; Q q; A& ra = b; P&…