1Which operator is used in C++ to allocate memory dynamically at runtime?
A.create
B.alloc
C.new
D.malloc
Correct Answer: new
Explanation:
The 'new' operator is used in C++ to request memory allocation on the heap at runtime.
Incorrect! Try again.
2Which operator is used to release memory previously allocated by 'new'?
A.remove
B.free
C.delete
D.dealloc
Correct Answer: delete
Explanation:
The 'delete' operator deallocates memory that was allocated using 'new', returning it to the heap.
Incorrect! Try again.
3What happens if the 'new' operator fails to allocate memory?
A.It throws a std::bad_alloc exception
B.It terminates the program immediately
C.It returns NULL by default
D.It returns a void pointer
Correct Answer: It throws a std::bad_alloc exception
Explanation:
In standard C++, if 'new' fails to find sufficient memory, it throws a std::bad_alloc exception (unless std::nothrow is used).
Incorrect! Try again.
4Which syntax is correct for dynamically allocating an array of 10 integers?
A.int *arr = new int[10];
B.int arr[10] = new int;
C.int arr = new int(10);
D.int *arr = new int(10);
Correct Answer: int *arr = new int[10];
Explanation:
The syntax 'new Type[size]' is used to allocate an array dynamically. It returns a pointer to the first element.
Incorrect! Try again.
5How do you correctly deallocate an array allocated with 'new int[10]'?
A.remove arr;
B.delete[] arr;
C.delete arr;
D.delete[10] arr;
Correct Answer: delete[] arr;
Explanation:
When memory is allocated as an array using 'new[]', it must be deallocated using 'delete[]' to ensure destructors are called for all elements.
Incorrect! Try again.
6Where is memory allocated when using the 'new' operator?
A.Heap (Free Store)
B.Stack
C.Data Segment
D.Code Segment
Correct Answer: Heap (Free Store)
Explanation:
Dynamic memory allocation occurs on the Heap (or Free Store), unlike local variables which are stored on the Stack.
Incorrect! Try again.
7What is a memory leak?
A.When a program uses too much RAM
B.When dynamically allocated memory is not freed
C.When the stack overflows
D.When a pointer points to NULL
Correct Answer: When dynamically allocated memory is not freed
Explanation:
A memory leak occurs when memory allocated on the heap is not properly deallocated (deleted) before the pointer to it is lost.
Incorrect! Try again.
8What is the return type of the 'new' operator?
A.void
B.int
C.Reference to the allocated type
D.Pointer to the allocated type
Correct Answer: Pointer to the allocated type
Explanation:
The 'new' operator returns a pointer to the newly allocated object or array.
Incorrect! Try again.
9Is it safe to use 'delete' on a NULL pointer?
A.Yes, but it prints a warning
B.No, it causes a compilation error
C.No, it causes a runtime crash
D.Yes, it does nothing
Correct Answer: Yes, it does nothing
Explanation:
Deleting a NULL pointer is safe in C++; the operation simply does nothing and the program continues.
Incorrect! Try again.
10What is Compile-time Polymorphism also known as?
A.Static Binding
B.Dynamic Binding
C.Late Binding
D.Virtual Binding
Correct Answer: Static Binding
Explanation:
Compile-time polymorphism is resolved during compilation, which is why it is called Static Binding or Early Binding.
Incorrect! Try again.
11Which of the following is an example of Compile-time Polymorphism?
A.Dynamic Casting
B.Abstract Classes
C.Function Overloading
D.Virtual Functions
Correct Answer: Function Overloading
Explanation:
Function overloading and operator overloading are resolved at compile time based on the function signature.
Incorrect! Try again.
12Which keyword is used to enable Run-time Polymorphism in C++?
A.friend
B.static
C.virtual
D.inline
Correct Answer: virtual
Explanation:
The 'virtual' keyword indicates that a function can be overridden in a derived class and the call should be resolved at runtime.
Incorrect! Try again.
13What is 'Early Binding'?
A.The function call is resolved at compile time
B.The memory is allocated at startup
C.The function call is ignored
D.The function call is resolved at runtime
Correct Answer: The function call is resolved at compile time
Explanation:
Early binding means the compiler knows exactly which function to call during the compilation phase.
Incorrect! Try again.
14What is 'Late Binding'?
A.Associating a function call to a function definition at runtime
B.Binding variables to types
C.Linking object files late in the build process
D.Declaring variables at the end of a function
Correct Answer: Associating a function call to a function definition at runtime
Explanation:
Late binding (dynamic binding) waits until the program is running to determine which function implementation to execute based on the object type.
Incorrect! Try again.
15Which mechanism does C++ use to implement Late Binding?
A.Template instantiation
B.Macro expansion
C.Virtual Table (vtable)
D.Stack unwinding
Correct Answer: Virtual Table (vtable)
Explanation:
C++ uses a Virtual Table (vtable) and a Virtual Pointer (vptr) to look up the correct function address at runtime.
Incorrect! Try again.
16Can a C++ constructor be declared 'virtual'?
A.No
B.Yes
C.Only in abstract classes
D.Only if the destructor is also virtual
Correct Answer: No
Explanation:
Constructors cannot be virtual because the object must be created (and its type known) before polymorphism mechanisms (like the vptr) are set up.
Incorrect! Try again.
17What is a Virtual Destructor used for?
A.To ensure the derived class destructor is called when deleting via a base pointer
B.To prevent memory leaks in stack memory
C.To delete static variables
D.To faster delete objects
Correct Answer: To ensure the derived class destructor is called when deleting via a base pointer
Explanation:
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).
Incorrect! Try again.
18What is a Pure Virtual Function?
A.A virtual function initialized to 0 with no body in the base class
B.A function that must be defined in the base class
C.A function that returns void
D.A function with no return type
Correct Answer: A virtual function initialized to 0 with no body in the base class
Explanation:
A pure virtual function is declared with '= 0' syntax (e.g., virtual void func() = 0;) and usually has no implementation in the base class.
Incorrect! Try again.
19A class containing at least one pure virtual function is called:
A.Static Class
B.Concrete Class
C.Abstract Class
D.Virtual Class
Correct Answer: Abstract Class
Explanation:
An abstract class is a class designed to be used as a base class and contains at least one pure virtual function.
Incorrect! Try again.
20Can you instantiate an object of an Abstract Class?
A.Only if it has a constructor
B.Only using pointers
C.Yes
D.No
Correct Answer: No
Explanation:
Abstract classes cannot be instantiated directly because they contain unimplemented pure virtual functions.
Incorrect! Try again.
21What is a Concrete Class?
A.A class that cannot be inherited from
B.A class with no virtual functions
C.A class made of stone
D.A class that implements all inherited pure virtual functions
Correct Answer: A class that implements all inherited pure virtual functions
Explanation:
A concrete class provides implementations for all pure virtual functions inherited from its base abstract classes and can be instantiated.
Incorrect! Try again.
22What is a Self-Referential Class?
A.A class that calls its own main function
B.A class containing a pointer to an object of the same class type
C.A class that inherits from itself
D.A class that cannot be pointed to
Correct Answer: A class containing a pointer to an object of the same class type
Explanation:
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.
Incorrect! Try again.
23Why can't a class contain an object of itself as a member variable?
A.It would cause infinite size/recursion definition
B.It violates encapsulation
C.Compiler cannot name it
D.It is allowed
Correct Answer: It would cause infinite size/recursion definition
Explanation:
If a class contained an instance of itself (not a pointer), the size would be infinite, making it impossible to allocate memory.
Incorrect! Try again.
24What is a Dynamic Constructor?
A.A constructor that changes its name at runtime
B.A constructor that calls other constructors
C.A constructor that is virtual
D.A constructor that allocates memory using 'new'
Correct Answer: A constructor that allocates memory using 'new'
Explanation:
A dynamic constructor is one that uses the 'new' operator to allocate memory for member variables.
Incorrect! Try again.
25If a class uses dynamic memory allocation in its constructor, what should usually be implemented?
A.An inline function
B.A global variable
C.A destructor to free memory
D.A static function
Correct Answer: A destructor to free memory
Explanation:
To prevent memory leaks, if memory is allocated in the constructor, it must be explicitly deallocated in the destructor.
Incorrect! Try again.
26What is the syntax to declare a pure virtual function?
A.virtual void display() {};
B.virtual void display() null;
C.virtual void display() = 0;
D.void display() = 0;
Correct Answer: virtual void display() = 0;
Explanation:
The standard syntax for a pure virtual function is adding '= 0' at the end of the function declaration.
Incorrect! Try again.
27If a derived class does not override a pure virtual function from the base class:
A.The program crashes at runtime
B.The compiler generates a default implementation
C.It is treated as a normal virtual function
D.The derived class also becomes an abstract class
Correct Answer: The derived class also becomes an abstract class
Explanation:
If the derived class fails to implement the pure virtual function, it remains abstract and cannot be instantiated.
Incorrect! Try again.
28Which pointer is used implicitly to access the object's members inside a member function?
A.object
B.self
C.this
D.me
Correct Answer: this
Explanation:
The 'this' pointer is a hidden pointer passed to all non-static member functions that points to the invoking object.
Incorrect! Try again.
29Function overriding requires which of the following?
A.Static keyword
B.Different function names
C.Different parameters
D.Same function name and same signature in base and derived class
Correct Answer: Same function name and same signature in base and derived class
Explanation:
Overriding occurs when a derived class provides a specific implementation of a function already defined in its base class with the exact same signature.
Incorrect! Try again.
30Which operator cannot be overloaded?
A.==
B.new
C.sizeof
D.+
Correct Answer: sizeof
Explanation:
The 'sizeof' operator (along with . , .* , :: , and ?:) cannot be overloaded in C++.
Incorrect! Try again.
31When is the memory allocated for a static data member?
A.When the object is created
B.At compile time
C.At the start of the program (before main)
D.When the class is defined
Correct Answer: At the start of the program (before main)
Explanation:
Static data members are allocated memory once when the program starts, not per object instance.
Incorrect! Try again.
32Which of the following best describes a memory allocation failure handling method?
A.return 0
B.std::nothrow
C.printf error
D.Ignore it
Correct Answer: std::nothrow
Explanation:
Using 'new(std::nothrow)' prevents the exception and returns a NULL pointer if allocation fails.
Incorrect! Try again.
33In C++, polymorphism is mainly divided into:
A.Single and Multiple
B.Public and Private
C.Internal and External
D.Compile-time and Run-time
Correct Answer: Compile-time and Run-time
Explanation:
The two main types of polymorphism are Compile-time (overloading) and Run-time (overriding).
Incorrect! Try again.
34If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show is NOT virtual?
A.Base class show()
B.Both
C.None
D.Derived class show()
Correct Answer: Base class show()
Explanation:
Without the 'virtual' keyword, early binding occurs, and the compiler links the call to the type of the pointer (Base), not the object (Derived).
Incorrect! Try again.
35If you have a pointer 'Base *b = new Derived();', which function is called for 'b->show()' if show IS virtual?
A.Random
B.Derived class show()
C.Compiler Error
D.Base class show()
Correct Answer: Derived class show()
Explanation:
With the 'virtual' keyword, late binding occurs, and the runtime system looks up the actual object type, calling the Derived version.
Incorrect! Try again.
36What is the overhead of using virtual functions?
A.Increased memory for vptr and vtable lookup time
B.Cannot use recursion
C.None
D.Compilation takes longer only
Correct Answer: Increased memory for vptr and vtable lookup time
Explanation:
Virtual functions introduce a small overhead: an extra pointer (vptr) per object and a table lookup during the function call.
Incorrect! Try again.
37Which data structure is typically used to implement a Self-Referential class?
A.Stack (Static)
B.Linked List
C.Array
D.Queue (Static)
Correct Answer: Linked List
Explanation:
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.
Incorrect! Try again.
38Can a static member function be virtual?
A.Sometimes
B.If it returns void
C.No
D.Yes
Correct Answer: No
Explanation:
Static functions are not tied to a specific object instance, so they cannot access the 'this' pointer or the vptr needed for virtual mechanisms.
Incorrect! Try again.
39Which of the following creates an object in the stack?
A.new MyClass;
B.MyClass obj;
C.MyClass *obj = new MyClass();
D.malloc(sizeof(MyClass));
Correct Answer: MyClass obj;
Explanation:
Declaring a variable normally (e.g., 'MyClass obj;') allocates it on the stack. The 'new' keyword allocates on the heap.
Incorrect! Try again.
40What is 'dangling pointer'?
A.A pointer initialized to 0
B.A pointer pointing to NULL
C.A pointer to a virtual function
D.A pointer pointing to a memory location that has been deleted
Correct Answer: A pointer pointing to a memory location that has been deleted
Explanation:
If you delete memory but don't set the pointer to NULL, it points to invalid memory, becoming a dangling pointer.
Incorrect! Try again.
41The mechanism of selecting the appropriate function at compile time is based on:
A.Execution history
B.Function Signature
C.Return Type
D.Object Type
Correct Answer: Function Signature
Explanation:
In compile-time polymorphism (overloading), the compiler distinguishes functions by the number and type of arguments (signature).
Incorrect! Try again.
42What is the role of the vptr (virtual pointer)?
A.Points to the heap memory
B.Points to the virtual table of the class
C.Points to the parent class
D.Points to the stack memory
Correct Answer: Points to the virtual table of the class
Explanation:
The vptr is a hidden pointer generated by the compiler in classes with virtual functions, pointing to the class's specific vtable.
Incorrect! Try again.
43Deep copy is recommended over shallow copy when:
A.The class has no pointers
B.The class uses dynamic memory allocation
C.The class is small
D.The class is static
Correct Answer: The class uses dynamic memory allocation
Explanation:
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.
Incorrect! Try again.
44In C++, an interface is best simulated by:
A.A class with no functions
B.A class with only pure virtual functions
C.A concrete class
D.A class with only static members
Correct Answer: A class with only pure virtual functions
Explanation:
C++ does not have an 'interface' keyword, but an abstract class containing only pure virtual functions acts as an interface.
Incorrect! Try again.
45If a destructor is virtual in the base class, is it automatically virtual in the derived class?
A.Only if explicitly declared
B.Yes
C.Only in multiple inheritance
D.No
Correct Answer: Yes
Explanation:
Once a function (including the destructor) is declared virtual in a base class, it remains virtual in all derived classes automatically.
Incorrect! Try again.
46Which operator is used to access members of a class using a pointer to an object?
A..
B.->
C.:
D.::
Correct Answer: ->
Explanation:
The arrow operator (->) is used to access members when you have a pointer to the object. The dot operator (.) is used with object instances.
Incorrect! Try again.
47What happens if you forget [] when deleting an array of objects?
A.Only the first object's destructor is called
B.All destructors are called
C.Compilation error
D.The heap is cleared entirely
Correct Answer: Only the first object's destructor is called
Explanation:
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.
Incorrect! Try again.
48Can friend functions be virtual?
A.No
B.Only if they are static
C.Only in protected mode
D.Yes
Correct Answer: No
Explanation:
Friend functions are not member functions of the class, so they cannot be virtual.
Incorrect! Try again.
49Dynamic binding typically reduces execution speed slightly because:
A.The code is interpreted
B.Pointer dereferencing takes time
C.Memory is fragmented
D.The compiler sleeps
Correct Answer: Pointer dereferencing takes time
Explanation:
The program must look up the function address in the vtable at runtime (dereferencing pointers) rather than jumping to a hardcoded address.
Incorrect! Try again.
50To prevent a class from being inherited, C++11 introduced which keyword?
A.sealed
B.stop
C.final
D.end
Correct Answer: final
Explanation:
The 'final' specifier creates a class that cannot be derived from, effectively preventing further polymorphism on that branch.
Incorrect! Try again.
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 →