C++ OOP MCQs Object-Oriented Programming

1 min read

Object-Oriented Programming (OOP) in C++ revolutionizes software design by organizing code around data (objects) rather than relying solely on functions and logic. Introduced by Bjarne Stroustrup as “C with Classes,” C++ fully supports the four fundamental pillars of OOP: encapsulation, abstraction, inheritance, and polymorphism. By utilizing access specifiers (public, private, protected), C++ enforces strict data hiding and memory security. Advanced features like virtual functions, abstract classes, and multiple inheritance give C++ immense architectural power and flexibility. Mastering C++ OOP principles is critical for building scalable, maintainable systems, preventing memory leaks through virtual destructors, and passing technical software engineering interviews.

1. What is OOP?

a) A programming paradigm based on objects
b) A procedural-only style
c) A hardware design method
d) A database language
Correct Answer: a) A programming paradigm based on objects
Explanation:
OOP organizes software around objects that combine data and behavior.

2. What is a class in C++?

a) A runtime variable
b) A blueprint for objects
c) A function name
d) A header file
Correct Answer: b) A blueprint for objects
Explanation:
A class defines the data and operations that its objects will have.

3. What is an object in C++?

a) A type definition
b) A function
c) An instance of a class
d) A macro
Correct Answer: c) An instance of a class
Explanation:
Objects are concrete instances created from a class.

4. What is encapsulation?

a) Inheriting from multiple classes
b) Using global variables
c) Writing only functions
d) Bundling data and methods together
Correct Answer: d) Bundling data and methods together
Explanation:
Encapsulation keeps related data and behavior inside a class and controls access.

5. Which access specifier is commonly used for data hiding?

a) private
b) public
c) global
d) external
Correct Answer: a) private
Explanation:
Private members are not directly accessible from outside the class.

6. What is abstraction?

a) Deleting classes
b) Exposing only essential features
c) Copying objects
d) Inlining all functions
Correct Answer: b) Exposing only essential features
Explanation:
Abstraction hides implementation details and presents a clear interface.

7. What is inheritance?

a) Copying functions manually
b) Deleting old classes
c) Deriving new classes from existing classes
d) Overloading operators
Correct Answer: c) Deriving new classes from existing classes
Explanation:
Inheritance lets a new class reuse and extend an existing class.

8. What is polymorphism?

a) One variable only
b) One function only
c) One class only
d) The same interface with different behavior
Correct Answer: d) The same interface with different behavior
Explanation:
Polymorphism allows different classes to respond to the same interface.

9. Which keyword is used to define a class in C++?

a) class
b) object
c) define
d) interface
Correct Answer: a) class
Explanation:
The class keyword introduces a class definition.

10. What is the default access specifier in a C++ class?

a) public
b) private
c) protected
d) global
Correct Answer: b) private
Explanation:
Class members are private by default unless specified otherwise.

11. What is the default access specifier in a C++ struct?

a) private
b) protected
c) public
d) hidden
Correct Answer: c) public
Explanation:
Struct members are public by default unless specified otherwise.

12. Which specifier restricts access to members within the same class only?

a) public
b) protected
c) friend
d) private
Correct Answer: d) private
Explanation:
Private members can only be accessed by members and friends of that class.

13. Which access specifier allows access in derived classes?

a) protected
b) private
c) static
d) const
Correct Answer: a) protected
Explanation:
Protected members are accessible in the class and its derived classes.

14. What is true about a member function defined inside a class body?

a) It is virtual
b) It is inline by default
c) It is static
d) It is abstract
Correct Answer: b) It is inline by default
Explanation:
Member functions defined inside the class are implicitly inline.

15. Which statement creates an object of class MyClass?

a) class MyClass;
b) MyClass();
c) MyClass obj;
d) object MyClass;
Correct Answer: c) MyClass obj;
Explanation:
A variable declaration using the class name creates an object.

16. What is the dot operator used for?

a) Dynamic allocation
b) Deleting objects
c) Scope resolution
d) Accessing object members
Correct Answer: d) Accessing object members
Explanation:
The dot operator accesses members through an object.

17. Which operator allocates dynamic memory in C++?

a) new
b) malloc
c) create
d) alloc
Correct Answer: a) new
Explanation:
new allocates memory and constructs objects in C++.

18. Which operator releases memory allocated by new?

a) free
b) delete
c) remove
d) dispose
Correct Answer: b) delete
Explanation:
delete destroys objects and releases memory allocated by new.

19. What is the name of a constructor?

a) main
b) init
c) The same as the class name
d) constructor
Correct Answer: c) The same as the class name
Explanation:
Constructors have the same name as their class.

20. What is the return type of a constructor?

a) int
b) void
c) bool
d) It has no return type
Correct Answer: d) It has no return type
Explanation:
Constructors do not specify a return type.

21. When is a constructor called?

a) When an object is created
b) When an object is destroyed
c) Only manually
d) Never
Correct Answer: a) When an object is created
Explanation:
Constructors initialize objects during creation.

22. What is a default constructor?

a) A constructor with one required argument
b) A constructor that can be called with no arguments
c) A constructor returning int
d) A deleted constructor
Correct Answer: b) A constructor that can be called with no arguments
Explanation:
A default constructor can create an object without providing arguments.

23. What does a copy constructor copy?

a) A class definition
b) A function
c) An object of the same class
d) A base class only
Correct Answer: c) An object of the same class
Explanation:
A copy constructor initializes a new object from an existing object.

24. What is the name of a destructor for class MyClass?

a) ~main
b) destroy
c) delete
d) ~MyClass
Correct Answer: d) ~MyClass
Explanation:
Destructors use the class name prefixed with a tilde.

25. When is a destructor called?

a) When an object is destroyed
b) When an object is created
c) When a class is defined
d) Never
Correct Answer: a) When an object is destroyed
Explanation:
Destructors clean up resources when object lifetime ends.

26. Can a class have multiple constructors?

a) No
b) Yes
c) Only one
d) Only if abstract
Correct Answer: b) Yes
Explanation:
Constructors can be overloaded with different parameter lists.

27. What is constructor overloading?

a) Using the same signature twice
b) Deleting constructors
c) Providing multiple constructors with different parameters
d) Inheriting constructors only
Correct Answer: c) Providing multiple constructors with different parameters
Explanation:
Overloading allows different ways to initialize objects.

28. What is a member initializer list used for?

a) Deleting members
b) Declaring namespaces
c) Defining macros
d) Initializing members before the constructor body runs
Correct Answer: d) Initializing members before the constructor body runs
Explanation:
Initializer lists initialize members directly during construction.

29. Which member usually must be initialized in a constructor initializer list?

a) A non-static const data member
b) A public data member
c) A static local variable
d) A friend function
Correct Answer: a) A non-static const data member
Explanation:
Const non-static data members need initialization before the constructor body.

30. What does the explicit keyword prevent for a constructor?

a) Inheritance
b) Implicit conversion from the constructor argument
c) Destructor execution
d) Operator overloading
Correct Answer: b) Implicit conversion from the constructor argument
Explanation:
explicit stops unintended implicit conversions during object creation.

31. What is true about an explicit constructor?

