🎙 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 7 of 10

What does this print? (code)

What does this print?

#include <iostream>
#include <vector>
using namespace std;
struct Box { vector<int> v{1, 2, 3}; };
int main() {
    Box a;
    Box b = a;
    b.v[0] = 9;
    cout << a.v[0] << b.v[0];
    return 0;
}
  1. A.99
  2. B.19
  3. C.11
  4. D.Compile error
Answer & explanation

Correct answer: 19

vector copies its elements, so b has its own data; changing b does not change a.

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 →