Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What should always follow a successful call to new in C++ to prevent resource leaks?

  1. A call to free()

  2. A call to delete

  3. A call to the destructor

  4. Memory cleaning

The correct answer is: A call to delete

After creating an object using the new keyword in C++, a call to the delete keyword is necessary to free up the memory allocated for that object. This is because using new dynamically allocates memory for an object and it is the responsibility of the programmer to manage and free up that memory when it is no longer needed. Options A, C, and D do not properly address the issue of memory management and could potentially lead to memory leaks. A call to free() is typically used in conjunction with malloc() in C, but is not needed in C++ when using new. A call to the destructor is automatically made when an object is deleted, but does not free up the allocated memory. And "memory cleaning" is a broad term that does not specify the proper way to free up memory in this context. Thus, the only correct and necessary step to prevent resource leaks after a successful call to new