a) It allows implicit conversion
b) It is virtual
c) It requires direct initialization for that conversion
d) It cannot be called
Correct Answer: c) It requires direct initialization for that conversion
Explanation:
explicit constructors are used for direct initialization, not implicit conversion.

32. What does MyClass obj; invoke if available?

a) Destructor
b) Copy constructor
c) Assignment operator
d) Default constructor
Correct Answer: d) Default constructor
Explanation:
Creating an object without arguments uses a default constructor.

33. What does MyClass obj2(obj1); invoke?

a) Copy constructor
b) Assignment operator
c) Destructor
d) Default constructor
Correct Answer: a) Copy constructor
Explanation:
Direct initialization from another object uses the copy constructor.

34. What does obj2 = obj1; invoke when both objects already exist?

a) Constructor
b) Copy assignment operator
c) Copy constructor
d) Destructor
Correct Answer: b) Copy assignment operator
Explanation:
Assigning to an existing object uses copy assignment.

35. Which function is part of the Rule of Three?

a) Default constructor
b) Friend function
c) Copy assignment operator
d) Static member function
Correct Answer: c) Copy assignment operator
Explanation:
The Rule of Three includes destructor, copy constructor, and copy assignment.

36. Which function is added in the Rule of Five?

a) Copy constructor
b) Destructor
c) Friend operator
d) Move assignment operator
Correct Answer: d) Move assignment operator
Explanation:
The Rule of Five adds move constructor and move assignment.

37. If no user-defined constructor exists, what may the compiler generate?

a) A default constructor
b) Only a destructor
c) A virtual function
d) An abstract class
Correct Answer: a) A default constructor
Explanation:
The compiler can implicitly declare a default constructor when needed.

38. If no destructor is declared, what may the compiler generate?

a) A copy constructor only
b) A default destructor
c) A main function
d) A new operator
Correct Answer: b) A default destructor
Explanation:
The compiler provides an implicit destructor if none is declared.

39. What is the this pointer?

a) A class name
b) A global pointer
c) A pointer to the current object
d) A null pointer
Correct Answer: c) A pointer to the current object
Explanation:
Inside member functions, this points to the object on which the function is called.

40. Can a static member function access the this pointer?

a) Always
b) Only if public
c) Only if private
d) No
Correct Answer: d) No
Explanation:
Static member functions are not associated with an object instance.

41. What is true about a static data member?

a) It is shared by all objects of the class
b) It exists separately for each object
c) It is local to one function
d) It must be const
Correct Answer: a) It is shared by all objects of the class
Explanation:
Static data members belong to the class, not to individual objects.

42. How can a static member function be called?

a) Only through an object
b) Using the class name
c) Only through a pointer
d) Only in main
Correct Answer: b) Using the class name
Explanation:
Static member functions can be called without an object.

43. Where is a non-inline static data member usually defined?

a) Inside the class only
b) Inside main only
c) Outside the class definition
d) Inside a constructor
Correct Answer: c) Outside the class definition
Explanation:
Static data members typically need a separate definition outside the class.

44. What is true about a const member function?

a) It cannot read data members
b) It must be static
c) It cannot be virtual
d) It promises not to modify non-mutable members
Correct Answer: d) It promises not to modify non-mutable members
Explanation:
const member functions cannot change non-mutable member variables.

45. What does the mutable keyword allow?

a) A member to be modified in a const member function
b) A class to be inherited
c) A destructor to be virtual
d) A static member to be private
Correct Answer: a) A member to be modified in a const member function
Explanation:
mutable members can change even inside const member functions.

46. What is a friend function?

a) A member function only
b) A non-member function that can access private members
c) A constructor
d) A destructor
Correct Answer: b) A non-member function that can access private members
Explanation:
friend functions are granted access to private and protected members.

47. What does a friend class do?

a) Inherits automatically
b) Becomes a nested class
c) Can access private members of the class that declares it friend
d) Replaces the base class
Correct Answer: c) Can access private members of the class that declares it friend
Explanation:
Friendship gives another class access to private and protected members.

48. Which operator cannot be overloaded?

a) +
b) -
c) *
d) ::
Correct Answer: d) ::
Explanation:
The scope resolution operator cannot be overloaded.

49. Which operator can be overloaded?

a) +
b) ::
c) .
d) ?:
Correct Answer: a) +
Explanation:
Arithmetic operators such as + can be overloaded for class types.

50. Which operator cannot be overloaded?

a) +
b) .
c) -
d) ==
Correct Answer: b) .
Explanation:
The member access operator . cannot be overloaded.

51. How is operator<< usually overloaded for output?

a) As a member only
b) As a destructor
c) As a non-member or friend function
d) As a constructor
Correct Answer: c) As a non-member or friend function
Explanation:
The left operand is usually ostream, so a non-member is natural.

52. What is a pure virtual function?

a) A function with no name
b) A static function
c) An inline function
d) A virtual function declared with = 0
Correct Answer: d) A virtual function declared with = 0
Explanation:
A pure virtual function has no required implementation in the base class.

53. What is an abstract class?

a) A class that cannot be instantiated
b) A class with no members
c) A class that cannot be inherited
d) A class with no functions
Correct Answer: a) A class that cannot be instantiated
Explanation:
Abstract classes define interfaces and cannot create direct objects.

54. What usually makes a class abstract?

a) A private constructor
b) At least one pure virtual function
c) A destructor
d) A static member
Correct Answer: b) At least one pure virtual function
Explanation:
A class with a pure virtual function cannot be instantiated.

55. What does the virtual keyword enable?

a) Static binding
b) Compile-time polymorphism
c) Runtime polymorphism
d) Operator overloading
Correct Answer: c) Runtime polymorphism
Explanation:
virtual allows derived classes to override functions selected at runtime.

56. What does overriding mean?

a) Hiding a variable
b) Overloading a function
c) Renaming a class
d) Redefining a virtual function in a derived class
Correct Answer: d) Redefining a virtual function in a derived class
Explanation:
An override provides a derived implementation of a base virtual function.

57. Which specifier helps ensure a function overrides a base function?

a) override
b) virtual
c) final
d) static
Correct Answer: a) override
Explanation:
override makes the compiler check that the function overrides a base function.

58. What does final do when applied to a virtual function?

a) Makes it pure
b) Prevents overriding
c) Makes it static
d) Deletes it
Correct Answer: b) Prevents overriding
Explanation:
A final virtual function cannot be overridden by derived classes.

59. When is a virtual destructor especially important?

a) When no inheritance is used
b) When objects are global only
c) When deleting derived objects through base pointers
d) When no polymorphism is used
Correct Answer: c) When deleting derived objects through base pointers
Explanation:
A virtual destructor ensures the derived destructor runs.

60. What can happen if a base destructor is not virtual when deleting a derived object through a base pointer?

a) It always works
b) It improves performance
c) It throws an exception
d) It can cause undefined behavior
Correct Answer: d) It can cause undefined behavior
Explanation:
Without a virtual destructor, only the base destructor may run.

61. Which mechanism achieves dynamic polymorphism in C++?

a) Virtual functions
b) Function overloading
c) Macros
d) Templates only
Correct Answer: a) Virtual functions
Explanation:
Virtual function dispatch selects implementations at runtime.

62. Which is an example of compile-time polymorphism?

