ProgrammingHardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
struct P { const int id; };
int main() {
P a{1}, b{2};
P c = a; // L1
a = b; // L2
}- A. L1 only
- B. L2 only
- C. Both
- D. Neither
Correct answer: B. L2 only
A const data member can be copied into a new object, so the copy constructor is fine, but it cannot be reassigned, so the implicit copy assignment operator is defined as deleted.
More OOP with C++ and Java questions
- How many of these statements about implicitly generated special members in C++11 and later are correct? (i) If a class declares a copy…
- What happens when this is compiled? cpp struct A { A(int) { } }; int main() { A a; }
- What is printed? cpp struct B { virtual void f() { cout << "B"; } }; struct D : B { void f() override { cout << "D"; } }; int main() {…
- What is printed? cpp template <class T> struct Base { void hi() { staticcast<T>(this)->impl(); } void impl() { cout << "B"; } }; struct X…
- What happens? cpp template <class T> T mx(T a, T b) { return a > b ? a : b; } int main() { cout << mx(3, 7); // L1 cout << mx(1, 2.5); //…
- What is printed? cpp struct A { A() { cout << "A"; } ~A() { cout << "a"; } }; struct B { B() { cout << "B"; } ~B() { cout << "b"; } }…