← 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;
}
- A.99
- B.19
- C.11
- 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 →