Skip to main content

Smart Pointers

1).What is a smart pointer in C++?

1) A pointer that manages dynamic memory
2) A pointer that always points to a valid memory
3) A pointer that can be dereferenced
4) None of the above

2).Which type of smart pointer automatically deallocates memory when it goes out of scope?

1) std::unique_ptr
2) std::shared_ptr
3) std::weak_ptr
4) None of the above

3).What is the purpose of `std::shared_ptr`?

1) To manage shared ownership of a dynamically allocated object
2) To avoid memory leaks
3) To allocate memory for variables
4) None of the above

4).Which of the following smart pointers allows multiple pointers to share ownership of the same object?

1) std::unique_ptr
2) std::shared_ptr
3) std::weak_ptr
4) None of the above

5).What is the purpose of `std::weak_ptr`?

1) To observe an object without affecting its reference count
2) To provide exclusive ownership
3) To manage multiple references
4) None of the above

6).How does `std::unique_ptr` ensure there is only one owner of the object?

1) By deleting the object when it goes out of scope
2) By preventing copying of the pointer
3) By allowing shared ownership
4) None of the above

7).What happens if a `std::shared_ptr` is copied?

1) It creates a new pointer pointing to the same object
2) It deletes the object
3) It throws an exception
4) None of the above

8).Which of the following is true about `std::weak_ptr`?

1) It does not contribute to reference counting
2) It manages the memory of the object
3) It is used for unique ownership
4) None of the above

9).Can you assign a `std::unique_ptr` to a `std::shared_ptr`?

1) Yes
2) No
3) It depends on the compiler
4) None of the above

10).Which of the following smart pointers should be used when you want shared ownership and the possibility of multiple owners?

1) std::unique_ptr
2) std::shared_ptr
3) std::weak_ptr
4) None of the above