a) Virtual functions
b) Function overloading or templates
c) Destructors
d) Inheritance only
Correct Answer: b) Function overloading or templates
Explanation:
Compile-time polymorphism resolves calls during compilation.

63. What is function overloading?

a) Using the same parameter list twice
b) Using different class names
c) Using the same function name with different parameters
d) Using recursion
Correct Answer: c) Using the same function name with different parameters
Explanation:
Overloaded functions differ by parameter types or counts.

64. What does operator overloading change?

a) Operator syntax
b) Operator precedence
c) Operator associativity
d) Operator behavior for class types
Correct Answer: d) Operator behavior for class types
Explanation:
Overloading defines how operators behave for user-defined types.

65. What is true about a derived class?

a) It can inherit members from a base class
b) It cannot add new members
c) It cannot override functions
d) It must be abstract
Correct Answer: a) It can inherit members from a base class
Explanation:
Derived classes extend base classes with new or overridden members.

66. Which syntax shows public inheritance?

a) class D private B
b) class D : public B
c) class D -> B
d) class D extends B
Correct Answer: b) class D : public B
Explanation:
C++ uses a colon and access specifier for inheritance.

67. In public inheritance, public base members become what in the derived class?

a) private
b) protected
c) public
d) hidden
Correct Answer: c) public
Explanation:
Public inheritance preserves public access for base members.

68. In protected inheritance, public base members become what?

a) public
b) private
c) inaccessible
d) protected
Correct Answer: d) protected
Explanation:
Protected inheritance lowers public base members to protected.

69. In private inheritance, public base members become what?

a) private
b) public
c) protected
d) global
Correct Answer: a) private
Explanation:
Private inheritance makes inherited members private in the derived class.

70. When is a base class constructor called during derived construction?

a) After the derived constructor
b) Before the derived constructor body
c) Never
d) After the destructor
Correct Answer: b) Before the derived constructor body
Explanation:
Base subobjects are initialized before derived members and body.

71. When is a base class destructor called during destruction?

a) Before the base constructor
b) Never
c) After the derived destructor body
d) Before the derived destructor body
Correct Answer: c) After the derived destructor body
Explanation:
Destruction proceeds from derived to base.

72. What is the order of construction in single inheritance?

a) Derived then base
b) Random
c) Base destructor first
d) Base then derived
Correct Answer: d) Base then derived
Explanation:
Base classes are constructed before derived classes.

73. What is the order of destruction in single inheritance?

a) Derived then base
b) Base then derived
c) Random
d) Simultaneous
Correct Answer: a) Derived then base
Explanation:
Derived destructors run before base destructors.

74. What is multiple inheritance?

a) One base class
b) A class inherits from more than one base class
c) Multiple objects
d) Multiple constructors
Correct Answer: b) A class inherits from more than one base class
Explanation:
A derived class can have several direct bases.

75. What causes the diamond problem?

a) Single inheritance
b) No inheritance
c) Multiple inheritance paths to the same base class
d) Templates
Correct Answer: c) Multiple inheritance paths to the same base class
Explanation:
Two paths to one base can create ambiguity.

76. What helps solve the diamond problem?

a) Operator overloading
b) Encapsulation
c) Function overloading
d) Virtual inheritance
Correct Answer: d) Virtual inheritance
Explanation:
Virtual inheritance creates one shared base subobject.

77. In virtual inheritance, what exists for the virtual base?

a) One shared base subobject
b) Two duplicate subobjects
c) No base subobject
d) A private constructor
Correct Answer: a) One shared base subobject
Explanation:
Virtual inheritance avoids duplicated base parts.

78. Which keyword is used for a virtual base class?

a) shared
b) virtual
c) base
d) common
Correct Answer: b) virtual
Explanation:
The virtual keyword in the base list enables virtual inheritance.

79. What relationship does composition usually model?

a) Is-a
b) Can-be
c) Has-a
d) Was-a
Correct Answer: c) Has-a
Explanation:
Composition means one object contains another.

80. What relationship does inheritance often model?

a) Has-a
b) Uses-a
c) Contains-a
d) Is-a
Correct Answer: d) Is-a
Explanation:
A derived object is a kind of base object.

81. Which is often preferred to reuse behavior without exposing implementation?

a) Composition
b) Global variables
c) Macros
d) goto
Correct Answer: a) Composition
Explanation:
Composition can combine objects while keeping implementation hidden.

82. What is a common C++ interface-like construct?

a) A global variable
b) An abstract class with pure virtual functions
c) An enum
d) A macro
Correct Answer: b) An abstract class with pure virtual functions
Explanation:
Pure virtual functions define an interface for derived classes.

83. What is a concrete class?

a) A class that cannot be instantiated
b) A class with a pure virtual function
c) A class that can be instantiated
d) A class with no members
Correct Answer: c) A class that can be instantiated
Explanation:
A concrete class has implementations for all required functions.

84. What does Base* p = new Derived; create?

a) A syntax error
b) A pointer to Base only
c) A compile error
d) A base pointer pointing to a Derived object
Correct Answer: d) A base pointer pointing to a Derived object
Explanation:
A base pointer can refer to a derived object.

85. What is needed to call an overridden function through a base pointer?

a) A virtual function
b) A static function
c) A friend function
d) A private function
Correct Answer: a) A virtual function
Explanation:
Virtual dispatch selects the derived implementation.

86. Without virtual, calls through a base pointer use which type?

a) Derived type
b) Static pointer type
c) Dynamic type
d) Random type
Correct Answer: b) Static pointer type
Explanation:
Non-virtual calls are resolved using the pointer type.

87. What is static binding?

a) Runtime resolution
b) Dynamic dispatch
c) Compile-time resolution
d) Virtual dispatch
Correct Answer: c) Compile-time resolution
Explanation:
Static binding decides the function call during compilation.

88. What is dynamic binding?

a) Compile-time resolution
b) Inline expansion
c) Private access
d) Runtime resolution
Correct Answer: d) Runtime resolution
Explanation:
Dynamic binding selects functions while the program runs.

89. What does a virtual table usually contain?

a) Pointers to virtual functions
b) Object addresses
c) Constructors only
d) Macros
Correct Answer: a) Pointers to virtual functions
Explanation:
The vtable stores addresses of virtual functions for dispatch.

90. What is a vptr?

a) A virtual data member
b) A pointer in an object to its vtable
c) A private constructor
d) A destructor
Correct Answer: b) A pointer in an object to its vtable
Explanation:
Objects with virtual functions often contain a vptr.

91. When is a vptr typically initialized?

a) After destruction
b) Never
c) During construction
d) By delete
Correct Answer: c) During construction
Explanation:
The constructor sets up the object's vptr.

92. Can a constructor be virtual?

a) Yes always
b) Only in abstract classes
c) Only private
d) No
Correct Answer: d) No
Explanation:
Constructors cannot be virtual because the object type is being created.

93. Can a destructor be virtual?

a) Yes
b) No
c) Only if pure
d) Only if static
Correct Answer: a) Yes
Explanation:
Virtual destructors support safe polymorphic deletion.

94. Can a copy constructor be virtual?

a) Yes
b) No
c) Only protected
d) Only derived
Correct Answer: b) No
Explanation:
Constructors, including copy constructors, cannot be virtual.

