C++ Inheritance and Polymorphism - MCQs

1 min read

1. What is inheritance in C++?

a) A mechanism to create new classes from existing classes
b) A way to delete classes
c) A method to hide data
d) A type of loop
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++?

a) ->
b) ::
c) :
d) .
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'?

a) public
b) private
c) protected
d) friend
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'?

a) public
b) private
c) protected
d) internal
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?

a) Multiple inheritance
b) Single inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
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)?

a) Multiple inheritance
b) Single inheritance
c) Multilevel inheritance
d) Hybrid inheritance
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?

a) Multiple inheritance
b) Single inheritance
c) Multilevel inheritance
d) Hierarchical inheritance
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++?

a) A memory leak issue
b) Ambiguity arising when two base classes inherit from the same grandparent class
c) A syntax error in multiple inheritance
d) A problem with virtual destructors
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++?

a) Using private inheritance
b) Using virtual inheritance
c) Using static members
d) Using friend classes
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?

a) static
b) virtual
c) inline
d) friend
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?

a) They become private
b) They become protected
c) They remain public
d) They are deleted
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?

a) They become private
b) They become protected
c) They remain public
d) They are deleted
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?

a) They become private
b) They become protected
c) They remain public
d) They are deleted
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?

a) Yes, always
b) No, never
c) Only if it is a friend
d) Only if they are static
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?

a) public
b) private
c) protected
d) default
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?

a) Derived then Base
b) Base then Derived
c) Random order
d) Simultaneously
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?

a) Derived then Base
b) Base then Derived
c) Random order
d) Simultaneously
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++?

a) Yes, automatically
b) No, but they can be explicitly called
c) Yes, if they are public
d) No, and they cannot be called
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?

a) Inside the derived constructor body
b) Using the initialization list of the derived constructor
c) Using the new keyword
d) It is called automatically
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++?

a) Hiding data
b) The ability of a message or function to be processed in more than one form
c) Creating multiple objects
d) Deleting multiple classes
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?

a) Dynamic polymorphism
b) Late binding
c) Static polymorphism
d) Runtime polymorphism
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?

a) Static polymorphism
b) Early binding
c) Dynamic polymorphism
d) Compile-time polymorphism
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?

a) Virtual functions
b) Function overriding
c) Function overloading
d) Dynamic casting
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?

a) Function overloading
b) Operator overloading
c) Template functions
d) Virtual functions
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?

a) static
b) inline
c) virtual
d) friend
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?

a) Yes
b) No
c) Only if it is public
d) Only if it is inline
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?

a) Yes
b) No
c) Only if it is static
d) Only if it is inline
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?

a) Yes
b) No
c) Only if it is default
d) Only if it is parameterized
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?

a) No, never
b) Yes, if the class is intended to be used polymorphically
c) Only if it is private
d) Only if it is static
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?

a) Defining multiple functions with the same name in the same class
b) Redefining a base class virtual function in the derived class with the exact same signature
c) Hiding a base class function
d) Overloading operators
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++?

a) Overriding a virtual function
b) Defining a function in the derived class with the same name but a different signature than the base class
c) Making a function private
d) Deleting a function
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?

a) Using the 'this' pointer
b) Using the scope resolution operator (Base::func)
c) Using the 'virtual' keyword
d) It cannot be accessed
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?

a) A virtual function with no implementation in the base class
b) A static virtual function
c) A friend virtual function
d) An inline 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?

a) virtual void func();
b) void func() = 0;
c) virtual void func() = 0;
d) pure virtual void func();
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?

a) A class with no data members
b) A class that contains at least one pure virtual function
c) A class with only static members
d) A class with no constructors
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?

a) Yes
b) No
c) Only if it has a default constructor
d) Only if it is public
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?

a) Yes
b) No
c) Only if it is pure virtual
d) Only if it is static
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?

a) Yes
b) No
c) Only if they are static
d) Only if they are friend
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)?

a) A table of object data
b) A static array of function pointers used for dynamic dispatch of virtual functions
c) A table of constructors
d) A table of friend functions
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)?

a) A pointer to the object's data
b) A hidden pointer added to objects of classes with virtual functions, pointing to the vtable
c) A pointer to the base class
d) A static 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?

a) In the global memory
b) Inside the memory layout of the object
c) In the vtable
d) In the base class only
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?

a) Before the constructor runs
b) During the constructor execution
c) After the destructor runs
d) At compile time
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?

a) 0 bytes
b) 1 byte
c) Size of the virtual function
d) Size of the vptr (usually 4 or 8 bytes)
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?

a) Compilation error
b) The derived class also becomes an abstract class
c) Runtime error
d) The base class function is called
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?

a) To make the class abstract
b) To ensure the derived class destructor is called when deleting via a base pointer
c) To prevent inheritance
d) To reduce memory usage
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?

a) Deleting part of an object
b) When a derived class object is assigned to a base class object by value, losing derived-specific data
c) Copying an object
d) Destroying an object
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?

a) Using pointers or references
b) Using static members
c) Using friend functions
d) Using private inheritance
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?

a) Making a function virtual
b) Explicitly indicating that a function is intended to override a base class virtual function
c) Hiding a base class function
d) Making a function static
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?

a) Making the class abstract
b) Preventing the class from being inherited
c) Making all members static
d) Deleting the 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?

a) Making the function pure virtual
b) Preventing the function from being overridden in derived classes
c) Making the function static
d) Deleting the 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?

