Unit 5: Dynamic Memory Management and Polymorphism - Practice Quiz

CAP455 — Object Oriented Programming Using C++ 60 Questions
0 Correct 0 Wrong 60 Left
0/60

1 Why is dynamic memory allocation useful in C++?

Importance of dynamic memory allocation Easy
A. It allocates memory during program execution
B. It makes all data permanent
C. It prevents functions from being called
D. It removes the need for variables

2 Dynamic memory is usually allocated from which memory area?

Importance of dynamic memory allocation Easy
A. The register
B. The constant pool
C. The stack
D. The heap

3 Which operator is used to allocate memory dynamically in C++?

Dynamic memory allocation using new and delete operators Easy
A. new
B. create
C. alloc
D. make

4 Which operator releases memory allocated for a single object using new?

Dynamic memory allocation using new and delete operators Easy
A. clear
B. free
C. remove
D. delete

5 Which operator should be used to release memory allocated with new[]?

Dynamic memory allocation using new and delete operators Easy
A. delete
B. delete[]
C. free[]
D. remove[]

6 What is a memory leak?

Memory leak and allocation failures Easy
A. Memory being reused correctly
B. Memory becoming read-only
C. Allocated memory becoming unreachable
D. Memory being stored on the stack

7 What can happen if a program repeatedly leaks memory?

Memory leak and allocation failures Easy
A. The compiler removes all objects
B. The program always becomes faster
C. The stack size becomes unlimited
D. The program may run out of memory

8 What exception does standard C++ commonly throw when new cannot allocate memory?

Memory leak and allocation failures Easy
A. std::bad_alloc
B. std::alloc_fail
C. std::new_error
D. std::memory_error

9 Why is a destructor often declared virtual in a polymorphic base class?

Virtual destructor Easy
A. To increase object size only
B. To allow correct derived cleanup
C. To disable function overloading
D. To prevent object construction

10 Which keyword is used to declare a virtual destructor?

Virtual destructor Easy
A. override-only
B. runtime
C. virtual
D. dynamic

11 Which type of polymorphism is resolved by the compiler?

Compile time polymorphism vs run time polymorphism Easy
A. Compile-time polymorphism
B. Run-time polymorphism
C. Object-time polymorphism
D. Link-time inheritance

12 Which feature is commonly associated with compile-time polymorphism?

Compile time polymorphism vs run time polymorphism Easy
A. Virtual dispatch
B. Abstract object creation
C. Function overloading
D. Dynamic casting only

13 Which keyword declares a virtual function in C++?

Virtual functions Easy
A. polymorphic
B. late
C. virtual
D. dynamic

14 What happens when a virtual function is called through a base-class pointer?

Virtual functions Easy
A. The function cannot be called
B. The base version always runs
C. The derived override may run
D. The compiler deletes the object

15 How is a pure virtual function commonly declared in C++?

Pure virtual functions Easy
A. Using pure 0
B. Using == 0
C. Using = 0
D. Using virtual 0

16 Does a pure virtual function have to be overridden in a derived concrete class?

Pure virtual functions Easy
A. No, it is replaced by a variable
B. Yes, normally it must be overridden
C. No, it is always ignored
D. Yes, but only by constructors

17 What is an abstract class?

Abstract classes and concrete class Easy
A. A class that cannot be instantiated
B. A class containing only data
C. A class with no member functions
D. A class created only on the stack

18 What is a concrete class?

Abstract classes and concrete class Easy
A. A class used only as an interface
B. A class that cannot have objects
C. A class that can be instantiated
D. A class with only pure functions

19 What is a self-referential class?

Self-referential classes Easy
A. A class containing no data members
B. A class having only static functions
C. A class containing a pointer to its own type
D. A class inherited from two classes

20 Which data structure commonly uses a self-referential class?

Self-referential classes Easy
A. Linked list
B. Enumeration
C. Simple constant
D. Preprocessor macro

21 A program reads the number of sensor measurements only after it starts running. Why is dynamic memory allocation appropriate for storing the measurements?

