Unit 5: Dynamic Memory Management and Polymorphism - Practice Quiz
1 Why is dynamic memory allocation useful in C++?
2 Dynamic memory is usually allocated from which memory area?
3 Which operator is used to allocate memory dynamically in C++?
4
Which operator releases memory allocated for a single object using new?
5
Which operator should be used to release memory allocated with new[]?
6 What is a memory leak?
7 What can happen if a program repeatedly leaks memory?
8
What exception does standard C++ commonly throw when new cannot allocate memory?
std::new_error
std::bad_alloc
std::memory_error
std::alloc_fail
9 Why is a destructor often declared virtual in a polymorphic base class?
10 Which keyword is used to declare a virtual destructor?
11 Which type of polymorphism is resolved by the compiler?
12 Which feature is commonly associated with compile-time polymorphism?
13 Which keyword declares a virtual function in C++?
14 What happens when a virtual function is called through a base-class pointer?
15 How is a pure virtual function commonly declared in C++?
pure 0
virtual 0
= 0
== 0
16 Does a pure virtual function have to be overridden in a derived concrete class?
17 What is an abstract class?
18 What is a concrete class?
19 What is a self-referential class?
20 Which data structure commonly uses a self-referential class?
21 A program reads the number of sensor measurements only after it starts running. Why is dynamic memory allocation appropriate for storing the measurements?
22
Which statement correctly releases the memory allocated by int* values = new int[50];?
delete values;
values = nullptr;
delete[] values;
free(values);
23
Consider the statement int* p = new int(25);. What does it do?
25 bytes and converts the block to an integer pointer
25
25 integers
25
24
What problem occurs in the following code?
int* p = new int(10);
p = new int(20);
delete p;
p receives a new address
25
Under the default throwing form of new, what normally happens when a memory allocation request cannot be satisfied?
std::bad_alloc
26
Which allocation allows failure to be tested using if (p == nullptr) without catching an exception?
int* p = new (std::nothrow) int[100];
int* p = new int[100], because every form of new returns nullptr on failure
int* p = static_cast<int*>(100);
int* p = new int[100];
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?
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;
Base, then Derived
Derived, then Base
Base
29 Which pair correctly matches a C++ feature with its usual form of polymorphism?
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?
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();
Base
Derived
32 Which change most directly prevents accidental signature mismatches when a derived class intends to replace a base virtual function?
static
override
33
Which declaration makes draw a pure virtual function?
void draw() override;
static virtual void draw();
virtual void draw();
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?
const from the inherited declaration
area() with a matching signature
area() into a static function
35
Which statement about an abstract class Shape is valid?
Shape* p; can be declared
Shape s; can always be created
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?
37 Which member declaration is suitable for a node in a singly linked list?
Node next;
Node& next = *this;
static Node next;, because every list node must share exactly the same successor
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?
current = nullptr;
free(current);
delete current;
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?
Derived::show() through late binding
Base::show() through early binding
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?
free(data);
delete[] data;
delete data;
data to nullptr without deallocating the array
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?
main
42
What is the correct deallocation sequence for the following allocations?
Widget* a = new Widget;
Widget* b = new Widget[4];
delete[] a; delete[] b;
delete a; delete[] b;
delete[] a; delete b;
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?
p points to one default-initialized integer and q to an uninitialized array
p points to one integer initialized to 7 and q to an array of three integers
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;
condition is evaluated
delete must be called before every conditional expression
delete, causing a memory leak
45
Assume new uses its ordinary throwing form. Which statement best describes the behavior when allocation cannot be satisfied?
delete on the nearest live pointer
nullptr and execution continues automatically
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?
Base::~Base() as virtual
Base::run() as static
Derived::run() before deleting the pointer
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?
Base() virtual = default;
inline delete Base() = default;
static ~Base() = default;
virtual ~Base() = default;
48 Which situation demonstrates run-time polymorphism rather than compile-time polymorphism?
max(int, int) through overload resolution
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?
Derived::f() through dynamic dispatch
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);
B because passing by value slices the derived object
BD because both overrides execute
D because f is virtual in B
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?
override specifier disables inheritance
52 Which declaration makes a member function pure virtual?
abstract void draw();
void draw() = pure;
virtual void draw() = 0;
virtual pure void draw();
53 Can a pure virtual function have a definition, and if so, what is the correct interpretation?
54 Which declaration prevents direct object creation but still permits pointers and references to the type?
struct Interface { static void run(); };
struct Interface { void run() final; };
struct Interface { virtual void run() = 0; };
struct Interface { private: int run; };
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?
56
Why is the following member declaration invalid or unsuitable for a linked node?
struct Node { int value; Node next; };
Node next into a virtual function
Node would contain another complete Node without a finite size
57
Which implementation correctly appends a dynamically allocated node after tail when tail is a valid Node* and Node has a Node* next member?
tail->next = new Node{value, nullptr}; tail = tail->next;
new tail->next Node{value, nullptr}; tail = next;
tail.next = Node{value, nullptr}; tail = tail.next;
tail = new Node{value, tail}; tail->next = nullptr;
58 Which statement correctly contrasts early binding and late binding in C++?
59
Given Base& r = derived;, under which condition does r.action() use late binding?
action is overloaded only in the derived class
action is a non-static virtual member with a matching override
Base contains at least one data member
r was initialized from an object with automatic storage
60 What is the defining characteristic of a dynamic constructor in traditional C++ terminology?
dynamic keyword
Did this save you a night before the exam?
LPU Notes is free, and it stays free. Ads cover part of the server bill. The rest comes out of a student's own pocket: the domain, the storage, and keeping the site up through the weeks everyone needs it at once.
The payment button didn't load. An ad blocker or a filtered network is the usual reason. to try again.
Nothing here is ever locked, and nothing unlocks. Chip in only if it was worth it. What it pays for →