a) Yes
b) No, it is a logical contradiction
c) Only if it has no members
d) Only in C++20
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?

a) Resolving function calls at compile time
b) Resolving function calls at runtime based on the actual object type
c) Binding variables to memory
d) Binding classes to namespaces
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?

a) Resolving function calls at compile time based on the reference/pointer type
b) Resolving function calls at runtime
c) Binding objects to the heap
d) Binding functions to classes
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?

a) static_cast
b) dynamic_cast
c) const_cast
d) reinterpret_cast
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?

a) static_cast
b) dynamic_cast
c) const_cast
d) reinterpret_cast
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?

a) Throws an exception
b) Returns a null pointer
c) Returns the original pointer
d) Causes a compilation error
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?

a) Returns a null reference
b) Throws a std::bad_cast exception
c) Returns the original reference
d) Causes a compilation error
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++?

a) Run-Time Type Information
b) Real-Time Thread Integration
c) Recursive Template Type Inference
d) Runtime Transaction Isolation
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?

a) typeof
b) typeid
c) decltype
d) typeinfo
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?

a) Yes
b) No
c) Only if it is static
d) Only if it is pure virtual
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?

a) Yes, always
b) No, because the call is resolved dynamically
c) Only if the function is small
d) Only if the compiler is optimized
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?

a) A class inheriting from multiple base classes
b) A class deriving from a derived class (e.g., A -> B -> C)
c) Multiple classes inheriting from a single base
d) A class inheriting from itself
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?

a) C, B, A
b) A, B, C
c) B, A, C
d) Random
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?

a) One base class, multiple derived classes
b) Multiple base classes, one derived class
c) A chain of classes
d) A mix of inheritance types
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?

a) A combination of two or more types of inheritance
b) Inheritance using templates
c) Inheritance with virtual functions only
d) Inheritance without constructors
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?

a) Using the 'virtual' keyword
b) Using the scope resolution operator to specify the base class
c) Overriding the function
d) It cannot be resolved
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?

a) Reverse order of declaration
b) Left to right as declared in the derived class inheritance list
c) Alphabetical order
d) Random order
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?

a) To delete base class members
b) To bring hidden base class names into the derived class scope for overloading
c) To make base class members private
d) To initialize 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?

a) By redefining the member
b) By using a 'using' declaration in the desired access section
c) By using the 'friend' keyword
d) It cannot be changed
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?

a) Is-a relationship
b) Is-implemented-in-terms-of relationship
c) Has-a relationship
d) Uses-a relationship
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?

a) Composition is faster
b) Composition is simpler and prevents overriding of virtual functions
c) Composition allows multiple inheritance
d) Composition uses less memory
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?

a) Yes, and it always calls the derived class version
b) Yes, but it calls the base class version
c) No, it causes a compilation error
d) No, it causes a runtime error
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?

a) A base class with only virtual functions
b) A base class that is inherited virtually to prevent duplicate instances in diamond inheritance
c) An abstract class
d) A base class with no constructors
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?

a) The immediate derived classes
b) The most derived class
c) The base class itself
d) The compiler automatically
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?

a) Yes
b) No
c) Only if it is static
d) Only if it is inline
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?

a) To make the class concrete
b) To provide default behavior that derived classes can explicitly call
c) To prevent inheritance
d) To reduce memory usage
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++?

a) A class with only data members
b) A class with only pure virtual functions and no data members
c) A struct with public members
d) A namespace
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?

a) Yes
b) No
c) Only if they are static
d) Only if they are const
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?

a) Increased compilation time
b) Extra memory for vptr and runtime overhead for indirect function calls
c) Larger source code size
d) Inability to use templates
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?

a) Yes
b) No
c) Only if it inherits from a class with virtual functions
d) Only if it is abstract
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?

a) Binding at runtime
b) Binding at compile time
c) Binding during linking
d) Binding during execution
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?

a) Binding at compile time
b) Binding at runtime
c) Binding during linking
d) Binding during preprocessing
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?

a) Yes
b) No, due to object slicing
c) Only if it is const
d) Only if it is static
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?

a) To save memory
b) To prevent object slicing and allow dynamic dispatch
c) To make the code shorter
d) To avoid compilation errors
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?

a) The name of the class as a string
b) A reference to a std::type_info object
c) The size of the object
d) The address of the vtable
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?

a) At compile time
b) At runtime
c) During linking
d) It is never 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?

a) Yes, it checks the type at runtime
b) No, it does not check the type at runtime
c) Yes, it throws an exception if wrong
d) Only if the class is abstract
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?

a) Compilation error
b) Runtime exception
c) Undefined behavior
d) Returns null
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?

a) Yes, always
b) No, it is often disabled by default to save overhead and must be enabled via compiler flags
c) Only in C++20
d) Only for abstract classes
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?

a) To reduce code size
b) To provide a unified interface and allow extensibility without modifying existing code
c) To hide data
d) To speed up compilation
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).
← Previous: C++ Exception / Error Handling - MCQs
Next →: C++ Templates - MCQs
New
No Image

C++ Exception / Error Handling - MCQs

1. What is inheritance in C++? a) A mechanism to create new classes from existing classes b) A way to…

By MCQs Generator
NewLatest C++ Basics MCQs

Latest C++ Basics MCQs

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language…

By MCQs Generator
New
No Image

C++ Templates - MCQs

1. What is inheritance in C++? a) A mechanism to create new classes from existing classes b) A way to…

By MCQs Generator