Importance of dynamic memory allocation Medium
A. It permits the array size to be selected at run time
B. It makes the allocated memory persist after program termination
C. It automatically stores every value in a CPU register
D. It guarantees that array access will always be bounds-checked

22 Which statement correctly releases the memory allocated by int* values = new int[50];?

Dynamic memory allocation using new and delete operators Medium
A. free(values);
B. delete values;
C. values = nullptr;
D. delete[] values;

23 Consider the statement int* p = new int(25);. What does it do?

Dynamic memory allocation using new and delete operators Medium
A. Allocates an array containing 25 integers
B. Allocates 25 bytes and converts the block to an integer pointer
C. Allocates one uninitialized integer at address 25
D. Allocates one integer initialized to 25

24 What problem occurs in the following code?

int* p = new int(10);
p = new int(20);
delete p;

Memory leak and allocation failures Medium
A. The first allocation is automatically reclaimed when p receives a new address
B. Both allocated integers are released
C. The first allocated integer is leaked
D. The second allocation is deleted twice

25 Under the default throwing form of new, what normally happens when a memory allocation request cannot be satisfied?

Memory leak and allocation failures Medium
A. It returns a null pointer
B. It creates a smaller memory block than the requested one and reports its size
C. It throws std::bad_alloc
D. It terminates the current loop

26 Which allocation allows failure to be tested using if (p == nullptr) without catching an exception?

Memory leak and allocation failures Medium
A. int* p = new int[100];
B. int* p = new (std::nothrow) int[100];
C. int* p = static_cast<int*>(100);
D. int* p = new int[100], because every form of new returns nullptr on failure

27 Suppose Derived allocates a dynamic resource and an object is deleted using Base* p = new Derived; delete p;. What should Base provide to ensure complete destruction?

Virtual destructor Medium
A. A virtual destructor
B. An overloaded assignment operator
C. A private constructor
D. A static destructor

28 Given the code below, which destructor order occurs when delete p; is executed?

class Base { public: virtual ~Base() {} };
class Derived : public Base { public: ~Derived() {} };
Base* p = new Derived;

Virtual destructor Medium
A. Both destructors execute in an unspecified order selected independently at run time
B. Only Base
C. Base, then Derived
D. Derived, then Base

29 Which pair correctly matches a C++ feature with its usual form of polymorphism?

Compile time polymorphism vs run time polymorphism Medium
A. Templates—run time; virtual functions—compile time
B. Function overloading—run time; templates—run time
C. Virtual functions—compile time; operator overloading—run time
D. Function overloading—compile time; virtual functions—run time

30 A class defines both print(int) and print(double). The compiler selects one using the argument type at the call site. Which mechanism is being used?

Compile time polymorphism vs run time polymorphism Medium
A. Run-time polymorphism
B. Compile-time polymorphism
C. Dynamic allocation
D. Run-time virtual dispatch through a table maintained separately for every object

31 What is printed by this program?

class Base { public: virtual const char* name() { return "Base"; } };
class Derived : public Base { public: const char* name() override { return "Derived"; } };
Base* p = new Derived;
std::cout << p->name();

Virtual functions Medium
A. Base
B. Derived
C. A compiler-generated class identifier
D. The output is undefined because a base pointer cannot refer to a derived object

32 Which change most directly prevents accidental signature mismatches when a derived class intends to replace a base virtual function?

Virtual functions Medium
A. Mark the derived function with override
B. Place the derived function in the protected section and repeat the base implementation
C. Mark the derived function with static
D. Declare the derived function as a friend

33 Which declaration makes draw a pure virtual function?

Pure virtual functions Medium
A. void draw() override;
B. static virtual void draw();
C. virtual void draw();
D. virtual void draw() = 0;

34 An abstract base class declares virtual double area() const = 0;. What must a derived class generally do before objects of that derived class can be created?

Pure virtual functions Medium
A. Call the pure virtual function once from the derived constructor before creating any objects
B. Override area() with a matching signature
C. Remove const from the inherited declaration
D. Convert area() into a static function

35 Which statement about an abstract class Shape is valid?

