Unit 5: Dynamic Memory Management and Polymorphism - Subjective Questions

CAP455 — Object Oriented Programming Using C++ • Practice Questions with Detailed Answers

20 questions

1

Define dynamic memory allocation. Why is it important in C++ programming?

2

Explain how the new and delete operators are used for dynamically allocating and deallocating a single object in C++.

3

Describe dynamic allocation and deallocation of arrays using new[] and delete[]. What happens if the wrong form of delete is used?

4

What is a memory leak? Explain its causes, consequences, and methods of prevention in C++.

5

Explain dynamic memory allocation failure. Compare the exception-based and std::nothrow methods of handling allocation failure.

6

Why should a base class destructor be declared virtual? Explain with an appropriate example.

7

Distinguish between compile-time polymorphism and runtime polymorphism in C++.

8

Explain function overloading and operator overloading as forms of compile-time polymorphism.

9

What is a virtual function? Describe how virtual functions implement runtime polymorphism.

10

Define a pure virtual function. How does it differ from an ordinary virtual function?

11

Compare abstract classes and concrete classes in C++. Give suitable examples.

12

Explain early binding and late binding. Under what conditions does each occur in C++?

13

What is a dynamic constructor? Explain how a constructor can allocate memory dynamically and how the corresponding class should release it.

14

Design a class that uses a dynamic constructor and follows the Rule of Three. Explain why each special member function is required.

15

What is a self-referential class? Explain its role in implementing dynamic data structures.

16

Using a self-referential class, describe how nodes are dynamically inserted into and deleted from the beginning of a singly linked list.

17

Develop a C++ example that demonstrates runtime polymorphism using an abstract base class, derived concrete classes, dynamic allocation, and a virtual destructor.

18

What is object slicing? How is it related to polymorphism, and how can it be avoided?

19

Compare C++ dynamic allocation using new and delete with C-style allocation using malloc() and free().

20

Explain how RAII and smart pointers improve dynamic memory management in polymorphic C++ programs.