Notice: Due to an ongoing migration of our storage and publishing system, material requested since last month will not be uploaded for the time being, and new requests are paused. All existing notes remain available.
You're offline
Unit 5 MCQsDynamic Memory Management and Polymorphism
Which operator is used in C++ to allocate memory dynamically at runtime?
Not quite. Try another option.
Answer: new
The 'new' operator is used in C++ to request memory allocation on the heap at runtime.
Question 2 of 50
Which operator is used to release memory previously allocated by 'new'?
Not quite. Try another option.
Answer: delete
The 'delete' operator deallocates memory that was allocated using 'new', returning it to the heap.
Question 3 of 50
What happens if the 'new' operator fails to allocate memory?
Not quite. Try another option.
Answer: It throws a std::bad_alloc exception
In standard C++, if 'new' fails to find sufficient memory, it throws a std::bad_alloc exception (unless std::nothrow is used).
Question 4 of 50
Which syntax is correct for dynamically allocating an array of 10 integers?
Not quite. Try another option.
Answer: int *arr = new int[10];
The syntax 'new Type[size]' is used to allocate an array dynamically. It returns a pointer to the first element.
Question 5 of 50
How do you correctly deallocate an array allocated with 'new int[10]'?
Not quite. Try another option.
Answer: delete[] arr;
When memory is allocated as an array using 'new[]', it must be deallocated using 'delete[]' to ensure destructors are called for all elements.
Question 6 of 50
Where is memory allocated when using the 'new' operator?
Not quite. Try another option.
Answer: Heap (Free Store)
Dynamic memory allocation occurs on the Heap (or Free Store), unlike local variables which are stored on the Stack.
Question 7 of 50
What is a memory leak?
Not quite. Try another option.
Answer: When dynamically allocated memory is not freed
A memory leak occurs when memory allocated on the heap is not properly deallocated (deleted) before the pointer to it is lost.
Question 8 of 50
What is the return type of the 'new' operator?
Not quite. Try another option.
Answer: Pointer to the allocated type
The 'new' operator returns a pointer to the newly allocated object or array.
Question 9 of 50
Is it safe to use 'delete' on a NULL pointer?
Not quite. Try another option.
Answer: Yes, it does nothing
Deleting a NULL pointer is safe in C++; the operation simply does nothing and the program continues.
Question 10 of 50
What is Compile-time Polymorphism also known as?
Not quite. Try another option.
Answer: Static Binding
Compile-time polymorphism is resolved during compilation, which is why it is called Static Binding or Early Binding.
Question 11 of 50
Which of the following is an example of Compile-time Polymorphism?
Not quite. Try another option.
Answer: Function Overloading
Function overloading and operator overloading are resolved at compile time based on the function signature.
Question 12 of 50
Which keyword is used to enable Run-time Polymorphism in C++?
Not quite. Try another option.
Answer: virtual
The 'virtual' keyword indicates that a function can be overridden in a derived class and the call should be resolved at runtime.
Question 13 of 50
What is 'Early Binding'?
Not quite. Try another option.
Answer: The function call is resolved at compile time
Early binding means the compiler knows exactly which function to call during the compilation phase.
Question 14 of 50
What is 'Late Binding'?
Not quite. Try another option.
Answer: Associating a function call to a function definition at runtime
Late binding (dynamic binding) waits until the program is running to determine which function implementation to execute based on the object type.
Question 15 of 50
Which mechanism does C++ use to implement Late Binding?
Not quite. Try another option.
Answer: Virtual Table (vtable)
C++ uses a Virtual Table (vtable) and a Virtual Pointer (vptr) to look up the correct function address at runtime.
Question 16 of 50
Can a C++ constructor be declared 'virtual'?
Not quite. Try another option.
Answer: No
Constructors cannot be virtual because the object must be created (and its type known) before polymorphism mechanisms (like the vptr) are set up.
Question 17 of 50
What is a Virtual Destructor used for?
Not quite. Try another option.
Answer: To ensure the derived class destructor is called when deleting via a base pointer
If a base class destructor is not virtual, deleting a derived object through a base pointer results in undefined behavior (usually the derived destructor is not called).
Question 18 of 50
What is a Pure Virtual Function?
Not quite. Try another option.
Answer: A virtual function initialized to 0 with no body in the base class
A pure virtual function is declared with '= 0' syntax (e.g., virtual void func() = 0;) and usually has no implementation in the base class.
Question 19 of 50
A class containing at least one pure virtual function is called:
Not quite. Try another option.
Answer: Abstract Class
An abstract class is a class designed to be used as a base class and contains at least one pure virtual function.
Question 20 of 50
Can you instantiate an object of an Abstract Class?
Not quite. Try another option.
Answer: No
Abstract classes cannot be instantiated directly because they contain unimplemented pure virtual functions.
Question 21 of 50
What is a Concrete Class?
Not quite. Try another option.
Answer: A class that implements all inherited pure virtual functions
A concrete class provides implementations for all pure virtual functions inherited from its base abstract classes and can be instantiated.
Question 22 of 50
What is a Self-Referential Class?
Not quite. Try another option.
Answer: A class containing a pointer to an object of the same class type
Self-referential classes have a member that is a pointer (or reference) to the class itself. This is common in data structures like Linked Lists.
Question 23 of 50
Why can't a class contain an object of itself as a member variable?
Not quite. Try another option.
Answer: It would cause infinite size/recursion definition
If a class contained an instance of itself (not a pointer), the size would be infinite, making it impossible to allocate memory.
Question 24 of 50
What is a Dynamic Constructor?
Not quite. Try another option.
Answer: A constructor that allocates memory using 'new'
A dynamic constructor is one that uses the 'new' operator to allocate memory for member variables.
Question 25 of 50
If a class uses dynamic memory allocation in its constructor, what should usually be implemented?
Not quite. Try another option.
Answer: A destructor to free memory
To prevent memory leaks, if memory is allocated in the constructor, it must be explicitly deallocated in the destructor.
Question 26 of 50
What is the syntax to declare a pure virtual function?
Not quite. Try another option.
Answer: virtual void display() = 0;
The standard syntax for a pure virtual function is adding '= 0' at the end of the function declaration.
Question 27 of 50
If a derived class does not override a pure virtual function from the base class:
Not quite. Try another option.
Answer: The derived class also becomes an abstract class
If the derived class fails to implement the pure virtual function, it remains abstract and cannot be instantiated.
Question 28 of 50
Which pointer is used implicitly to access the object's members inside a member function?
Not quite. Try another option.
Answer: this
The 'this' pointer is a hidden pointer passed to all non-static member functions that points to the invoking object.
Question 29 of 50
Function overriding requires which of the following?
Not quite. Try another option.
Answer: Same function name and same signature in base and derived class
Overriding occurs when a derived class provides a specific implementation of a function already defined in its base class with the exact same signature.
Question 30 of 50
Which operator cannot be overloaded?
Not quite. Try another option.
Answer: sizeof
The 'sizeof' operator (along with . , .* , :: , and ?:) cannot be overloaded in C++.
Question 31 of 50
When is the memory allocated for a static data member?
Not quite. Try another option.
Answer: At the start of the program (before main)
Static data members are allocated memory once when the program starts, not per object instance.
Question 32 of 50
Which of the following best describes a memory allocation failure handling method?
Not quite. Try another option.
Answer: std::nothrow
Using 'new(std::nothrow)' prevents the exception and returns a NULL pointer if allocation fails.
Question 33 of 50
In C++, polymorphism is mainly divided into:
Not quite. Try another option.
Answer: Compile-time and Run-time
The two main types of polymorphism are Compile-time (overloading) and Run-time (overriding).
Question 34 of 50
If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show is NOT virtual?
Not quite. Try another option.
Answer: Base class show()
Without the 'virtual' keyword, early binding occurs, and the compiler links the call to the type of the pointer (Base), not the object (Derived).
Question 35 of 50
If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show IS virtual?
Not quite. Try another option.
Answer: Derived class show()
With the 'virtual' keyword, late binding occurs, and the runtime system looks up the actual object type, calling the Derived version.
Question 36 of 50
What is the overhead of using virtual functions?
Not quite. Try another option.
Answer: Increased memory for vptr and vtable lookup time
Virtual functions introduce a small overhead: an extra pointer (vptr) per object and a table lookup during the function call.
Question 37 of 50
Which data structure is typically used to implement a Self-Referential class?
Not quite. Try another option.
Answer: Linked List
A node in a linked list contains a pointer to the next node of the same type, making it a classic example of a self-referential class.
Question 38 of 50
Can a static member function be virtual?
Not quite. Try another option.
Answer: No
Static functions are not tied to a specific object instance, so they cannot access the 'this' pointer or the vptr needed for virtual mechanisms.
Question 39 of 50
Which of the following creates an object in the stack?
Not quite. Try another option.
Answer: MyClass obj;
Declaring a variable normally (e.g., 'MyClass obj;') allocates it on the stack. The 'new' keyword allocates on the heap.
Question 40 of 50
What is 'dangling pointer'?
Not quite. Try another option.
Answer: A pointer pointing to a memory location that has been deleted
If you delete memory but don't set the pointer to NULL, it points to invalid memory, becoming a dangling pointer.
Question 41 of 50
The mechanism of selecting the appropriate function at compile time is based on:
Not quite. Try another option.
Answer: Function Signature
In compile-time polymorphism (overloading), the compiler distinguishes functions by the number and type of arguments (signature).
Question 42 of 50
What is the role of the vptr (virtual pointer)?
Not quite. Try another option.
Answer: Points to the virtual table of the class
The vptr is a hidden pointer generated by the compiler in classes with virtual functions, pointing to the class's specific vtable.
Question 43 of 50
Deep copy is recommended over shallow copy when:
Not quite. Try another option.
Answer: The class uses dynamic memory allocation
If a class has pointers to dynamic memory, a shallow copy copies the pointer address (sharing memory), causing double-delete errors. Deep copy duplicates the actual data.
Question 44 of 50
In C++, an interface is best simulated by:
Not quite. Try another option.
Answer: A class with only pure virtual functions
C++ does not have an 'interface' keyword, but an abstract class containing only pure virtual functions acts as an interface.
Question 45 of 50
If a destructor is virtual in the base class, is it automatically virtual in the derived class?
Not quite. Try another option.
Answer: Yes
Once a function (including the destructor) is declared virtual in a base class, it remains virtual in all derived classes automatically.
Question 46 of 50
Which operator is used to access members of a class using a pointer to an object?
Not quite. Try another option.
Answer: ->
The arrow operator (->) is used to access members when you have a pointer to the object. The dot operator (.) is used with object instances.
Question 47 of 50
What happens if you forget [] when deleting an array of objects?
Not quite. Try another option.
Answer: Only the first object's destructor is called
Using 'delete' instead of 'delete[]' on an array is undefined behavior, but typically results in only the destructor for the first element being called, causing memory leaks.
Question 48 of 50
Can friend functions be virtual?
Not quite. Try another option.
Answer: No
Friend functions are not member functions of the class, so they cannot be virtual.
Question 49 of 50
Dynamic binding typically reduces execution speed slightly because:
Not quite. Try another option.
Answer: Pointer dereferencing takes time
The program must look up the function address in the vtable at runtime (dereferencing pointers) rather than jumping to a hardcoded address.
Question 50 of 50
To prevent a class from being inherited, C++11 introduced which keyword?
Not quite. Try another option.
Answer: final
The 'final' specifier creates a class that cannot be derived from, effectively preventing further polymorphism on that branch.
That's unit 5
Answer a few questions and your score shows up here.
Nearly through. Did unit 5 of CSE202 save you a night before the exam?
LPU Notes is free. A student pays for the servers.
Did unit 5 of CSE202 save you a night before the exam?
Then you can help keep it free for the next batch. 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.