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 prevents functions from being called
B. It makes all data permanent
C. It removes the need for variables
D. It allocates memory during program execution

2 Dynamic memory is usually allocated from which memory area?

Importance of dynamic memory allocation Easy
A. The stack
B. The constant pool
C. The register
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. make
C. create
D. alloc

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

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

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

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

6 What is a memory leak?

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

7 What can happen if a program repeatedly leaks memory?

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

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

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

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

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

10 Which keyword is used to declare a virtual destructor?

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

11 Which type of polymorphism is resolved by the compiler?

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

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

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

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

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

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

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

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

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

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

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

17 What is an abstract class?

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

18 What is a concrete class?

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

19 What is a self-referential class?

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

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

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

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 makes the allocated memory persist after program termination
B. It automatically stores every value in a CPU register
C. It guarantees that array access will always be bounds-checked
D. It permits the array size to be selected at run time

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

Dynamic memory allocation using new and delete operators Medium
A. delete values;
B. values = nullptr;
C. delete[] values;
D. free(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 25 bytes and converts the block to an integer pointer
B. Allocates one integer initialized to 25
C. Allocates an array containing 25 integers
D. Allocates one uninitialized integer at address 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 allocated integer is leaked
B. The second allocation is deleted twice
C. Both allocated integers are released
D. The first allocation is automatically reclaimed when p receives a new address

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 creates a smaller memory block than the requested one and reports its size
B. It terminates the current loop
C. It returns a null pointer
D. It throws std::bad_alloc

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 (std::nothrow) int[100];
B. int* p = new int[100], because every form of new returns nullptr on failure
C. int* p = static_cast<int*>(100);
D. 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?

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

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. Base, then Derived
B. Derived, then Base
C. Only Base
D. Both destructors execute in an unspecified order selected independently at run time

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

Compile time polymorphism vs run time polymorphism Medium
A. Virtual functions—compile time; operator overloading—run time
B. Templates—run time; virtual functions—compile time
C. Function overloading—run time; templates—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. Compile-time polymorphism
B. Run-time virtual dispatch through a table maintained separately for every object
C. Dynamic allocation
D. Run-time polymorphism

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. The output is undefined because a base pointer cannot refer to a derived object
B. Base
C. Derived
D. A compiler-generated class identifier

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 static
B. Place the derived function in the protected section and repeat the base implementation
C. Declare the derived function as a friend
D. Mark the derived function with override

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. Remove const from the inherited declaration
C. Override area() with a matching signature
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. An abstract class may only be used as a base and therefore cannot contain data members or constructors
C. A direct object such as Shape s; can always be created
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. Invalid because a derived class must implement either all pure functions immediately or none of them
B. Abstract because one pure virtual function remains
C. Concrete because pure virtual functions are optional
D. Concrete because it implements at least one function

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. current = nullptr;
B. free(current);
C. delete current;
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. The implementation is chosen randomly because the pointer and object have different types
C. Both implementations in inheritance order
D. 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?

Dynamic constructors Medium
A. A static function containing free(data);
B. A destructor containing delete[] data;
C. A constructor containing delete data;
D. A virtual member that sets 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?

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

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. delete must be called before every conditional expression
C. The pointer becomes automatically invalid after the conditional return
D. The early return bypasses delete, causing a memory leak

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 invokes delete on the nearest live pointer
B. It returns nullptr and execution continues automatically
C. It converts the allocation into stack storage
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. Declare Base::run() as static
D. Call 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?

Virtual destructor Hard
A. Base() virtual = default;
B. inline delete Base() = default;
C. static ~Base() = default;
D. virtual ~Base() = 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. Calling an overridden virtual function through a base reference
D. Selecting a template specialization during compilation

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. Both functions in base-to-derived order
B. No function because matching signatures are forbidden
C. Derived::f() through dynamic dispatch
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. Compilation fails because virtual calls require pointers
B. B because passing by value slices the derived object
C. BD because both overrides execute
D. 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?

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

52 Which declaration makes a member function pure virtual?

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

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

Pure virtual functions Hard
A. No, a pure virtual function can never have a definition
B. Only if the function is also declared static
C. Yes, and the class automatically becomes concrete
D. Yes, but the class remains abstract and derived classes may call the qualified 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 { void run() final; };
C. struct Interface { virtual void run() = 0; };
D. 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?

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

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. Self-reference is allowed only through static data members
C. A complete Node would contain another complete Node without a finite size
D. A structure cannot contain integer members and class members together

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->next = new Node{value, nullptr}; tail = tail->next;
B. new tail->next Node{value, nullptr}; tail = next;
C. tail.next = Node{value, nullptr}; tail = tail.next;
D. tail = new Node{value, tail}; tail->next = nullptr;

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

Early binding and late binding Hard
A. Early binding always requires heap allocation, while late binding requires stack allocation
B. Early binding uses dynamic type, while late binding uses only templates
C. Early binding applies only to data, while late binding applies only to constructors
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. action is overloaded only in the derived class
B. action is a non-static virtual member with a matching override
C. Base contains at least one data member
D. r was initialized from an object with automatic storage

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

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