95. Can a member function be both static and virtual?

a) Yes
b) Sometimes
c) No
d) Only if public
Correct Answer: c) No
Explanation:
Virtual dispatch requires an object, while static functions do not have this.

96. Can a const member function be virtual?

a) No
b) Only if private
c) Only if static
d) Yes
Correct Answer: d) Yes
Explanation:
const member functions can be virtual and overridden.

97. What is object slicing?

a) Copying only the base part when a derived object is copied as a base value
b) Deleting objects
c) Inheriting privately
d) Dynamic casting
Correct Answer: a) Copying only the base part when a derived object is copied as a base value
Explanation:
Value assignment to a base type can lose derived data.

98. How can slicing be avoided?

a) Passing objects by value
b) Using pointers or references
c) Using macros
d) Using global variables
Correct Answer: b) Using pointers or references
Explanation:
Pointers and references preserve the dynamic type.

99. What does dynamic_cast require for downcasting?

a) No RTTI
b) A private base
c) A polymorphic source type
d) A template
Correct Answer: c) A polymorphic source type
Explanation:
dynamic_cast needs virtual functions for runtime type checking.

100. What does dynamic_cast return if a pointer cast fails?

a) true
b) false
c) An exception always
d) nullptr
Correct Answer: d) nullptr
Explanation:
For pointer types, a failed dynamic_cast returns nullptr.

101. Which cast is usually safest for polymorphic downcasting?

a) dynamic_cast
b) static_cast
c) reinterpret_cast
d) C-style cast
Correct Answer: a) dynamic_cast
Explanation:
dynamic_cast checks the actual runtime type.

102. What is true about static_cast downcasting?

a) It always checks runtime type
b) It does not check runtime type
c) It cannot compile
d) It always throws
Correct Answer: b) It does not check runtime type
Explanation:
static_cast assumes the cast is valid.

103. What does RTTI stand for?

a) Real Type Iteration
b) Runtime Template Interface
c) Runtime Type Information
d) Rapid Type Initialization
Correct Answer: c) Runtime Type Information
Explanation:
RTTI provides type identity features such as dynamic_cast and typeid.

104. What can typeid provide?

a) Memory size only
b) Constructor list
c) Private access
d) Type information
Correct Answer: d) Type information
Explanation:
typeid returns a type_info object describing the type.

105. Which header is needed for typeid?

a) typeinfo
b) type
c) rtti
d) class
Correct Answer: a) typeinfo
Explanation:
The typeinfo header defines type_info.

106. What is a class with at least one virtual function called?

a) Static
b) Polymorphic
c) Abstract always
d) Final
Correct Answer: b) Polymorphic
Explanation:
Virtual functions make a class capable of runtime dispatch.

107. What is a class with a pure virtual function called?

a) Concrete
b) Sealed
c) Abstract
d) Static
Correct Answer: c) Abstract
Explanation:
Pure virtual functions prevent direct instantiation.

108. Can a pure virtual function have a definition?

a) Never
b) Only destructor
c) No
d) Yes, separately outside the class
Correct Answer: d) Yes, separately outside the class
Explanation:
A pure virtual function can still be defined explicitly.

109. Which is the correct syntax for a virtual destructor?

a) virtual ~Base() { }
b) ~virtual Base() { }
c) void ~Base() { }
d) virtual ~Base
Correct Answer: a) virtual ~Base() { }
Explanation:
The virtual keyword appears before the destructor declaration.

110. Where is the override specifier placed?

a) Before the function name
b) After the function declaration in the derived class
c) In the base class only
d) After the class name
Correct Answer: b) After the function declaration in the derived class
Explanation:
override follows the member function declarator.

111. Where can final be used?

a) Before class only
b) After namespace
c) After a virtual function or after a class name
d) Before include
Correct Answer: c) After a virtual function or after a class name
Explanation:
final can prevent overriding or inheritance.

112. What does class Derived final mean?

a) Derived cannot be instantiated
b) Derived cannot have members
c) Derived cannot override
d) Derived cannot be used as a base class
Correct Answer: d) Derived cannot be used as a base class
Explanation:
A final class cannot be inherited.

113. What does a function marked final prevent?

a) Further overriding
b) Instantiation
c) Construction
d) Class inheritance
Correct Answer: a) Further overriding
Explanation:
Derived classes cannot override a final virtual function.

114. What can happen if a derived class declares a member with the same name as a base member?

a) It always causes a runtime error
b) The base member may be hidden
c) It always causes a compile error
d) The base object is destroyed
Correct Answer: b) The base member may be hidden
Explanation:
Name hiding can shadow base class members.

115. What is name hiding?

a) Virtual dispatch
b) Overriding
c) A derived member shadows a base member
d) Inheritance
Correct Answer: c) A derived member shadows a base member
Explanation:
The derived declaration can hide same-named base declarations.

116. How can a hidden base member be accessed explicitly?

a) Using this
b) Using friend
c) Using private
d) Using Base::member
Correct Answer: d) Using Base::member
Explanation:
Scope resolution selects the base class member.

117. What can a using declaration in a derived class do?

a) Bring base overloaded functions into scope
b) Delete the base class
c) Make private members public
d) Inherit constructors always
Correct Answer: a) Bring base overloaded functions into scope
Explanation:
using Base::name can prevent hiding of overloads.

118. How are constructors inherited in C++11 and later?

a) inherit keyword
b) using Base::Base;
c) constructor extends
d) import Base
Correct Answer: b) using Base::Base;
Explanation:
A using declaration can inherit base constructors.

119. What is a nested class?

a) A class inside a namespace
b) A class inside a function
c) A class defined inside another class
d) A derived class
Correct Answer: c) A class defined inside another class
Explanation:
Nested classes are declared within the scope of another class.

120. What is true about a nested class?

a) It inherits from the outer class
b) It automatically accesses outer private data
c) It is a member variable
d) It is scoped inside the enclosing class
Correct Answer: d) It is scoped inside the enclosing class
Explanation:
A nested class name is scoped by its enclosing class.

121. What is a local class?

a) A class defined inside a function
b) A class in global scope
c) A class nested in another class
d) An abstract class
Correct Answer: a) A class defined inside a function
Explanation:
Local classes are declared within function scope.

122. Can a local class have static data members?

a) Yes always
b) No
c) Only if public
d) Only if inline
Correct Answer: b) No
Explanation:
Local classes cannot contain static data members.

123. Can a local class define member functions outside the class body?

a) Yes
b) Sometimes
c) No
d) Only if virtual
Correct Answer: c) No
Explanation:
Local class member functions must be defined inside the class.

124. Can a local class have constructors and destructors?

a) No
b) Only constructors
c) Only destructors
d) Yes
Correct Answer: d) Yes
Explanation:
Local classes may define constructors and destructors inside the class.

125. Can a class be declared without being defined?

a) Yes, with a forward declaration
b) No
c) Only for struct
d) Only for union
Correct Answer: a) Yes, with a forward declaration
Explanation:
class MyClass; declares an incomplete type.

126. What is a forward declaration useful for?

