🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Copy Constructor and Deep Copy: all 10 questions

Quiz: Copy Constructor and Deep Copy · Question 8 of 10

In copy-and-swap, why does operator= take its parameter by value?

In copy-and-swap, why does operator= take its parameter by value?

  1. A.To avoid any copy at all
  2. B.The parameter becomes a fresh copy; swapping with it gives us the new data and lets it free our old data
  3. C.Because operator= cannot take references
  4. D.To make self-assignment a compile error
Answer & explanation

Correct answer: The parameter becomes a fresh copy; swapping with it gives us the new data and lets it free our old data

The copy is made by the copy constructor, and the old data is freed when the parameter is destroyed.

Can you get the rest right without peeking?

Take the timed Quiz: Copy Constructor and Deep Copy quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →