1Which operator is used in C++ to allocate memory dynamically at runtime?
A.malloc
B.alloc
C.create
D.new
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.delete
C.dealloc
D.free
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 returns NULL by default
C.It terminates the program immediately
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[10] arr;
D.delete[] 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.Code Segment
B.Stack
C.Data Segment
D.Heap (Free Store)
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 the stack overflows
B.When a program uses too much RAM
C.When dynamically allocated memory is not freed
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.Pointer to the allocated type
C.Reference to the allocated type
D.int
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.No, it causes a runtime crash
B.No, it causes a compilation error
C.Yes, it does nothing
D.Yes, but it prints a warning
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.Late Binding
B.Static Binding
C.Dynamic 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.Function Overloading
B.Abstract Classes
C.Virtual Functions
D.Dynamic Casting
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.inline
C.static
D.virtual
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 runtime
B.The function call is ignored
C.The memory is allocated at startup
D.The function call is resolved at compile time
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.Virtual Table (vtable)
B.Macro expansion
C.Stack unwinding
D.Template instantiation
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.Yes
B.No
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 faster delete objects
B.To delete static variables
C.To prevent memory leaks in stack memory
D.To ensure the derived class destructor is called when deleting via a base pointer
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 function that must be defined in the base class
B.A function with no return type
C.A function that returns void
D.A virtual function initialized to 0 with no body in the base class
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.Virtual Class
C.Abstract Class
D.Concrete 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.Yes
B.Only if it has a constructor
C.Only using pointers
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 implements all inherited pure virtual functions
B.A class made of stone
C.A class with no virtual functions
D.A class that cannot be inherited from
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 cannot be pointed to
B.A class containing a pointer to an object of the same class type
C.A class that calls its own main function
D.A class that inherits from itself
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.Compiler cannot name it
B.It violates encapsulation
C.It is allowed
D.It would cause infinite size/recursion definition
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 allocates memory using 'new'
C.A constructor that is virtual
D.A constructor that calls other constructors
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 destructor to free memory
C.A static function
D.A global variable
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() null;
B.virtual void display() {};
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 compiler generates a default implementation
B.The derived class also becomes an abstract class
C.The program crashes at runtime
D.It is treated as a normal virtual function
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.self
B.object
C.me
D.this
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.Same function name and same signature in base and derived class
B.Different parameters
C.Different function names
D.Static keyword
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.At compile time
B.At the start of the program (before main)
C.When the object is created
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.printf error
B.Ignore it
C.return 0
D.std::nothrow
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.Compile-time and Run-time
B.Internal and External
C.Single and Multiple
D.Public and Private
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.None
B.Derived class show()
C.Both
D.Base 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.Base class show()
D.Compiler Error
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.None
B.Compilation takes longer only
C.Increased memory for vptr and vtable lookup time
D.Cannot use recursion
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.Queue (Static)
D.Array
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.Yes
D.No
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.malloc(sizeof(MyClass));
B.MyClass *obj = new MyClass();
C.new MyClass;
D.MyClass obj;
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 pointing to a memory location that has been deleted
B.A pointer pointing to NULL
C.A pointer initialized to 0
D.A pointer to a virtual function
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.Return Type
B.Execution history
C.Function Signature
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 parent class
B.Points to the virtual table of the class
C.Points to the stack memory
D.Points to the heap 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 uses dynamic memory allocation
B.The class is static
C.The class is small
D.The class has no pointers
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 concrete class
B.A class with no functions
C.A class with only static members
D.A class with only pure virtual functions
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.No
C.Yes
D.Only in multiple inheritance
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.The heap is cleared entirely
B.All destructors are called
C.Compilation error
D.Only the first object's destructor is called
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.Only in protected mode
B.Only if they are static
C.Yes
D.No
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.Pointer dereferencing takes time
B.The code is interpreted
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.end
C.final
D.stop
Correct Answer: final
Explanation:
The 'final' specifier creates a class that cannot be derived from, effectively preventing further polymorphism on that branch.