a) Full object definition
b) Reducing dependencies and incomplete type usage
c) Deleting classes
d) Inheriting privately
Correct Answer: b) Reducing dependencies and incomplete type usage
Explanation:
Forward declarations can reduce header coupling.

127. What is an incomplete type?

a) A fully defined class
b) A class with a constructor
c) A declared but not defined class
d) An enumeration
Correct Answer: c) A declared but not defined class
Explanation:
Incomplete types lack a full definition.

128. Can you create an object of an incomplete type?

a) Yes
b) Only if private
c) Only if static
d) No
Correct Answer: d) No
Explanation:
Objects require a complete class definition.

129. Can you declare a pointer to an incomplete type?

a) Yes
b) No
c) Only if virtual
d) Only if derived
Correct Answer: a) Yes
Explanation:
Pointers can refer to incomplete types.

130. What is the PIMPL idiom?

a) Public inheritance
b) Pointer to implementation to hide details
c) Private main
d) Polymorphic interface
Correct Answer: b) Pointer to implementation to hide details
Explanation:
PIMPL separates interface from implementation.

131. What does RAII stand for?

a) Random Allocation Interface
b) Recursive Initialization Access
c) Resource Acquisition Is Initialization
d) Runtime API Integration
Correct Answer: c) Resource Acquisition Is Initialization
Explanation:
RAII ties resource management to object lifetime.

132. What does RAII tie resource lifetime to?

a) Functions
b) Global variables
c) Macros
d) Object lifetime
Correct Answer: d) Object lifetime
Explanation:
Resources are acquired in constructors and released in destructors.

133. Which is a smart pointer type?

a) unique_ptr
b) array
c) string literal
d) enum
Correct Answer: a) unique_ptr
Explanation:
unique_ptr manages dynamically allocated memory.

134. What kind of ownership does unique_ptr provide?

a) Shared
b) Exclusive
c) No ownership
d) Global
Correct Answer: b) Exclusive
Explanation:
Only one unique_ptr should own a resource.

135. What does shared_ptr provide?

a) Exclusive ownership
b) No ownership
c) Reference-counted shared ownership
d) Stack allocation
Correct Answer: c) Reference-counted shared ownership
Explanation:
shared_ptr deletes the object when the last owner is gone.

136. What is weak_ptr?

a) An owning pointer
b) A deleting pointer
c) A reference-count incrementing pointer
d) A non-owning observer
Correct Answer: d) A non-owning observer
Explanation:
weak_ptr observes shared_ptr without extending ownership.

137. What helps break circular references with shared_ptr?

a) weak_ptr
b) new
c) delete
d) malloc
Correct Answer: a) weak_ptr
Explanation:
weak_ptr avoids increasing the reference count.

138. What does make_shared do?

a) Deletes a shared_ptr
b) Creates a shared_ptr efficiently
c) Casts pointers
d) Overloads operators
Correct Answer: b) Creates a shared_ptr efficiently
Explanation:
make_shared allocates the object and control block together.

139. What is move semantics?

a) Copying objects
b) Inheritance
c) Transferring resources from one object to another
d) Operator overloading
Correct Answer: c) Transferring resources from one object to another
Explanation:
Move semantics avoid unnecessary copying.

140. How is an rvalue reference declared?

a) &
b) *
c) const
d) &&
Correct Answer: d) &&
Explanation:
Rvalue references use double ampersand.

141. What does std::move do?

a) Casts an object to an rvalue reference
b) Deletes an object
c) Allocates memory
d) Copies an object
Correct Answer: a) Casts an object to an rvalue reference
Explanation:
std::move enables move operations but does not move by itself.

142. What does a move constructor do?

a) Copies always
b) Transfers resources from the source object
c) Prevents construction
d) Is virtual
Correct Answer: b) Transfers resources from the source object
Explanation:
Move constructors take resources from rvalue sources.

143. What state should a moved-from object usually be in?

a) Always invalid
b) Deleted automatically
c) Valid but unspecified
d) Const
Correct Answer: c) Valid but unspecified
Explanation:
Moved-from objects should be safe to destroy or assign.

144. What is a common problem caused by shallow copying?

a) No effect
b) Faster always
c) No pointers
d) Double deletion
Correct Answer: d) Double deletion
Explanation:
Two objects may delete the same resource.

145. What does a deep copy do?

a) Copies owned resources
b) Copies pointers only
c) Deletes objects
d) Moves resources
Correct Answer: a) Copies owned resources
Explanation:
Deep copying duplicates the underlying resource.

146. What does the copy-and-swap idiom help implement?

a) Constructors only
b) Copy assignment safely
c) Destructors only
d) Inheritance
Correct Answer: b) Copy assignment safely
Explanation:
It uses a temporary copy and swap to provide strong exception safety.

147. What is a swap member function used for?

a) Allocation
b) Deletion
c) Exchanging object states
d) Inheritance
Correct Answer: c) Exchanging object states
Explanation:
swap exchanges internals of two objects efficiently.

148. What does noexcept indicate?

a) Virtual
b) Abstract
c) Private
d) The function is not expected to throw
Correct Answer: d) The function is not expected to throw
Explanation:
noexcept can enable optimizations and safer move operations.

149. Why mark move operations noexcept?

a) To enable optimizations and safe container behavior
b) To make them virtual
c) To hide them
d) To require heap allocation
Correct Answer: a) To enable optimizations and safe container behavior
Explanation:
Containers may prefer noexcept move operations.

150. What is a functor?

a) A function object
b) A class with only a destructor
c) A pointer
d) A macro
Correct Answer: b) A function object
Explanation:
A functor is an object that can be called like a function.

151. What is a lambda expression?

a) A class only
b) A pointer
c) An anonymous function object
d) A loop
Correct Answer: c) An anonymous function object
Explanation:
Lambdas create callable objects inline.

152. What does an empty lambda capture [] mean?

a) Capture all by reference
b) Capture this
c) Capture all by value
d) Capture nothing
Correct Answer: d) Capture nothing
Explanation:
An empty capture list uses no local variables.

153. What does [&] capture mean?

a) Capture by reference
b) Capture nothing
c) Capture by value
d) Capture globals only
Correct Answer: a) Capture by reference
Explanation:
All used local variables are captured by reference.

154. What does [=] capture mean?

a) Capture by reference
b) Capture by value
c) Capture nothing
d) Capture this only
Correct Answer: b) Capture by value
Explanation:
All used local variables are copied into the lambda.

155. Can a lambda be stored in std::function?

a) No
b) Only if it has no parameters
c) Yes, if signatures are compatible
d) Only if static
Correct Answer: c) Yes, if signatures are compatible
Explanation:
std::function can hold callable objects.

156. What is a generic lambda?

a) A lambda with no parameters
b) A lambda using int only
c) A C++03 lambda
d) A lambda using auto parameters
Correct Answer: d) A lambda using auto parameters
Explanation:
Generic lambdas accept arguments of deduced types.

157. What is a template?

a) A generic blueprint for types or functions
b) An object
c) A class only
d) A variable
Correct Answer: a) A generic blueprint for types or functions
Explanation:
Templates allow code parameterized by types.

158. What do templates primarily support?

a) Runtime polymorphism
b) Compile-time generic programming
c) Inheritance only
d) File input
Correct Answer: b) Compile-time generic programming
Explanation:
Templates generate type-specific code during compilation.