Abstract classes and concrete class Medium
A. A pointer such as Shape* p; can be declared
B. A direct object such as Shape s; can always be created
C. An abstract class may only be used as a base and therefore cannot contain data members or constructors
D. Every member function of Shape must be pure virtual

36 Shape has pure virtual functions draw() and area(). Class Circle implements draw() but does not implement area(). How should Circle be classified?

Abstract classes and concrete class Medium
A. Concrete because it implements at least one function
B. Invalid because a derived class must implement either all pure functions immediately or none of them
C. Abstract because one pure virtual function remains
D. Concrete because pure virtual functions are optional

37 Which member declaration is suitable for a node in a singly linked list?

Self-referential classes Medium
A. Node* next;
B. Node& next = *this;
C. static Node next;, because every list node must share exactly the same successor
D. Node next;

38 A linked-list node owns its successor through Node* next, and the list is destroyed iteratively. After saving next in a temporary pointer, what operation should be applied to the current dynamically allocated node?

Self-referential classes Medium
A. delete[] current;
B. free(current);
C. current = nullptr;
D. delete current;

39 Consider a non-virtual function show() defined in both Base and Derived. If Base* p = new Derived; and p->show(); is executed, which implementation is selected?

Early binding and late binding Medium
A. Derived::show() through late binding
B. Base::show() through early binding
C. The implementation is chosen randomly because the pointer and object have different types
D. Both implementations in inheritance order

40 A class owns an array allocated in its constructor using data = new int[size];. Which design is essential for releasing that resource when an object is destroyed?

Dynamic constructors Medium
A. A virtual member that sets data to nullptr without deallocating the array
B. A constructor containing delete data;
C. A destructor containing delete[] data;
D. A static function containing free(data);

41 A program must store an unknown number of Record objects, receive more records during execution, and release storage after processing. Which design most directly satisfies these requirements?

Importance of dynamic memory allocation Hard
A. Use a dynamically resized container or manually managed heap storage
B. Use one fixed-size automatic array declared in main
C. Use a global array whose size is selected at compile time
D. Use a local pointer without allocating any target objects

42 What is the correct deallocation sequence for the following allocations?

Widget* a = new Widget;
Widget* b = new Widget[4];

Dynamic memory allocation using new and delete operators Hard
A. delete a; delete[] b;
B. delete[] a; delete b;
C. delete a; delete b;
D. delete[] a; delete[] b;

43 Consider this code:

int* p = new int(7);
int* q = new int[3]{1, 2, 3};

Which statement correctly describes the initialized objects?

Dynamic memory allocation using new and delete operators Hard
A. Both allocations create arrays whose first elements are initialized
B. p points to one default-initialized integer and q to an uninitialized array
C. p points to one integer initialized to 7 and q to an array of three integers
D. p points to an array and q points to one integer

44 What is the main resource-management defect in this code?

int* p = new int(10);
if (condition) return;
delete p;

Memory leak and allocation failures Hard
A. The allocation always throws before condition is evaluated
B. The early return bypasses delete, causing a memory leak
C. The pointer becomes automatically invalid after the conditional return
D. delete must be called before every conditional expression

45 Assume new uses its ordinary throwing form. Which statement best describes the behavior when allocation cannot be satisfied?

Memory leak and allocation failures Hard
A. It converts the allocation into stack storage
B. It invokes delete on the nearest live pointer
C. It returns nullptr and execution continues automatically
D. It throws std::bad_alloc unless a nonthrowing form is used

46 Given:

struct Base { virtual void run(); };
struct Derived : Base { int* data; ~Derived() { delete[] data; } };

What is the required correction before executing delete basePtr; where basePtr points to a Derived object?

Virtual destructor Hard
A. Replace inheritance with function overloading
B. Declare Base::~Base() as virtual
C. Call Derived::run() before deleting the pointer
D. Declare Base::run() as static

47 Which declaration is the most appropriate for a polymorphic base class that should not be directly instantiated but must support safe deletion through a base pointer?

Virtual destructor Hard
A. virtual ~Base() = default;
B. static ~Base() = default;
C. inline delete Base() = default;
D. Base() virtual = default;

