1. What is inheritance in C++?
Correct Answer: a) A mechanism to create new classes from existing classes
Explanation:
Inheritance is an OOP mechanism where a new class (derived) acquires the properties and behaviors of an existing class (base).
2. Which symbol is used to denote inheritance in C++?
Correct Answer: c) :
Explanation:
A single colon (:) is used to specify inheritance, e.g., class Derived : public Base.
3. What is the default access specifier for inheritance in a 'class'?
Correct Answer: b) private
Explanation:
If no access specifier is provided, a class inherits from another class using private inheritance by default.
4. What is the default access specifier for inheritance in a 'struct'?
Correct Answer: a) public
Explanation:
If no access specifier is provided, a struct inherits from another struct/class using public inheritance by default.
5. Which type of inheritance involves exactly one base class and one derived class?
Correct Answer: b) Single inheritance
Explanation:
Single inheritance occurs when a derived class inherits from only one base class.
6. Which type of inheritance involves a chain of classes (e.g., A -> B -> C)?
Correct Answer: c) Multilevel inheritance
Explanation:
Multilevel inheritance occurs when a class is derived from a class, which is further derived from another class.
7. Which type of inheritance allows a derived class to inherit from more than one base class?
Correct Answer: a) Multiple inheritance
Explanation:
Multiple inheritance allows a single derived class to inherit properties from multiple base classes.
8. What is the 'diamond problem' in C++?
Correct Answer: b) Ambiguity arising when two base classes inherit from the same grandparent class
Explanation:
The diamond problem occurs in multiple inheritance when two base classes share a common ancestor, leading to duplicate copies of the grandparent's members in the derived class.
9. How is the diamond problem typically resolved in C++?
Correct Answer: b) Using virtual inheritance
Explanation:
Virtual inheritance ensures that only a single instance of the common base class is shared among the derived classes.
10. Which keyword is used to implement virtual inheritance?
Correct Answer: b) virtual
Explanation:
The 'virtual' keyword is placed before the base class name in the inheritance list to make it a virtual base class.
11. In public inheritance, what happens to the public members of the base class?
Correct Answer: c) They remain public
Explanation:
In public inheritance, the access level of base class members remains unchanged in the derived class.
12. In protected inheritance, what happens to the public members of the base class?
Correct Answer: b) They become protected
Explanation:
In protected inheritance, public and protected members of the base class become protected in the derived class.
13. In private inheritance, what happens to the public members of the base class?
Correct Answer: a) They become private
Explanation:
In private inheritance, all public and protected members of the base class become private in the derived class.
14. Can a derived class directly access the private members of its base class?
Correct Answer: b) No, never
Explanation:
Private members of a base class are hidden from the derived class. They can only be accessed via public or protected member functions of the base class.
15. Which access specifier allows members to be accessible in the derived class but not from outside the class hierarchy?
Correct Answer: c) protected
Explanation:
Protected members are accessible within the class and its derived classes, but not from the outside world.
16. What is the order of constructor calls when a derived class object is created?
Correct Answer: b) Base then Derived
Explanation:
The base class constructor is called first to initialize the base part of the object, followed by the derived class constructor.
17. What is the order of destructor calls when a derived class object is destroyed?
Correct Answer: a) Derived then Base
Explanation:
Destructors are called in the reverse order of constructors: the derived class destructor runs first, then the base class destructor.
18. Are constructors inherited in C++?
Correct Answer: b) No, but they can be explicitly called
Explanation:
Constructors are not inherited, but the derived class constructor can (and must) call the base class constructor to initialize the base part.
19. How do you call a base class parameterized constructor from a derived class?
Correct Answer: b) Using the initialization list of the derived constructor
Explanation:
Base class constructors with parameters must be invoked in the initialization list of the derived class constructor.
20. What is polymorphism in C++?
Correct Answer: b) The ability of a message or function to be processed in more than one form
Explanation:
Polymorphism means 'many forms'. It allows entities like functions or objects to behave differently based on the context.
21. Compile-time polymorphism is also known as?
Correct Answer: c) Static polymorphism
Explanation:
Compile-time polymorphism is resolved by the compiler during compilation, hence it is called static or early binding.
22. Run-time polymorphism is also known as?
Correct Answer: c) Dynamic polymorphism
Explanation:
Run-time polymorphism is resolved during program execution, hence it is called dynamic or late binding.
23. Which of the following is an example of compile-time polymorphism?
Correct Answer: c) Function overloading
Explanation:
Function overloading and operator overloading are resolved at compile time based on the argument types.
24. Which of the following is an example of run-time polymorphism?
Correct Answer: d) Virtual functions
Explanation:
Virtual functions achieve run-time polymorphism by allowing the correct function to be called based on the actual object type at runtime.
25. Which keyword is used to declare a virtual function?
Correct Answer: c) virtual
Explanation:
The 'virtual' keyword is used in the base class declaration to indicate that a function can be overridden in derived classes.
26. Can a virtual function be static?
Correct Answer: b) No
Explanation:
Virtual functions must be non-static member functions because they rely on the object's vptr for dynamic dispatch, and static functions do not have a 'this' pointer.
27. Can a virtual function be a friend function?
Correct Answer: b) No
Explanation:
A virtual function must be a member of a class to participate in dynamic binding, whereas a friend function is a non-member external function.
28. Can a constructor be virtual?
Correct Answer: b) No
Explanation:
Constructors cannot be virtual because the object is not fully constructed when the constructor is invoked, meaning the vptr is not yet initialized.
29. Should the destructor of a base class be virtual?
Correct Answer: b) Yes, if the class is intended to be used polymorphically
Explanation:
Making the base class destructor virtual ensures that the derived class destructor is called when an object is deleted through a base class pointer, preventing memory leaks.
30. What is function overriding?
Correct Answer: b) Redefining a base class virtual function in the derived class with the exact same signature
Explanation:
Function overriding occurs when a derived class provides a specific implementation for a virtual function already defined in its base class, using the same signature.
31. What is function hiding (name hiding) in C++?
Correct Answer: b) Defining a function in the derived class with the same name but a different signature than the base class
Explanation:
If a derived class defines a function with the same name but different parameters, it hides all base class functions with that name, preventing overloading across scopes.
32. How can you access a hidden base class function from the derived class?
Correct Answer: b) Using the scope resolution operator (Base::func)
Explanation:
You can explicitly call the hidden base class function using the scope resolution operator with the base class name.
33. What is a pure virtual function?
Correct Answer: a) A virtual function with no implementation in the base class
Explanation:
A pure virtual function is a virtual function that has no definition in the base class and is assigned 0 in its declaration.
34. What is the correct syntax to declare a pure virtual function?
Correct Answer: c) virtual void func() = 0;
Explanation:
A pure virtual function is declared by adding '= 0' at the end of the virtual function declaration.
35. What is an abstract class?
Correct Answer: b) A class that contains at least one pure virtual function
Explanation:
A class becomes abstract in C++ if it contains one or more pure virtual functions.
36. Can you instantiate an object of an abstract class?
Correct Answer: b) No
Explanation:
Abstract classes cannot be instantiated directly; they serve only as base classes for other classes to inherit and implement the pure virtual functions.
37. Can an abstract class have a constructor?
Correct Answer: a) Yes
Explanation:
Abstract classes can have constructors, which are called when an object of a concrete derived class is created to initialize the base part.
38. Can an abstract class have non-virtual member functions?
Correct Answer: a) Yes
Explanation:
An abstract class can contain any combination of virtual, pure virtual, and non-virtual member functions, as well as data members.
39. What is a vtable (virtual table)?
Correct Answer: b) A static array of function pointers used for dynamic dispatch of virtual functions
Explanation:
The vtable is a static array created by the compiler for each class with virtual functions, storing pointers to the actual virtual function implementations.
40. What is a vptr (virtual pointer)?
Correct Answer: b) A hidden pointer added to objects of classes with virtual functions, pointing to the vtable
Explanation:
The vptr is a hidden pointer inserted by the compiler into each object of a class with virtual functions, pointing to its respective vtable.
41. Where is the vptr stored?
Correct Answer: b) Inside the memory layout of the object
Explanation:
The vptr is stored as a hidden data member within the memory footprint of each object of a class that has virtual functions.
42. When is the vptr initialized?
Correct Answer: b) During the constructor execution
Explanation:
The compiler inserts code to initialize the vptr inside the constructor, ensuring it points to the correct vtable for the object being constructed.
43. What is the size of an object of a class that has one virtual function and no data members?
Correct Answer: d) Size of the vptr (usually 4 or 8 bytes)
Explanation:
Even with no data members, the object will contain the hidden vptr, making its size equal to the size of a pointer on the target architecture.
44. What happens if a derived class does not override a pure virtual function from its base class?
Correct Answer: b) The derived class also becomes an abstract class
Explanation:
If a derived class fails to provide an implementation for all pure virtual functions of its base, it remains abstract and cannot be instantiated.
45. Why is it important to use a virtual destructor in a base class?
Correct Answer: b) To ensure the derived class destructor is called when deleting via a base pointer
Explanation:
Without a virtual destructor, deleting a derived object through a base class pointer results in undefined behavior, typically causing memory leaks because the derived destructor is skipped.
46. What is object slicing?
Correct Answer: b) When a derived class object is assigned to a base class object by value, losing derived-specific data
Explanation:
Object slicing occurs when a derived object is passed by value to a base class parameter or variable, truncating the derived part and leaving only the base part.
47. How can object slicing be prevented?
Correct Answer: a) Using pointers or references
Explanation:
Passing or storing objects via pointers or references prevents slicing because the original derived object is not copied into a base object.
48. What is the purpose of the 'override' specifier in C++11?
Correct Answer: b) Explicitly indicating that a function is intended to override a base class virtual function
Explanation:
The 'override' specifier ensures the function actually overrides a base class virtual function, catching signature mismatches at compile time.
49. What is the purpose of the 'final' specifier when applied to a class?
Correct Answer: b) Preventing the class from being inherited
Explanation:
When 'final' is applied to a class, no other class can inherit from it.
50. What is the purpose of the 'final' specifier when applied to a virtual function?
Correct Answer: b) Preventing the function from being overridden in derived classes
Explanation:
When 'final' is applied to a virtual function, derived classes cannot override it.
51. Can a class be both abstract and final?
Correct Answer: b) No, it is a logical contradiction
Explanation:
An abstract class cannot be instantiated and must be inherited to be useful, while a final class cannot be inherited. Thus, they contradict each other.
52. What is dynamic binding?
Correct Answer: b) Resolving function calls at runtime based on the actual object type
Explanation:
Dynamic binding (late binding) delays the resolution of a function call until runtime, using mechanisms like virtual functions.
53. What is static binding?
Correct Answer: a) Resolving function calls at compile time based on the reference/pointer type
Explanation:
Static binding (early binding) resolves function calls during compilation, typical for non-virtual functions, overloading, and templates.
54. Which cast is used for safe downcasting with a runtime type check?
Correct Answer: b) dynamic_cast
Explanation:
dynamic_cast safely converts pointers or references of base classes to derived classes, performing a runtime check to ensure the object is actually of the target type.
55. Which cast is used for downcasting without a runtime type check?
Correct Answer: a) static_cast
Explanation:
static_cast can perform downcasting based on compile-time type information, but it does not check the actual runtime type, making it unsafe if the assumption is wrong.
56. What does dynamic_cast return if the cast fails for a pointer type?
Correct Answer: b) Returns a null pointer
Explanation:
If dynamic_cast fails on a pointer, it returns a null pointer (nullptr), allowing the programmer to check for success.
57. What happens if dynamic_cast fails for a reference type?
Correct Answer: b) Throws a std::bad_cast exception
Explanation:
Since references cannot be null, dynamic_cast throws a std::bad_cast exception if the cast to a reference type fails.
58. What does RTTI stand for in C++?
Correct Answer: a) Run-Time Type Information
Explanation:
RTTI is a mechanism that exposes information about an object's data type at runtime, primarily used via dynamic_cast and typeid.
59. Which operator is used to query the type of an object at runtime?
Correct Answer: b) typeid
Explanation:
The typeid operator returns a reference to a std::type_info object, which contains information about the type of the expression.
60. Can a virtual function be declared inline?
Correct Answer: a) Yes
Explanation:
A virtual function can be declared inline. The compiler may inline it if it can resolve the call at compile time, but dynamic dispatch will still occur if resolved at runtime.
61. Is the inline request honored for a virtual function called via a base class pointer at runtime?
Correct Answer: b) No, because the call is resolved dynamically
Explanation:
When a virtual function is called through a pointer or reference and resolved at runtime, the compiler cannot inline it because the exact function to call is not known until execution.
62. What is multilevel inheritance?
Correct Answer: b) A class deriving from a derived class (e.g., A -> B -> C)
Explanation:
Multilevel inheritance is a chain of inheritance where a class serves as a base for one derived class, which in turn acts as a base for another.
63. In multilevel inheritance (A -> B -> C), what is the order of constructor execution when creating an object of C?
Correct Answer: b) A, B, C
Explanation:
Constructors are always called in the order of inheritance, from the most base class down to the most derived class.
64. What is hierarchical inheritance?
Correct Answer: a) One base class, multiple derived classes
Explanation:
Hierarchical inheritance occurs when two or more derived classes inherit from a single common base class.
65. What is hybrid inheritance?
Correct Answer: a) A combination of two or more types of inheritance
Explanation:
Hybrid inheritance is a mix of multiple, multilevel, hierarchical, or single inheritance in a single program.
66. If two base classes in multiple inheritance have a member function with the same name, how do you resolve the ambiguity in the derived class?
Correct Answer: b) Using the scope resolution operator to specify the base class
Explanation:
You must explicitly specify which base class's function to call using the scope resolution operator (e.g., Base1::func()).
67. In multiple inheritance, what is the order of base class initialization?
Correct Answer: b) Left to right as declared in the derived class inheritance list
Explanation:
Base class constructors are called in the exact order they are listed in the derived class's inheritance specification, regardless of the initialization list order.
68. What is the purpose of the 'using' declaration in a derived class regarding base class members?
Correct Answer: b) To bring hidden base class names into the derived class scope for overloading
Explanation:
A 'using' declaration (e.g., using Base::func;) injects the base class name into the derived class scope, allowing it to participate in overloading with derived class functions.
69. How can you change the access level of an inherited member in the derived class?
Correct Answer: b) By using a 'using' declaration in the desired access section
Explanation:
You can change the access level of an inherited member by placing a 'using Base::member;' declaration in the public, protected, or private section of the derived class.
70. What does private inheritance typically represent in design terms?
Correct Answer: b) Is-implemented-in-terms-of relationship
Explanation:
Private inheritance is often used to implement a class using another class's implementation without exposing an 'is-a' interface to the outside world.
71. Why is composition often preferred over private inheritance?
Correct Answer: b) Composition is simpler and prevents overriding of virtual functions
Explanation:
Composition is generally preferred because it is easier to understand, avoids the complexities of inheritance, and prevents unintended overriding of virtual functions.
72. Can a virtual function be called from within a base class constructor?
Correct Answer: b) Yes, but it calls the base class version
Explanation:
When a virtual function is called from a base class constructor, the object's vptr points to the base class's vtable, so the base class version is executed, not the derived one.
73. What is a virtual base class?
Correct Answer: b) A base class that is inherited virtually to prevent duplicate instances in diamond inheritance
Explanation:
A virtual base class ensures that only one shared instance of the base class exists in the derived class hierarchy, resolving the diamond problem.
74. In a diamond inheritance with a virtual base class, who is responsible for initializing the virtual base class?
Correct Answer: b) The most derived class
Explanation:
When virtual inheritance is used, the most derived class's constructor is responsible for directly invoking the virtual base class's constructor, bypassing the intermediate classes.
75. Can a pure virtual function have an implementation in the base class?
Correct Answer: a) Yes
Explanation:
A pure virtual function can have an implementation provided outside the class definition, which derived classes can optionally call.
76. Why would you provide an implementation for a pure virtual function?
Correct Answer: b) To provide default behavior that derived classes can explicitly call
Explanation:
Providing an implementation for a pure virtual function allows derived classes to reuse common default code while still forcing them to provide their own override.
77. What is an 'interface' in C++?
Correct Answer: b) A class with only pure virtual functions and no data members
Explanation:
In C++, an interface is typically modeled as an abstract class containing only pure virtual functions and no data members or implementations.
78. Can an abstract class have data members?
Correct Answer: a) Yes
Explanation:
An abstract class can have data members, constructors, and non-virtual functions, as long as it contains at least one pure virtual function.
79. What is the primary performance cost of using virtual functions?
Correct Answer: b) Extra memory for vptr and runtime overhead for indirect function calls
Explanation:
Virtual functions require extra memory for the vptr in each object and introduce a slight runtime overhead due to the indirect call through the vtable.
80. Does a class with no virtual functions have a vptr?
Correct Answer: b) No
Explanation:
A class only gets a vptr if it declares or inherits at least one virtual function.
81. What is early binding?
Correct Answer: b) Binding at compile time
Explanation:
Early binding, or static binding, occurs when the compiler resolves the function call during compilation.
82. What is late binding?
Correct Answer: b) Binding at runtime
Explanation:
Late binding, or dynamic binding, occurs when the function call is resolved during program execution based on the actual object type.
83. Can you achieve true run-time polymorphism by passing an object by value?
Correct Answer: b) No, due to object slicing
Explanation:
Passing by value causes object slicing, where the derived part is lost, and the function will always resolve to the base class version.
84. Why must you pass objects by reference or pointer to achieve polymorphism?
Correct Answer: b) To prevent object slicing and allow dynamic dispatch
Explanation:
References and pointers preserve the actual derived type of the object, allowing the vptr to correctly route the call to the overridden function.
85. What does the typeid operator return?
Correct Answer: b) A reference to a std::type_info object
Explanation:
The typeid operator returns a const reference to a std::type_info object, which contains runtime type information.
86. If typeid is applied to a reference of a polymorphic type, when is it evaluated?
Correct Answer: b) At runtime
Explanation:
For polymorphic types (classes with virtual functions), typeid evaluates the actual runtime type of the object referred to, not the static type of the reference.
87. Is static_cast safe for downcasting?
Correct Answer: b) No, it does not check the type at runtime
Explanation:
static_cast relies on compile-time information. If the object is not actually of the target derived type, using the resulting pointer/reference leads to undefined behavior.
88. What happens if you use static_cast for an invalid downcast and then access the object?
Correct Answer: c) Undefined behavior
Explanation:
Because static_cast performs no runtime check, an invalid downcast results in undefined behavior when the incorrectly cast pointer or reference is used.
89. Is RTTI enabled by default in all C++ compilers?
Correct Answer: b) No, it is often disabled by default to save overhead and must be enabled via compiler flags
Explanation:
Many compilers (like GCC and Clang) disable RTTI by default (e.g., -fno-rtti) to reduce binary size and runtime overhead, requiring explicit flags to enable it.
90. What is the primary purpose of polymorphism in software design?
Correct Answer: b) To provide a unified interface and allow extensibility without modifying existing code
Explanation:
Polymorphism allows writing code that operates on base class interfaces, making it easy to add new derived classes without changing the existing code that uses the interface (Open/Closed Principle).









