Unit 5: Dynamic Memory Management and Polymorphism - Practice Quiz
1 Which C++ operator allocates memory dynamically?
delete
sizeof
new
typedef
2
Which statement correctly releases an integer allocated as int* p = new int;?
delete[] p;
remove(p);
delete p;
free(p);
3
Which statement correctly releases an array allocated as int* a = new int[10];?
delete[] a;
free[] a;
delete 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++?
friend
static
virtual
explicit
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?
delete
new
sizeof
return
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?
virtual void show() = 0;
void virtual = show();
virtual void show() = 1;
static void show() = 0;
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];?
values = nullptr;
delete values;
free(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;?
25
0
nullptr automatically
23 What change is required to ensure that both destructors execute when a derived object is deleted through a base-class pointer?
virtual
virtual
static
explicit
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;
Base* p = new Derived; delete p;
delete new Base;
delete new Derived;
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();
Base::show() in every case
Derived::show() through dynamic dispatch
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?
static_cast<Base&>(d).display();
d.Base::display();
d.display();
(&d)->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?
area() unimplemented
Shape but adds only data members
double area() const override
34
Which declaration is valid when Shape is an abstract class?
Shape copy = Shape();
Shape object;
Shape* pointer = nullptr;
Shape objects[3];
35
Which member declaration correctly makes Node a self-referential class suitable for a singly linked list?
Node* next;
static Node next;
Node next;
virtual 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;
int calculate() override = 0;
virtual int calculate() = 0;
static int calculate() = 0;
38
Class B inherits from an abstract class containing two pure virtual functions. B overrides only one of them. What follows?
B remains an abstract class
B cannot define any constructors
B becomes a concrete 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?
delete[] p has undefined behavior because no elements were constructed.
p must be a null pointer because the requested array is empty.
p is a pointer value that may validly be passed to delete[].
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;
int has a trivial destructor and no array cleanup is needed.
delete can detect every array allocation.
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?
Base::~Base is not virtual.
Derived::~Derived from the dynamic allocation type.
Base is virtual.
Base::~Base runs, but the behavior remains otherwise well-defined.
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<int>(int), because it provides an exact parameter match.
process(long), because a non-template function always defeats a template.
46
What does the following call select?
struct Base {
void show(int);
};
struct Derived : Base {
void show(double);
};
Derived d;
d.show(7);
Derived::show(double), because the derived declaration hides the base overload set.
Base::show(int), because its conversion sequence is an exact match.
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;
B2, because the dynamic default is combined with the statically selected function body.
B1, because omitting an argument disables virtual dispatch for the function call.
D1, because dispatch is dynamic but the default argument is selected statically.
D2, because both the function body and its default argument are selected dynamically.
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?
Base; a private override is excluded from the virtual dispatch table.
Derived; access is checked through Base, then virtual dispatch occurs.
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;
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.
BDBD, because only constructor calls suppress dispatch to a derived override.
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();
B because explicit qualification suppresses virtual dispatch.
D because the object still has Derived as its dynamic type.
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?
size is copied after data.
b is ill-formed.
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?
Concrete remains abstract because an override cannot call a pure virtual implementation.
Abstract becomes concrete because every pure virtual function now has a definition.
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; const Node next; };
struct Node { int value; Node child[1]; };
struct Node { int value; Node next; };
struct Node { int value; 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?
a is destroyed, while b remains as a safely detached allocation.
delete, so each node is destroyed exactly once.
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::~Interface needs no definition because pure virtual functions are never invoked.
Implementation cannot be instantiated because a pure destructor cannot be overridden implicitly.
Interface becomes concrete once the compiler generates Implementation::~Implementation.
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.
D, because slicing removes data members but preserves the derived dynamic type.
B, because calls made with object syntax always use compile-time binding.
59
Which statement correctly distinguishes allocation failure from constructor failure?
Widget* p = new (std::nothrow) Widget;
std::nothrow disables exception handling.
std::bad_alloc, while constructor failure returns nullptr.
nullptr result.
nullptr, but an exception from Widget() can still propagate.
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
}
auto p = std::make_unique<Resource>(); process(*p);
Resource* p = new Resource; delete p; process(*p);
Resource* p = new Resource; process(*p); p = nullptr;
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 →