48 Which situation demonstrates run-time polymorphism rather than compile-time polymorphism?

Compile time polymorphism vs run time polymorphism Hard
A. Selecting a function using a non-type template argument
B. Selecting max(int, int) through overload resolution
C. Selecting a template specialization during compilation
D. Calling an overridden virtual function through a base reference

49 Suppose Base::f() is non-virtual and Derived::f() has the same signature. For Base* p = new Derived; p->f();, which function is called?

Compile time polymorphism vs run time polymorphism Hard
A. No function because matching signatures are forbidden
B. Derived::f() through dynamic dispatch
C. Both functions in base-to-derived order
D. Base::f() through static binding

50 What is the output of this program?

struct B { virtual void f() { std::cout << "B"; } };
struct D : B { void f() override { std::cout << "D"; } };
void g(B b) { b.f(); }
D d; g(d);

Virtual functions Hard
A. BD because both overrides execute
B. B because passing by value slices the derived object
C. D because f is virtual in B
D. Compilation fails because virtual calls require pointers

51 A derived class declares void process(int) override;, but compilation fails because the base declares virtual void process(int) const;. What caused the failure?

Virtual functions Hard
A. The base function must be declared private
B. Virtual functions cannot accept integer parameters
C. The derived function has an incompatible cv-qualification
D. The override specifier disables inheritance

52 Which declaration makes a member function pure virtual?

Pure virtual functions Hard
A. virtual pure void draw();
B. void draw() = pure;
C. abstract void draw();
D. virtual void draw() = 0;

53 Can a pure virtual function have a definition, and if so, what is the correct interpretation?

Pure virtual functions Hard
A. Yes, and the class automatically becomes concrete
B. Only if the function is also declared static
C. Yes, but the class remains abstract and derived classes may call the qualified definition
D. No, a pure virtual function can never have a definition

54 Which declaration prevents direct object creation but still permits pointers and references to the type?

Abstract classes and concrete class Hard
A. struct Interface { static void run(); };
B. struct Interface { private: int run; };
C. struct Interface { void run() final; };
D. struct Interface { virtual void run() = 0; };

55 A derived class implements every inherited pure virtual function but declares one additional pure virtual function. What is the status of the derived class?

Abstract classes and concrete class Hard
A. It is invalid because derived classes cannot add virtual functions
B. It remains abstract because it introduces a new pure virtual requirement
C. It becomes abstract only when a constructor is private
D. It is concrete because all inherited functions were implemented

56 Why is the following member declaration invalid or unsuitable for a linked node?

struct Node { int value; Node next; };

Self-referential classes Hard
A. The compiler converts Node next into a virtual function
B. A structure cannot contain integer members and class members together
C. A complete Node would contain another complete Node without a finite size
D. Self-reference is allowed only through static data members

57 Which implementation correctly appends a dynamically allocated node after tail when tail is a valid Node* and Node has a Node* next member?

Self-referential classes Hard
A. tail = new Node{value, tail}; tail->next = nullptr;
B. tail->next = new Node{value, nullptr}; tail = tail->next;
C. new tail->next Node{value, nullptr}; tail = next;
D. tail.next = Node{value, nullptr}; tail = tail.next;

58 Which statement correctly contrasts early binding and late binding in C++?

Early binding and late binding Hard
A. Early binding uses dynamic type, while late binding uses only templates
B. Early binding applies only to data, while late binding applies only to constructors
C. Early binding always requires heap allocation, while late binding requires stack allocation
D. Early binding is resolved during compilation, while late binding uses virtual dispatch at run time

59 Given Base& r = derived;, under which condition does r.action() use late binding?

Early binding and late binding Hard
A. r was initialized from an object with automatic storage
B. action is overloaded only in the derived class
C. action is a non-static virtual member with a matching override
D. Base contains at least one data member

60 What is the defining characteristic of a dynamic constructor in traditional C++ terminology?

Dynamic constructors Hard
A. It constructs only objects whose type is determined at run time
B. It is declared with the dynamic keyword
C. It can be invoked only through a virtual function
D. It allocates an object's required memory during construction