Sample questions
Question 1Easy
Which smart pointer was removed from the C++ standard library in C++17?
std::weak_ptrstd::unique_ptrstd::shared_ptrstd::auto_ptr
Answer: D. std::auto_ptr
Explanation
auto_ptr had confusing copy semantics (copying transferred ownership). It was deprecated in C++11 and removed in C++17 in favour of unique_ptr.
Step-by-step solution
- Auto_ptr was deprecated in C++11 because copying it silently transferred ownership.
- C++17 removed it from the standard library, and unique_ptr replaced it.
- So the removed smart pointer is std::auto_ptr: option D.
Why the other options are wrong
- A. Weak_ptr is still in the standard library; it was not removed.
- B. Unique_ptr is the replacement for auto_ptr, and it was not removed.
- C. Shared_ptr is still in the standard library; it was not removed.
Tip: Auto_ptr is gone since C++17; unique_ptr is its safe replacement.
Question 2Easy
What happens when this program is compiled?
#include <iostream>
#include <memory>
using namespace std;
int main() {
unique_ptr<int> a = make_unique<int>(5);
unique_ptr<int> b = a;
cout << *b;
return 0;
}
- It prints 5
- Compilation error: unique_ptr cannot be copied
- It prints 5 and a becomes null
- Runtime error: double delete
Answer: B. Compilation error: unique_ptr cannot be copied
Explanation
unique_ptr models exclusive ownership, so its copy constructor is deleted. Ownership can only be transferred with std::move.
Step-by-step solution
- Unique_ptr models exclusive ownership, so its copy constructor is deleted.
- A unique_ptr to int b = a tries to copy a, which the compiler rejects.
- So the program fails to compile: option B.
Why the other options are wrong
- A. The copy is not allowed, so the program cannot compile and print 5.
- C. The copy does not compile, so no output or null state happens.
- D. There is no runtime double delete, because the program does not compile.
Tip: Move a unique_ptr with std::move to transfer ownership; a plain copy will not compile.
Question 3Easy
A Document object should have exactly one owner at any time, but ownership may be handed over to another part of the program. Which smart pointer fits best?
std::weak_ptrstd::unique_ptr- A raw pointer with a comment
std::shared_ptr
Answer: B. std::unique_ptr
Explanation
unique_ptr expresses exclusive ownership with zero overhead and supports transferring ownership with std::move.
Step-by-step solution
- A Document has exactly one owner at a time, which is exclusive ownership.
- Unique_ptr expresses exclusive ownership, and std::move can hand it to another part of the program.
- So the best fit is unique_ptr: option B.
Why the other options are wrong
- A. Weak_ptr does not own the object, so it cannot be the single owner.
- C. A raw pointer does not express ownership and can leak or dangle.
- D. Shared_ptr allows many owners, which the question does not need.
Tip: Use unique_ptr for single ownership, shared_ptr for shared ownership, and weak_ptr for non-owning observation.
Question 4Medium
What is the output?
#include <iostream>
#include <memory>
using namespace std;
#include <vector>
#include <utility>
int main() {
vector<unique_ptr<int>> v;
v.push_back(make_unique<int>(4));
auto p = make_unique<int>(9);
v.push_back(std::move(p));
cout << *v[0] + *v[1] << " " << (p == nullptr);
return 0;
}
- 4 1
- Compilation error
- 13 1
- 13 0
Answer: C. 13 1
Explanation
unique_ptr is move-only, so it can be stored in a vector by moving it in. After the move p is null.
Step-by-step solution
- V holds two unique_ptrs: 4 from the first push_back, and 9 moved in from p.
- *v[0] + *v[1] is 4 + 9 = 13.
- After std::move, p is null, so p == nullptr is 1.
- The output is 13 1: option C.
Why the other options are wrong
- A. 4 is only v[0]; the sum of v[0] and v[1] is 13.
- B. The code compiles, because a unique_ptr can be moved into a vector.
- D. P is null after the move, so the last value is 1, not 0.
Tip: Unique_ptr is move-only, so a named pointer must be moved with std::move before push_back.
Question 5Medium
Two objects hold shared_ptr members that point to each other, and all outside shared_ptrs to them are destroyed. What happens, and what is the usual fix?
- Neither object is destroyed because each keeps the other's count above zero; make one of the links a weak_ptr
- Both are destroyed normally because shared_ptr detects cycles
- The program crashes with a double delete; use raw pointers instead
- Compilation fails because shared_ptr cannot form cycles
Answer: A. Neither object is destroyed because each keeps the other's count above zero; make one of the links a weak_ptr
Explanation
Reference counting cannot reclaim cycles, so the objects leak. A weak_ptr for the back-reference (e.g. child to parent) breaks the cycle.
Step-by-step solution
- Each object holds a shared_ptr to the other, so each count stays at least 1 after the outside pointers go.
- Neither count reaches zero, so neither destructor runs. The objects leak.
- Making one back link a weak_ptr breaks the cycle, so both can be freed: option A.
Why the other options are wrong
- B. Shared_ptr does not detect cycles; reference counts alone cannot free them.
- C. No double delete happens; the objects are simply never freed.
- D. The code compiles, because shared_ptr members can form a cycle.
Tip: Use weak_ptr for back references such as child to parent to break shared_ptr cycles.
Question 6Medium
What is the output?
#include <iostream>
#include <memory>
using namespace std;
struct A {
~A() { cout << "~A "; }
};
int main() {
unique_ptr<A> p(new A);
p.reset();
cout << "after ";
cout << (p ? "set" : "null");
return 0;
}
- after set ~A
- ~A after null
- after null ~A
- ~A after set
Answer: B. ~A after null
Explanation
reset() with no argument deletes the owned object immediately and leaves the unique_ptr empty.
Step-by-step solution
- P owns a new A object.
- p.reset() with no argument deletes the A at once, so ~A is printed first.
- P is now null, so the ternary prints null, after the word after.
- The output is ~A after null: option B.
Why the other options are wrong
- A. The destructor runs at reset, before after is printed, not at the end.
- C. The destructor runs at reset, so ~A comes before the word after.
- D. P is null after reset, so the text is null, not set.
Tip: reset() deletes the object now; release() gives up the pointer without deleting it.
Question 7Medium
What is the output?
#include <iostream>
#include <memory>
using namespace std;
int main() {
shared_ptr<int> p(new int(3), [](int* q) {
cout << "del" << *q;
delete q;
});
auto p2 = p;
p.reset();
cout << "!";
p2.reset();
cout << "?";
return 0;
}
- del3!?
- !del3?
- !?del3
- del3!del3?
Answer: B. !del3?
Explanation
The custom deleter runs only when the last owner releases the object: resetting p leaves p2 as an owner, so the deleter runs at p2.reset().
Step-by-step solution
- P owns the int with a custom deleter. p2 is a copy, so both share ownership.
- p.reset() drops only p. p2 still owns the object, so no deleter runs and! is printed.
- p2.reset() releases the last owner, so the deleter prints del3 and deletes the int.
- Then? is printed. The output is !del3?: option B.
Why the other options are wrong
- A. Del3 is printed only when the last owner releases, which is after the exclamation mark.
- C. ? comes after del3, because the deleter runs at p2.reset() before the question mark.
- D. The deleter runs only once, at the last reset, not at both resets.
Tip: A shared_ptr deleter runs once, when the last owner is released.
Question 8Medium
What is the output?
#include <iostream>
#include <memory>
using namespace std;
int main() {
auto a = make_shared<int>(5);
auto b = a;
{
auto c = a;
cout << a.use_count() << " ";
}
cout << a.use_count();
return 0;
}
- 1 1
- 3 3
- 2 2
- 3 2
Answer: D. 3 2
Explanation
Each shared_ptr copy increments the shared reference count; when c goes out of scope the count drops back to 2.
Step-by-step solution
- a makes count 1; b = a makes 2.
- Inside the block c = a makes 3: first print 3.
- After the block c is destroyed: count 2.
- Output: 3 2.
Why the other options are wrong
- A. Copies increase the count.
- B. c is destroyed at block end, so the count drops.
- C. Inside the block three owners exist.
Tip: Count follows the copies in scope.
Question 9Hard
What is the output?
#include <iostream>
#include <memory>
using namespace std;
int main() {
weak_ptr<int> w;
{
auto s = make_shared<int>(42);
w = s;
if (auto p = w.lock()) cout << *p << " ";
cout << w.expired() << " ";
}
cout << w.expired();
if (!w.lock()) cout << " gone";
return 0;
}
- 42 0 1
- 42 1 1 gone
- 42 0 1 gone
- 42 0 0
Answer: C. 42 0 1 gone
Explanation
A weak_ptr observes without owning.
While s is alive lock() returns a valid shared_ptr; once the last owner is destroyed the weak_ptr is expired and lock() returns an empty pointer.
Step-by-step solution
- While s is alive, lock() returns a valid shared_ptr, so 42 is printed followed by a space.
- w.expired() is false while s is alive, so 0 is printed followed by a space.
- When the block ends, s is destroyed and w is expired, so 1 is printed. Then lock() returns empty, so gone is printed.
- The output is 42 0 1 gone: option C.
Why the other options are wrong
- A. It leaves out gone, which is printed because lock() is empty after the block.
- B. While s is alive expired is 0, not 1, so the second value is wrong.
- D. The final expired check is 1 after the block, not 0, and gone is printed.
Tip: A weak_ptr becomes expired once every shared_ptr owner is gone, and lock() then returns null.
Question 10Hard
What is the output?
#include <iostream>
#include <memory>
using namespace std;
#include <utility>
struct A {
~A() { cout << "~A "; }
};
void consume(unique_ptr<A> p) {
cout << "in ";
}
int main() {
auto p = make_unique<A>();
consume(std::move(p));
cout << "back " << (p == nullptr);
return 0;
}
- in back 0 ~A
- in back 1 ~A
- in ~A back 0
- in ~A back 1
Answer: D. in ~A back 1
Explanation
Passing a unique_ptr by value transfers ownership to the parameter; the object is destroyed when the parameter goes out of scope at the end of consume.
Step-by-step solution
- Std::move transfers p into the parameter of consume, so p becomes null.
- Consume prints in. When consume ends, the parameter is destroyed, printing ~A.
- Back in main, back is printed, and p == nullptr is 1.
- The output is in ~A back 1: option D.
Why the other options are wrong
- A. The object is destroyed when consume ends, so ~A comes before back; and p is null, so the last value is 1.
- B. The destructor runs at the end of consume, before back is printed, not after it.
- C. P was moved, so p == nullptr is 1, not 0.
Tip: Passing a unique_ptr by value transfers ownership, and the object dies when the parameter goes out of scope.
Practise all 19 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →