ProgrammingModerateProduct-company MCQ pattern
Why must a C++ copy constructor take its parameter by reference (A(const A &other)) rather than by value?
- A. By value would make the object const
- B. References are faster but by value would also work
- C. Passing by value would itself call the copy constructor, recursing without end
- D. The compiler cannot copy private members by value
Correct answer: C. Passing by value would itself call the copy constructor, recursing without end
To pass an A by value the compiler must copy it, which means calling the copy constructor, which needs another copy, and so on forever. The language therefore rejects A(A other).
More OOP with C++ and Java questions
- What is printed? java System.out.print("placement".substring(2, 5) + "placement".indexOf('e'));
- What is the time complexity of get(i) on a Java ArrayList and on a LinkedList?
- In C++, what does new do that malloc does not?
- What is printed? java class A { void show() { System.out.print("A"); } } class B extends A { void show() { super.show()…
- What is printed? cpp class Base { public: void show() { cout << "B1"; } virtual void vshow() { cout << "B2"; } }; class Derived : public…
- In C++, Base has a non-virtual destructor and Derived : public Base owns a heap buffer freed in ~Derived. What happens on: Base p = new…