Unit 5: Dynamic Memory Management and Polymorphism - Practice Quiz
1 Which C++ operator allocates memory dynamically?
typedef
sizeof
new
delete
2
Which statement correctly releases an integer allocated as int* p = new int;?
remove(p);
free(p);
delete[] p;
delete p;
3
Which statement correctly releases an array allocated as int* a = new int[10];?
delete a;
delete[] a;
free[] a;
clear[] a;
4
Why is a base-class destructor commonly declared virtual?
5 Which C++ feature is an example of compile-time polymorphism?
6 When is compile-time polymorphism resolved?
7 Which combination is commonly used to achieve run-time polymorphism in C++?
8 In run-time polymorphism, when is the function implementation selected?
9 Which keyword declares a virtual function in C++?
virtual
friend
explicit
static
10 Where is a virtual function initially declared for use in an inheritance hierarchy?
11 What does a dynamic constructor typically do?
12 Which operator is commonly used inside a dynamic constructor?
new
return
sizeof
delete
13 Which statement about an abstract class is correct?
14 What is a concrete class?
15 What does a self-referential class contain?
16
Which syntax declares show as a pure virtual function?
static void show() = 0;
virtual void show() = 1;
virtual void show() = 0;
void virtual = show();
17 What usually happens when a class contains a pure virtual function?
18 Which type of binding is generally associated with non-virtual function calls?
19 Which type of binding is associated with virtual function calls through a base pointer?
20 Which situation causes a memory leak?
nullptr
21
Which statement correctly releases the memory allocated by int* values = new int[10];?
free(values);
values = nullptr;
delete[] values;
delete values;
22
Consider the following code:
int* p = new int(25);
int* q = p;
delete p;
What is the state of q after delete p;?
nullptr automatically
0
25
23 What change is required to ensure that both destructors execute when a derived object is deleted through a base-class pointer?
virtual
explicit
virtual
static
24
Given the following declarations, which operation has undefined behavior because Base does not have a virtual destructor?
class Base {
public:
~Base() {}
};
class Derived : public Base {
public:
~Derived() {}
};
Derived* p = new Derived; delete p;
delete new Derived;
Base* p = new Derived; delete p;
delete new Base;
25
A class defines print(int) and print(double). When print(7) is called, how is the selected function determined?
26 Which C++ feature provides compile-time polymorphism without requiring inheritance?
27
Suppose Derived overrides the virtual function show() inherited from Base. What does the following code call?
Base* p = new Derived;
p->show();
Derived::show() through dynamic dispatch
Base::show() in every case
28 Which combination is necessary for run-time polymorphism when calling an overridden member function?
29
What is the purpose of writing override in the following declaration?
void process() override;
process() a static function
30
Assume Base::display() is virtual and Derived::display() overrides it. Which call explicitly bypasses virtual dispatch for an object d of type Derived?
(&d)->display();
static_cast<Base&>(d).display();
d.display();
d.Base::display();
31
A class constructor allocates an array using data = new int[size];. Which class member should normally release this resource?
32 A class owns a dynamically allocated array through a raw pointer. Why is the compiler-generated copy constructor usually unsuitable?
void*
33
Given class Shape { public: virtual double area() const = 0; };, which class is concrete?
double area() const override
Shape but adds only data members
area() unimplemented
34
Which declaration is valid when Shape is an abstract class?
Shape objects[3];
Shape* pointer = nullptr;
Shape copy = Shape();
Shape object;
35
Which member declaration correctly makes Node a self-referential class suitable for a singly linked list?
virtual Node next;
static Node next;
Node next;
Node* next;
36
Consider struct Node { int value; Node* next; };. What should next contain for the final node of a non-circular linked list?
37
Which declaration makes calculate() a pure virtual function?
virtual int calculate() == 0;
static int calculate() = 0;
virtual int calculate() = 0;
int calculate() override = 0;
38
Class B inherits from an abstract class containing two pure virtual functions. B overrides only one of them. What follows?
B cannot define any constructors
B becomes a concrete class
B remains an abstract class
B loses access to base members
39
A non-virtual member function is called through a Base* that points to a Derived object. Which class's function is selected?
Derived
Base
40 Which call is resolved using late binding?
41
Consider the following standard C++ code:
int* p = new int[0];
delete[] p;
Assuming the new expression completes successfully, which statement is guaranteed?
p is a pointer value that may validly be passed to delete[].
p must be a null pointer because the requested array is empty.
delete[] p has undefined behavior because no elements were constructed.
p points to one default-initialized int used as an array sentinel.
42
What is the behavior of the following program fragment?
int* p = new int[4];
delete p;
delete can detect every array allocation.
int has a trivial destructor and no array cleanup is needed.
43
Consider:
struct Base {
virtual void run() {}
~Base() {}
};
struct Derived : Base {
int* data = new int[100];
~Derived() { delete[] data; }
};
Base* p = new Derived;
delete p;
Which conclusion is correct?
Derived::~Derived from the dynamic allocation type.
Base::~Base runs, but the behavior remains otherwise well-defined.
Base::~Base is not virtual.
Base is virtual.
44
Assume Base has a public virtual destructor. What is the status of this code?
Base* p = new Derived[2];
delete[] p;
Derived object and then releases the complete array allocation.
Base and Derived have identical object sizes.
45
Which overload is selected by the call below?
void process(long);
template<class T>
void process(T);
process(1);
process(long), because a non-template function always defeats a template.
process<int>(int), because it provides an exact parameter match.
46
What does the following call select?
struct Base {
void show(int);
};
struct Derived : Base {
void show(double);
};
Derived d;
d.show(7);
Base::show(int), because its conversion sequence is an exact match.
Derived::show(double), because the derived declaration hides the base overload set.
47
What is printed by this code?
struct Base {
virtual void print(int x = 1) { std::cout << "B" << x; }
};
struct Derived : Base {
void print(int x = 2) override { std::cout << "D" << x; }
};
Base* p = new Derived;
p->print();
delete p;
D1, because dispatch is dynamic but the default argument is selected statically.
B2, because the dynamic default is combined with the statically selected function body.
D2, because both the function body and its default argument are selected dynamically.
B1, because omitting an argument disables virtual dispatch for the function call.
48
Consider the access levels in this hierarchy:
struct Base {
virtual void execute() { std::cout << "Base"; }
};
struct Derived : Base {
private:
void execute() override { std::cout << "Derived"; }
};
Base* p = new Derived;
p->execute();
What happens?
Derived; access is checked through Base, then virtual dispatch occurs.
Base; a private override is excluded from the virtual dispatch table.
49
What is printed while constructing and destroying Derived d?
struct Base {
Base() { identify(); }
virtual ~Base() { identify(); }
virtual void identify() { std::cout << "B"; }
};
struct Derived : Base {
Derived() { identify(); }
~Derived() override { identify(); }
void identify() override { std::cout << "D"; }
};
Derived d;
BDBD, because only constructor calls suppress dispatch to a derived override.
BDDB, because dispatch is restricted to the currently constructed or destroyed class.
DBBD, because destruction changes the dynamic type before the derived destructor starts.
DDDD, because every call observes the complete object's most-derived dynamic type.
50
Given a virtual override, what does the explicitly qualified call do?
struct Base {
virtual void f() { std::cout << "B"; }
};
struct Derived : Base {
void f() override { std::cout << "D"; }
};
Base* p = new Derived;
p->Base::f();
D because the object still has Derived as its dynamic type.
B because explicit qualification suppresses virtual dispatch.
51
Analyze this constructor:
class BufferPair {
int* first;
int* second;
public:
BufferPair()
: first(new int[100]), second(new int[200]) {}
~BufferPair() {
delete[] first;
delete[] second;
}
};
What happens if allocation of second throws std::bad_alloc?
BufferPair::~BufferPair.
second == nullptr after the allocation exception is handled.
52
Consider a class whose constructor allocates memory:
class Buffer {
std::size_t size;
int* data;
public:
Buffer(std::size_t n) : size(n), data(new int[n]) {}
~Buffer() { delete[] data; }
};
Buffer a(10);
Buffer b = a;
With only the shown members, what is the central defect?
b is ill-formed.
size is copied after data.
53
Why does Derived remain abstract in this example?
struct Base {
virtual void update(int) = 0;
};
struct Derived : Base {
void update(double) {}
};
Derived::update(double) overrides the function but preserves its pure status.
Derived::update(double) hides but does not override Base::update(int).
54
Consider:
struct Abstract {
virtual void work() = 0;
};
void Abstract::work() {
std::cout << "default";
}
struct Concrete : Abstract {
void work() override {
Abstract::work();
}
};
Which statement is correct?
Abstract becomes concrete because every pure virtual function now has a definition.
Concrete remains abstract because an override cannot call a pure virtual implementation.
Abstract remains abstract, while Concrete is concrete and may call the qualified definition.
55 Which declaration can represent a link to another object of the same class without making the class definition infinitely recursive?
struct Node { int value; Node child[1]; };
struct Node { int value; Node* next; };
struct Node { int value; Node next; };
struct Node { int value; const Node next; };
56
Consider this owning self-referential class:
struct Node {
Node* next = nullptr;
~Node() { delete next; }
};
Node* a = new Node;
Node* b = new Node;
a->next = b;
b->next = a;
delete a;
What is the result?
delete, so each node is destroyed exactly once.
a is destroyed, while b remains as a safely detached allocation.
57
What requirement applies to this pure virtual destructor?
struct Interface {
virtual ~Interface() = 0;
};
struct Implementation : Interface {};
Interface::~Interface still needs a definition because derived destruction invokes it.
Interface becomes concrete once the compiler generates Implementation::~Implementation.
Implementation cannot be instantiated because a pure destructor cannot be overridden implicitly.
Interface::~Interface needs no definition because pure virtual functions are never invoked.
58
What is printed after object slicing occurs?
struct Base {
virtual void identify() { std::cout << "B"; }
};
struct Derived : Base {
void identify() override { std::cout << "D"; }
};
Derived d;
Base b = d;
b.identify();
B, because b is a separate sliced object whose dynamic type is Base.
D, because the virtual table from d is copied into the sliced base object.
B, because calls made with object syntax always use compile-time binding.
D, because slicing removes data members but preserves the derived dynamic type.
59
Which statement correctly distinguishes allocation failure from constructor failure?
Widget* p = new (std::nothrow) Widget;
std::nothrow disables exception handling.
nullptr, but an exception from Widget() can still propagate.
nullptr result.
std::bad_alloc, while constructor failure returns nullptr.
60
Assume process() may throw. Which implementation releases the allocated object on both normal and exceptional exits without an explicit try/catch?
void run() {
// replacement code
}
Resource* p = new Resource; delete p; process(*p);
Resource* p = new Resource; process(*p); p = nullptr;
auto p = std::make_unique<Resource>(); process(*p);
Resource* p = new Resource; process(*p); delete p;
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 →