1Which operator is used in C++ to allocate memory dynamically at runtime?
A.malloc
B.alloc
C.new
D.create
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.free
B.delete
C.remove
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 returns NULL by default
B.It throws a std::bad_alloc exception
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 = new int[10];
C.int *arr = new int(10);
D.int arr[10] = new int;
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.delete arr;
B.delete[] arr;
C.delete[10] arr;
D.remove 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.Stack
B.Heap (Free Store)
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.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.Yes, it does nothing
B.No, it causes a runtime crash
C.No, it causes a compilation error
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.Dynamic Binding
B.Late Binding
C.Static 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.Virtual Functions
B.Function Overloading
C.Abstract Classes
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.static
B.virtual
C.friend
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 runtime
B.The function call is resolved at compile time
C.The function call is ignored
D.The memory is allocated at startup
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.Binding variables to types
B.Associating a function call to a function definition at runtime
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.Stack unwinding
B.Virtual Table (vtable)
C.Macro expansion
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 delete static variables
B.To ensure the derived class destructor is called when deleting via a base pointer
C.To faster delete objects
D.To prevent memory leaks in stack memory
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 with no return type
B.A function that must be defined in the base class
C.A virtual function initialized to 0 with no body in the base class
D.A function that returns void
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.Virtual Class
B.Abstract Class
C.Concrete Class
D.Static 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.No
C.Only using pointers
D.Only if it has a constructor
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 made of stone
B.A class with no virtual functions
C.A class that implements all inherited pure 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 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 is allowed
B.It would cause infinite size/recursion definition
C.Compiler cannot name it
D.It violates encapsulation
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 is virtual
B.A constructor that calls other constructors
C.A constructor that allocates memory using 'new'
D.A constructor that changes its name at runtime
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.A static function
B.A destructor to free memory
C.A global variable
D.An inline 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() null;
B.virtual void display() = 0;
C.virtual void display() {};
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.me
C.this
D.object
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.Different function names
B.Different parameters
C.Same function name and same signature in base and derived class
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.When the object is created
B.When the class is defined
C.At compile time
D.At the start of the program (before main)
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.Ignore it
B.std::nothrow
C.printf error
D.return 0
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.Internal and External
C.Compile-time and Run-time
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.Base class show()
B.Derived class show()
C.Both
D.None
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.Base class show()
B.Derived class show()
C.Compiler Error
D.Random
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.Increased memory for vptr and vtable lookup time
C.Compilation takes longer only
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.Array
B.Linked List
C.Stack (Static)
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.Yes
B.No
C.Sometimes
D.If it returns void
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.MyClass obj;
B.MyClass *obj = new MyClass();
C.malloc(sizeof(MyClass));
D.new 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 pointing to NULL
B.A pointer pointing to a memory location that has been deleted
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.Function Signature
B.Object Type
C.Return Type
D.Execution history
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 heap memory
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 only static members
B.A class with only pure virtual functions
C.A class with no functions
D.A concrete class
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.Yes
B.No
C.Only if explicitly declared
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.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.Yes
B.No
C.Only if they are static
D.Only in protected mode
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.The compiler sleeps
D.Memory is fragmented
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.stop
B.final
C.end
D.sealed
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.
Give Feedback
Help us improve by sharing your thoughts or reporting issues.