C++ Programming Questions and Answers – Resource Management

Show Answer D. Exception
Explanation If there is any mishap in memory or resource management means, the problems that are mentioned above can happen.
Show Answer B. It cannot be accessed by any standard mean
Explanation Resource is said to be leaked when it cannot be accessed by any means of standard mean.
Show Answer A. Segmentation fault
Explanation segmentation fault error can arise when there is a problem with memory.
Show Answer B. 55
Explanation As we had given i value as 2, It will print the 5 for two times. Output: Note: Join free Sanfoundry classes at Telegram or Youtube $ g++ res.cpp $ a.out 55
Show Answer B. TestTest
Explanation We are copying the values from one variable to other, So it is printing is TestTest Output: $ g++ res1.cpp $ a.out TestTest
Show Answer A. 10
Explanation Even Though we passed the value, we didn’t caught to manipulate it, So it is printing as 10. Output: $ g++ res2.cpp $ a.out 10

Q 7. What is resource management in C++?

A. Managing CPU speed
B. Managing memory, file handles, and system resources
C. Managing loops
D. Managing variables only

Show Answer B. Managing memory, file handles, and system resources

Q 8. Which concept is central to C++ resource management?

A. OOP
B. RAII
C. STL
D. Templates

Show Answer B. RAII

Q 9. What does RAII stand for?

A. Resource Allocation is Initialization
B. Resource Allocation is Implementation
C. Runtime Allocation is Initialization
D. Resource Assignment is Initialization

Show Answer A. Resource Allocation is Initialization

Q 10. Which function is used to allocate memory dynamically?

A. malloc()
B. new
C. alloc()
D. create()

Show Answer B. new

Q 11. Which operator is used to deallocate memory?

A. free
B. delete
C. remove
D. destroy

Show Answer B. delete

Q 12. What happens if memory is not deallocated?

A. Faster execution
B. Memory leak
C. Compilation error
D. Syntax error

Show Answer B. Memory leak

Q 13. Which keyword is used for dynamic array allocation?

A. new[]
B. malloc[]
C. array
D. alloc[]

Show Answer A. new[]

Q 14. Which keyword is used to delete a dynamic array?

A. delete
B. delete[]
C. free[]
D. remove[]

Show Answer B. delete[]

Q 15 . What is a smart pointer?

A. A fast pointer
B. Pointer that automatically manages memory
C. Null pointer
D. Static pointer

Show Answer B. Pointer that automatically manages memory

Q 16. Which header defines smart pointers?

A. <memory>
B. <pointer>
C. <smart>
D. <alloc>

Show Answer A.

Q 17. Which smart pointer has exclusive ownership?

A. shared_ptr
B. weak_ptr
C. unique_ptr
D. auto_ptr

Show Answer C. unique_ptr

Q 18. Which smart pointer allows shared ownership?

A. unique_ptr
B. shared_ptr
C. weak_ptr
D. raw pointer

Show Answer B. shared_ptr

Q 19. Which smart pointer does not affect reference count?

A. shared_ptr
B. weak_ptr
C. unique_ptr
D. auto_ptr

Show Answer B. weak_ptr

Q 20. What is a destructor used for?

A. Allocate memory
B. Initialize object
C. Release resources
D. Copy object

Show Answer C. Release resources

Q 21. When is a destructor called?

A. Object creation
B. Object deletion or goes out of scope
C. During compilation
D. During linking

Show Answer B. Object deletion or goes out of scope

Q 22. Which of the following prevents memory leaks?

A. Manual deletion
B. Smart pointers
C. RAII
D. All of the above

Show Answer D. All of the above

Q 23. What is a dangling pointer?

A. Pointer to null
B. Pointer to deleted memory
C. Pointer to constant
D. Pointer to function

Show Answer B. Pointer to deleted memory

Q 24. What is the risk of double deletion?

A. Compile error
B. Undefined behavior
C. Faster program
D. Memory optimization

Show Answer B. Undefined behavior

Q 25. Which of the following is deprecated in modern C++?

A. unique_ptr
B. shared_ptr
C. auto_ptr
D. weak_ptr
Answer: C

Show Answer C. auto_ptr

Q 26. What is the purpose of std::move?

A. Copy object
B. Delete object
C. Transfer ownership of resources
D. Allocate memory

Show Answer C. Transfer ownership of resources
Show Answer B. segmentation fault
Explanation As we are using a pointer value to copy a string, So it will be producing a runtime error. Output: $ g++ res3.cpp $ a.out segmentation fault
Show Answer D. The form of automatic memory management
Explanation The garbage collection attempts to reclaim memory occupied by objects that are no longer in use by the program.
Show Answer D. both new & delete
Explanation new and delete operators are mainly used to allocate and deallocate during runtime.
Show Answer C. smart pointers
Explanation In C++, smart pointers are used to manage memory issues like deallocate memory after use, checking bounds, etc.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page