159. Which keyword begins a template definition?

a) generic
b) type
c) template
d) virtual
Correct Answer: c) template
Explanation:
The template keyword introduces template parameters.

160. Which is correct class template syntax?

a) template class <T> C
b) class template T
c) template class T
d) template <typename T> class C { }
Correct Answer: d) template <typename T> class C { }
Explanation:
Template parameters appear in angle brackets after template.

161. Can templates be overloaded?

a) Yes
b) No
c) Only class templates
d) Only function templates
Correct Answer: a) Yes
Explanation:
Different template or parameter combinations can be overloaded.

162. What is template specialization?

a) A general template
b) A custom definition for a specific type
c) Inheritance
d) A macro
Correct Answer: b) A custom definition for a specific type
Explanation:
Specializations tailor templates for particular arguments.

163. What does SFINAE mean?

a) Runtime inheritance
b) Syntax extension
c) Substitution failure is not an error
d) Static inheritance
Correct Answer: c) Substitution failure is not an error
Explanation:
Failed template substitution removes candidates instead of causing an error.

164. What does constexpr enable?

a) Runtime loops
b) Virtual calls
c) Abstract classes
d) Compile-time constant evaluation
Correct Answer: d) Compile-time constant evaluation
Explanation:
constexpr functions and variables can be evaluated at compile time.

165. Can a constructor be constexpr?

a) Yes, if requirements are met
b) No
c) Only in unions
d) Only in enums
Correct Answer: a) Yes, if requirements are met
Explanation:
constexpr constructors allow compile-time object initialization.

166. What is an enum class?

a) An unscoped enum
b) A scoped enumeration
c) A class with inheritance
d) A template
Correct Answer: b) A scoped enumeration
Explanation:
enum class keeps enumerators in the enum scope.

167. What does a namespace do?

a) Allocates memory
b) Creates objects
c) Groups identifiers
d) Deletes symbols
Correct Answer: c) Groups identifiers
Explanation:
Namespaces organize names and avoid collisions.

168. What can using namespace std; cause?

a) Safer code automatically
b) No effect
c) Faster compilation always
d) Name collisions
Correct Answer: d) Name collisions
Explanation:
Importing many names can create ambiguity.

169. What is the scope resolution operator?

a) ::
b) .
c) ->
d) &
Correct Answer: a) ::
Explanation:
It accesses names in namespaces, classes, and global scope.

170. What does the -> operator do?

a) Accesses a member through an object
b) Accesses a member through a pointer
c) Resolves scope
d) Takes an address
Correct Answer: b) Accesses a member through a pointer
Explanation:
Arrow combines dereference and member access.

171. What is a pointer-to-member?

a) A pointer to an object
b) A pointer to a function only
c) A pointer to a member of a class
d) A global pointer
Correct Answer: c) A pointer to a member of a class
Explanation:
It points to a member rather than an object address.

172. Which operator accesses a member using an object and a pointer-to-member?

a) ->*
b) .
c) ::
d) .*
Correct Answer: d) .*
Explanation:
The .* operator applies a pointer-to-member to an object.

173. Which operator accesses a member using an object pointer and a pointer-to-member?

a) ->*
b) .*
c) ::
d) &
Correct Answer: a) ->*
Explanation:
The ->* operator applies a pointer-to-member through a pointer.

174. What is type erasure?

a) Removing all types
b) Hiding the concrete type behind an interface
c) Deleting classes
d) Inheritance
Correct Answer: b) Hiding the concrete type behind an interface
Explanation:
Type erasure stores different types behind a common interface.

175. What concept does std::function demonstrate?

a) Inheritance
b) Slicing
c) Type erasure
d) Friendship
Correct Answer: c) Type erasure
Explanation:
std::function can hold many callable types with a signature.

176. What is design by contract?

a) Syntax checking
b) Memory allocation
c) Loop optimization
d) Specifying obligations for interfaces
Correct Answer: d) Specifying obligations for interfaces
Explanation:
Contracts define preconditions, postconditions, and invariants.

177. What is SOLID?

a) A set of OOP design principles
b) A compiler
c) A loop
d) A data type
Correct Answer: a) A set of OOP design principles
Explanation:
SOLID guides maintainable object-oriented design.

178. What does the Single Responsibility Principle mean?

a) Many responsibilities
b) A class should have one reason to change
c) No functions
d) Global data
Correct Answer: b) A class should have one reason to change
Explanation:
Each class should focus on one responsibility.

179. What does the Open/Closed Principle mean?

a) Modify only
b) Closed only
c) Open for extension, closed for modification
d) No classes
Correct Answer: c) Open for extension, closed for modification
Explanation:
Behavior can be extended without changing existing code.

180. What does the Liskov Substitution Principle mean?

a) No inheritance
b) Derived cannot replace base
c) No virtual functions
d) Derived objects should be usable through base interfaces
Correct Answer: d) Derived objects should be usable through base interfaces
Explanation:
Derived classes must preserve base class expectations.

181. What does the Interface Segregation Principle suggest?

a) Avoid large, forced interfaces
b) Use one class only
c) Avoid inheritance
d) Use globals
Correct Answer: a) Avoid large, forced interfaces
Explanation:
Clients should not depend on unneeded methods.

182. What does the Dependency Inversion Principle suggest?

a) Depend on concrete classes
b) Depend on abstractions
c) Avoid classes
d) Use loops
Correct Answer: b) Depend on abstractions
Explanation:
High-level modules should rely on stable interfaces.

183. What is tight coupling?

a) Low dependency
b) No relationship
c) Strong dependency between classes
d) Abstraction
Correct Answer: c) Strong dependency between classes
Explanation:
Tightly coupled classes are hard to change independently.

184. What is loose coupling?

a) High dependency
b) No classes
c) Hard to change
d) Components are relatively independent
Correct Answer: d) Components are relatively independent
Explanation:
Loose coupling improves maintainability.

185. What is high cohesion?

a) Related responsibilities are kept together
b) Responsibilities are scattered
c) Global variables
d) Low abstraction
Correct Answer: a) Related responsibilities are kept together
Explanation:
Cohesive classes have focused responsibilities.

186. What is low cohesion?

a) Focused responsibilities
b) Unrelated responsibilities together
c) No functions
d) Inline functions
Correct Answer: b) Unrelated responsibilities together
Explanation:
Low cohesion makes classes harder to understand.

187. Which is an example of has-a?

a) Dog inherits Animal
b) Car inherits Vehicle
c) Car contains Engine
d) main calls function
Correct Answer: c) Car contains Engine
Explanation:
Containment models ownership or composition.

188. Which is an example of is-a?

a) Contains
b) Uses
c) Friend
d) Square inherits Shape
Correct Answer: d) Square inherits Shape
Explanation:
Inheritance expresses a subtype relationship.

189. What does uses-a mean?

a) One class uses another through methods or parameters
b) Inheritance
c) Containment
d) Private access
Correct Answer: a) One class uses another through methods or parameters
Explanation:
Uses relationships are often temporary dependencies.

190. What is aggregation?

a) Exclusive ownership
b) Association without necessarily owning the object
c) Inheritance
d) Template
Correct Answer: b) Association without necessarily owning the object
Explanation:
Aggregation is a weaker has-a relationship than composition.

191. What is composition?

a) Weak association
b) No lifetime control
c) Strong has-a with lifetime ownership
d) Public inheritance
Correct Answer: c) Strong has-a with lifetime ownership
Explanation:
Composed parts often live and die with the owner.

192. What is multiple dispatch?

a) One function only
b) Compile-time binding
c) Overloading
d) Function selection based on multiple objects
Correct Answer: d) Function selection based on multiple objects
Explanation:
C++ natively dispatches on one dynamic type.

193. What does a pure interface class often contain?

a) Only pure virtual functions
b) Global data
c) Macros
d) Constructors only
Correct Answer: a) Only pure virtual functions
Explanation:
Interfaces often avoid state.

194. What is an abstract base class used for?

a) Direct instantiation
b) Providing a common interface for derived classes
c) Preventing polymorphism
d) Templates only
Correct Answer: b) Providing a common interface for derived classes
Explanation:
Abstract bases define shared behavior.

195. What is a covariant return type?

a) Same return only
b) void only
c) An override returning a derived type
d) No return
Correct Answer: c) An override returning a derived type
Explanation:
Overrides can return more specific pointer or reference types.

196. What does covariant return require?

a) No inheritance
b) Value return
c) Static return
d) Related base and derived return types with pointers or references
Correct Answer: d) Related base and derived return types with pointers or references
Explanation:
Covariance works with related class hierarchies.

197. What is object lifetime?

a) From construction to destruction
b) Compilation time
c) Link time
d) Include time
Correct Answer: a) From construction to destruction
Explanation:
Lifetime is the period an object exists.

198. What is stack allocation?

a) Dynamic new
b) Automatic storage
c) Heap storage
d) Global only
Correct Answer: b) Automatic storage
Explanation:
Stack objects are created and destroyed automatically.

199. What is heap allocation?

a) Stack storage
b) Automatic storage
c) Dynamic allocation via new
d) Register storage
Correct Answer: c) Dynamic allocation via new
Explanation:
Heap objects require manual or smart pointer management.

200. What is a memory leak?

a) Freeing memory
b) Running destructors
c) Stack allocation
d) Allocated memory that is not freed
Correct Answer: d) Allocated memory that is not freed
Explanation:
Leaks exhaust memory over time.

201. What is a dangling pointer?

a) A pointer to freed or deleted memory
b) A safe pointer
c) A null pointer
d) A stack pointer
Correct Answer: a) A pointer to freed or deleted memory
Explanation:
Dereferencing it causes undefined behavior.

202. What is double free?

a) Freeing once
b) Deleting the same dynamic memory twice
c) Creating two objects
d) Calling two constructors
Correct Answer: b) Deleting the same dynamic memory twice
Explanation:
Double free corrupts memory.

203. What is undefined behavior?

a) Guaranteed result
b) Standard-specified result
c) No guaranteed result
d) Inline expansion
Correct Answer: c) No guaranteed result
Explanation:
The standard imposes no requirements on undefined behavior.

204. What is a vtable used for?

a) Data alignment
b) Constructor lists
c) Memory pools
d) Virtual function dispatch
Correct Answer: d) Virtual function dispatch
Explanation:
The vtable enables runtime selection of overrides.

205. Which is valid virtual inheritance syntax?

a) class B : virtual public A
b) class B virtual A
c) class B : shared A
d) class B extends A
Correct Answer: a) class B : virtual public A
Explanation:
virtual in the base list creates a virtual base.

206. How can ambiguous member access in multiple inheritance be resolved?

a) Deleting a class
b) Scope resolution or virtual inheritance
c) Friendship
d) Static members
Correct Answer: b) Scope resolution or virtual inheritance
Explanation:
Qualify the base or share one base subobject.

207. What is true about object layout with a virtual base?

a) It is always simpler
b) It has no pointers
c) It may include a virtual base pointer
d) It has no base
Correct Answer: c) It may include a virtual base pointer
Explanation:
Implementations need to locate the shared base.

208. What is a polymorphic base class?

a) A class with no virtual functions
b) A class with only static members
c) A class with a private destructor
d) A class with at least one virtual function
Correct Answer: d) A class with at least one virtual function
Explanation:
Polymorphic classes support dynamic dispatch.

209. What is a final class?

a) A class that cannot be inherited
b) A class that cannot be instantiated
c) A class with no members
d) A class with no constructor
Correct Answer: a) A class that cannot be inherited
Explanation:
final prevents derivation.

210. What is the C++ keyword closest to a sealed class?

a) abstract
b) final
c) friend
d) static
Correct Answer: b) final
Explanation:
C++ uses final to prevent inheritance.

211. What is a protected destructor used for?

a) Preventing all deletion
b) Making a class abstract
c) Controlling deletion through derived classes or factories
d) Preventing inheritance
Correct Answer: c) Controlling deletion through derived classes or factories
Explanation:
Protected destructors restrict who can destroy objects.

212. What is a private constructor used for?

a) Making objects public
b) Allowing inheritance
c) Making a class abstract
d) Preventing direct object creation
Correct Answer: d) Preventing direct object creation
Explanation:
Private constructors restrict instantiation.

213. What does the Singleton pattern often use?

a) A private constructor and static accessor
b) A public constructor
c) No class
d) Multiple instances
Correct Answer: a) A private constructor and static accessor
Explanation:
Singleton controls creation of one instance.

214. What does the Factory pattern do?

a) Uses new everywhere directly
b) Creates objects without exposing exact creation logic
c) Deletes objects only
d) Sorts data
Correct Answer: b) Creates objects without exposing exact creation logic
Explanation:
Factories encapsulate object creation.

215. What does the Observer pattern do?

a) Manages memory
b) Sorts arrays
c) Notifies dependent objects about changes
d) Reads files
Correct Answer: c) Notifies dependent objects about changes
Explanation:
Observers subscribe to subject state changes.

216. What does the Adapter pattern do?

a) Prevents conversion
b) Deletes interfaces
c) Creates loops
d) Converts one interface into another
Correct Answer: d) Converts one interface into another
Explanation:
Adapters make incompatible interfaces work together.

217. What does the Decorator pattern do?

a) Adds behavior dynamically
b) Deletes objects
c) Prevents polymorphism
d) Uses globals
Correct Answer: a) Adds behavior dynamically
Explanation:
Decorators wrap objects to extend functionality.

218. What does the Strategy pattern provide?

a) A fixed algorithm
b) Interchangeable algorithms
c) No classes
d) Database access
Correct Answer: b) Interchangeable algorithms
Explanation:
Strategy selects behavior at runtime.

219. What does the Command pattern do?

a) Creates loops
b) Manages memory
c) Encapsulates a request as an object
d) Reads files
Correct Answer: c) Encapsulates a request as an object
Explanation:
Commands can be queued, logged, or undone.

220. What does the Template Method pattern do?

a) Avoids inheritance
b) Uses random steps
c) Uses globals
d) Defines an algorithm skeleton in a base class
Correct Answer: d) Defines an algorithm skeleton in a base class
Explanation:
Derived classes override selected steps.

221. What does comparing two pointers usually compare?

a) Addresses
b) Object contents
c) Constructors
d) Destructors
Correct Answer: a) Addresses
Explanation:
Pointer comparison checks location, not value.

222. What operator is often overloaded to compare object contents?

a) new
b) operator==
c) delete
d) constructor
Correct Answer: b) operator==
Explanation:
Equality can be defined for class types.

223. What is copy elision?

a) Copying always
b) Moving only
c) Compiler omission of a copy operation
d) Destructor call
Correct Answer: c) Compiler omission of a copy operation
Explanation:
The compiler can avoid creating temporary copies.

224. What does return value optimization do?

a) Prevents return
b) Forces copies
c) Uses move only
d) Reduces temporary objects
Correct Answer: d) Reduces temporary objects
Explanation:
RVO constructs return values directly where needed.

225. What is a temporary object?

a) An unnamed object created by an expression
b) A global object
c) A static object
d) A constructor
Correct Answer: a) An unnamed object created by an expression
Explanation:
Temporaries are often created during evaluation.

226. Can a const reference extend the lifetime of a temporary?

a) No
b) Yes
c) Only through pointers
d) Only if virtual
Correct Answer: b) Yes
Explanation:
A const lvalue reference can bind to a temporary.

227. What is a conversion constructor?

a) A destructor
b) A friend
c) A constructor that can convert from another type
d) A static function
Correct Answer: c) A constructor that can convert from another type
Explanation:
It creates an object from a compatible argument.

228. What is a risk of conversion constructors?

a) None
b) Speed
c) Syntax
d) Implicit unwanted conversions
Correct Answer: d) Implicit unwanted conversions
Explanation:
Implicit conversions can cause surprising behavior.

229. How can implicit constructor conversion be prevented?

a) explicit
b) virtual
c) static
d) friend
Correct Answer: a) explicit
Explanation:
explicit stops implicit conversion during initialization.

230. What is a user-defined conversion operator?

a) A constructor
b) operator Type()
c) A friend
d) A destructor
Correct Answer: b) operator Type()
Explanation:
It converts an object to another type.

231. What does operator int() do?

a) Deletes an object
b) Constructs an object
c) Converts the object to int
d) Inherits from int
Correct Answer: c) Converts the object to int
Explanation:
Conversion operators define type conversion.

232. What does an explicit conversion operator prevent?

a) All conversions
b) Construction
c) Inheritance
d) Implicit conversion in many contexts
Correct Answer: d) Implicit conversion in many contexts
Explanation:
explicit requires a direct cast or initialization.

233. What does = default mean for a function?

a) Ask the compiler to generate the default implementation
b) Delete the function
c) Make it virtual
d) Make it private
Correct Answer: a) Ask the compiler to generate the default implementation
Explanation:
Defaulted functions use compiler-generated behavior.

234. What does = delete mean for a function?

a) Generate default
b) Prevent use of the function
c) Make it virtual
d) Make it inline
Correct Answer: b) Prevent use of the function
Explanation:
Deleted functions cause compile errors when used.

235. Why delete a copy constructor?

a) To allow copying
b) To improve speed
c) To prevent copying
d) To enable inheritance
Correct Answer: c) To prevent copying
Explanation:
Deleted copy constructors make objects non-copyable.

236. What is a special member function?

a) main
b) A helper function
c) A friend function
d) A constructor, destructor, copy, or move operation
Correct Answer: d) A constructor, destructor, copy, or move operation
Explanation:
Special members control object lifecycle operations.

237. What is aggregate initialization?

a) Initializing an aggregate with braces
b) Using constructors only
c) Inheritance
d) Dynamic allocation
Correct Answer: a) Initializing an aggregate with braces
Explanation:
Aggregates can be initialized element by element.

238. What does POD historically stand for?

a) Pointer Object Data
b) Plain Old Data
c) Public Object Definition
d) Polymorphic Object Data
Correct Answer: b) Plain Old Data
Explanation:
POD types have simple layout and copying rules.

239. What is a trivially copyable type?

a) It requires virtual functions
b) It cannot be copied
c) It can be copied by copying its object representation
d) It must have a destructor
Correct Answer: c) It can be copied by copying its object representation
Explanation:
Trivially copyable types can be moved as bytes.

240. What is a standard-layout type?

a) A type with no members
b) A type with virtual functions
c) An abstract type
d) A type with C-compatible layout
Correct Answer: d) A type with C-compatible layout
Explanation:
Standard layout helps interoperability with C.

241. What is a benefit of encapsulation?

a) Controlled access to data
b) No functions
c) Global state
d) No classes
Correct Answer: a) Controlled access to data
Explanation:
Encapsulation protects internal state.

242. What is a benefit of abstraction?

a) Exposing all internals
b) Simplifying how code is used
c) Increasing coupling
d) Preventing inheritance
Correct Answer: b) Simplifying how code is used
Explanation:
Abstraction hides complexity behind interfaces.

243. What is a benefit of inheritance?

a) No reuse
b) Duplication
c) Code reuse and polymorphism
d) No classes
Correct Answer: c) Code reuse and polymorphism
Explanation:
Inheritance shares behavior across related classes.

244. What is a benefit of polymorphism?

a) Fixed behavior
b) No interface
c) Compile-only behavior
d) Different implementations through a common interface
Correct Answer: d) Different implementations through a common interface
Explanation:
Polymorphism enables flexible, extensible designs.

245. What is a member function?

a) A function belonging to a class
b) A global function
c) A macro
d) A variable
Correct Answer: a) A function belonging to a class
Explanation:
Member functions define class behavior.

246. What is a data member?

a) A function
b) A variable inside a class
c) A loop
d) A namespace
Correct Answer: b) A variable inside a class
Explanation:
Data members store object state.

247. What is a class invariant?

a) A syntax rule
b) A header file
c) A condition that should hold for a valid object
d) A compiler flag
Correct Answer: c) A condition that should hold for a valid object
Explanation:
Invariants preserve object consistency.

248. What is const correctness?

a) Avoiding const
b) Using const only globally
c) Random const usage
d) Using const to enforce immutability
Correct Answer: d) Using const to enforce immutability
Explanation:
const correctness improves safety and clarity.

249. What is true about a const object?

a) Its non-mutable state cannot be modified
b) It has no destructor
c) It is abstract
d) It is global
Correct Answer: a) Its non-mutable state cannot be modified
Explanation:
const objects cannot be changed through non-mutable members.

250. What is true about a const member function?

a) It modifies the object
b) It promises not to modify object state
c) It is virtual only
d) It is static only
Correct Answer: b) It promises not to modify object state
Explanation:
const member functions support const correctness.
← Previous: C++ Memory Management MCQs
Next →: C++ Pointers and References - MCQs
New
No Image

C++ Templates - MCQs

1. What is OOP? a) A programming paradigm based on objects b) A procedural-only style c) A hardware design method…

By MCQs Generator
New
No Image

C++ Arrays / Lists - MCQs

1. What is OOP? a) A programming paradigm based on objects b) A procedural-only style c) A hardware design method…

By MCQs Generator
NewC++ Memory Management MCQs

C++ Memory Management MCQs

Memory management in C++ empowers developers with direct control over hardware resources, primarily through the allocation and deallocation of memory…

By MCQs Generator