Class and Objects in C ++ Programming MCQ Questions and Answers

1. What is the default access specifier for members of a class in C++?
A) public
B) protected
C) private
D) internal
Answer: C) private

2. Which of the following will be called first when an object is created?
A) Destructor
B) Member function
C) Constructor
D) Copy assignment operator
Answer: C) Constructor

3. Which statement about a default constructor is true?
A) It must have at least one parameter.
B) It cannot be user-defined.
C) It is only provided if no constructors are declared.
D) It can be implicitly provided by the compiler if no constructors are declared.
Answer: D) It can be implicitly provided by the compiler if no constructors are declared.

4. Which of the following correctly declares a constructor for class A?
A) void A() {}
B) A::A() {}
C) A() {}
D) A::~A() {}
Answer: C) A() {}

5. What does the this pointer inside a non-static member function of class C represent?
A) The class type C
B) The pointer to the function
C) Pointer to the calling object
D) Address of the member function table
Answer: C) Pointer to the calling object

6. Which of the following is true about static data members in a class?
A) Each object has its own copy.
B) They cannot be private.
C) They are destroyed with each object.
D) They are shared by all objects of the class.
Answer: D) They are shared by all objects of the class.

7. What is the purpose of a copy constructor?
A) To assign values after object creation.
B) To delete an object.
C) To initialize a new object from an existing object.
D) To overload operators.
Answer: C) To initialize a new object from an existing object.

8. Which signature declares a copy constructor for class B?
A) B(B &b) {}
B) B(const B &b) {}
C) B(B* b) {}
D) B(B b) {}
Answer: B) B(const B &b) {}

9. What problem does a shallow copy cause when objects manage dynamic memory?
A) Faster execution
B) Safer memory usage
C) Double free or dangling pointer issues
D) None — shallow copy always safe
Answer: C) Double free or dangling pointer issues

10. To prevent copying of objects, which C++11 feature is used?
A) static keyword
B) volatile keyword
C) = delete on copy constructor and copy assignment
D) friend keyword
Answer: C) = delete on copy constructor and copy assignment

11. Which operator should be overloaded to allow cout << obj for a class X?
A) operator+
B) operator=
C) operator<< (stream insertion)
D) operator>>
Answer: C) operator<< (stream insertion)

12. What is the effect of declaring a member function as const?
A) It cannot access any members.
B) It can modify only static members.
C) It promises not to modify the object’s non-mutable members.
D) It becomes a static member.
Answer: C) It promises not to modify the object’s non-mutable members.

13. Which keyword allows a data member to be modified even inside a const member function?
A) volatile
B) static
C) register
D) mutable
Answer: D) mutable

14. What does the friend keyword do for a function in relation to a class?
A) Makes the function inline.
B) Makes the function a member.
C) Gives the function access to private and protected members.
D) Prevents calling the function from outside.
Answer: C) Gives the function access to private and protected members.

15. Which of these is true about destructors?
A) They can be overloaded.
B) They can take parameters.
C) Only one destructor per class, with no parameters, is allowed.
D) They are called before constructors.
Answer: C) Only one destructor per class, with no parameters, is allowed.

16. What happens if you forget to define a destructor for a class that allocates dynamic memory?
A) Compiler will free memory automatically.
B) Program will refuse to compile.
C) Memory leak may occur.
D) Memory is stored on stack automatically.
Answer: C) Memory leak may occur.

17. Which of the following statements about inline member functions is correct?
A) They cannot be defined inside class body.
B) Member functions defined inside class definition are implicitly inline.
C) inline prevents function from being called.
D) Inline functions must be static.
Answer: B) Member functions defined inside class definition are implicitly inline.

18. What is object slicing?
A) Cutting an object into pieces.
B) Assigning a derived-class object to a base-class object by value, losing derived parts.
C) Passing object by pointer.
D) Removing private members at compile time.
Answer: B) Assigning a derived-class object to a base-class object by value, losing derived parts.

19. Which mechanism avoids object slicing when passing polymorphic objects?
A) Pass by value
B) Use static_cast
C) Pass by pointer or reference to base class
D) Use auto keyword
Answer: C) Pass by pointer or reference to base class

20. What is the default behavior of compiler-generated assignment operator?
A) Deep copy for pointers
B) Moves resources by default
C) Performs member-wise assignment (shallow copy)
D) Throws exception
Answer: C) Performs member-wise assignment (shallow copy)

21. Which of the following best describes RAII (Resource Acquisition Is Initialization)?
A) Resources are acquired in global functions.
B) Resources are never released.
C) Resource lifetime is tied to object lifetime (acquire in constructor, release in destructor).
D) Uses garbage collector to free memory.
Answer: C) Resource lifetime is tied to object lifetime (acquire in constructor, release in destructor).

22. Which is the correct way to allocate an object of class D on the free store (heap)?
A) D obj;
B) D obj = new D;
C) D* p = new D;
D) D p = (D*)malloc(sizeof(D));
Answer: C) D* p = new D;

23. Which statement properly deallocates an object allocated with new?
A) free(p);
B) delete[] p;
C) delete p;
D) p = nullptr;
Answer: C) delete p;

24. For an array allocated as new Type[n], which deallocation is correct?
A) delete p;
B) delete[] p;
C) free(p);
D) delete p[n];
Answer: B) delete[] p;

25. Which of the following makes a member function callable without an object instance?
A) virtual
B) const
C) friend
D) static
Answer: D) static

26. Can a static member function access non-static members directly?
A) Yes, always.
B) Yes, if object passed as parameter.
C) No — static functions have no this and cannot access non-static members directly.
D) Only if qualified by class name.
Answer: C) No — static functions have no this and cannot access non-static members directly.

27. Which of the following uses the member initializer list correctly for class P with member int x?
A) P() { x = 0; }
B) P() : x(0) {}
C) Both A and B are valid, but initializer list is preferred for certain cases.
D) P() -> x(0) {}
Answer: C) Both A and B are valid, but initializer list is preferred for certain cases.

28. Why is initializer list preferred for initializing const or reference data members?
A) It’s shorter to type.
B) It allows late assignment.
C) Const and reference members must be initialized at construction and cannot be assigned later.
D) It avoids calling constructors.
Answer: C) Const and reference members must be initialized at construction and cannot be assigned later.

29. Which is true about virtual destructors?
A) They make object creation faster.
B) They’re required for all classes.
C) They ensure derived class destructors run when deleting via base pointer.
D) They cannot be pure virtual.
Answer: C) They ensure derived class destructors run when deleting via base pointer.

30. What is a pure virtual function?
A) A function that is static.
B) A virtual function with no implementation declared by = 0, making the class abstract.
C) Non-virtual member that cannot be overridden.
D) A virtual function that returns void.
Answer: B) A virtual function with no implementation declared by = 0, making the class abstract.

31. Which is a correct way to prevent object slicing while storing objects of different derived types?
A) Use vector of base objects
B) Use copies of derived objects
C) Use pointers (or smart pointers) to base class in containers
D) Store raw bytes of derived object
Answer: C) Use pointers (or smart pointers) to base class in containers

32. What is the recommended replacement in modern C++ for raw owning pointers created by new?
A) void*
B) std::vector
C) Smart pointers like std::unique_ptr or std::shared_ptr
D) malloc/free
Answer: C) Smart pointers like std::unique_ptr or std::shared_ptr

33. Which of the following is true about std::unique_ptr<T>?
A) It allows multiple shared owners.
B) It copies by default.
C) It enforces unique ownership and cannot be copied.
D) It is thread-safe by default for all operations.
Answer: C) It enforces unique ownership and cannot be copied.

34. What is the main difference between aggregation and composition?
A) Aggregation is language feature; composition is not.
B) Composition uses pointers; aggregation uses objects.
C) Composition implies ownership and lifetime dependency; aggregation does not.
D) Aggregation destroys owned objects automatically.
Answer: C) Composition implies ownership and lifetime dependency; aggregation does not.

35. Given class A { public: A(int); };, which statement defines an object a with parameter 5?
A) A a = 5;
B) A a(5);
C) A a := 5;
D) A a {}
Answer: B) A a(5);

36. Which of these function declarations inside a class makes it accessible only to the class and its friends/derived classes?
A) public:
B) protected:
C) private:
D) internal:
Answer: B) protected:

37. What will the compiler do when you declare a class member as private?
A) Allow access from anywhere.
B) Restrict direct access to member functions and friends only.
C) Make it accessible to derived classes only.
D) Make it static.
Answer: B) Restrict direct access to member functions and friends only.

38. Which of these converts an rvalue to an lvalue reference and is commonly used in move semantics?
A) std::copy
B) std::forward
C) std::move
D) std::bind
Answer: C) std::move

39. Which operator is commonly overloaded to implement deep copy semantics to allocate new resources?
A) operator+
B) operator= (copy assignment operator)
C) operator[]
D) operator->
Answer: B) operator= (copy assignment operator)

40. What does marking a member function virtual allow?
A) Better optimization
B) Compile-time binding only
C) Runtime polymorphism via dynamic (late) binding
D) Prevent overriding in derived classes
Answer: C) Runtime polymorphism via dynamic (late) binding

41. Which of the following correctly declares a class-level constant integral member?
A) static int const x = 5; (inside class)
B) const static int x = 5; (inside class)
C) Both A and B are acceptable declarations (ordering doesn’t matter).
D) int static constexpr x;
Answer: C) Both A and B are acceptable declarations (ordering doesn’t matter).

42. What is the effect of marking a member function as inline?
A) It forces linking errors.
B) It suggests to the compiler to expand calls inline; also affects linkage for definitions in headers.
C) Prevents function calls.
D) Makes the function virtual.
Answer: B) It suggests to the compiler to expand calls inline; also affects linkage for definitions in headers.

43. Which of the following is an example of delegation in C++ classes?
A) A function calling itself recursively.
B) A member function calling another member function to perform part of its job.
C) Using multiple inheritance.
D) Using global functions.
Answer: B) A member function calling another member function to perform part of its job.

44. What does the explicit keyword for constructors prevent?
A) Construction at compile time
B) Run-time errors
C) Implicit conversions (and copy-initialization via single-argument constructors)
D) Overloading constructors
Answer: C) Implicit conversions (and copy-initialization via single-argument constructors)

45. Consider class S { public: S(int); }; Which initialization will not call the constructor?
A) S s(3);
B) S s = 3;
C) S* p = new S(3);
D) S s;
Answer: D) S s;

46. Which of these statements about operator overloading is true?
A) You can change operator precedence.
B) You cannot create new operators; only existing ones can be overloaded.
C) Overloading always affects global behavior of an operator.
D) All operators can be overloaded as member functions only.
Answer: B) You cannot create new operators; only existing ones can be overloaded.

47. Which operator must be a non-member (usually) when overloading for symmetry (e.g., int + MyClass)?
A) operator=
B) operator->
C) operator+ (to allow left operand be non-class type)
D) operator[]
Answer: C) operator+ (to allow left operand be non-class type)

48. What is the primary purpose of accessors (getters) and mutators (setters) in a class?
A) Speed up access
B) Provide global access to data
C) Encapsulation: control how data members are read/modified
D) Replace constructors
Answer: C) Encapsulation: control how data members are read/modified

49. Which of the following describes a trivially copyable class?
A) Has virtual functions
B) Uses custom destructor with non-trivial behavior
C) Can be copied via memcpy safely (no user-defined copy operations or non-trivial members).
D) Contains std::string member
Answer: C) Can be copied via memcpy safely (no user-defined copy operations or non-trivial members).

50. When is the implicitly-declared copy constructor deleted by the compiler?
A) When class has no members.
B) When class has public members only.
C) When class has a member that is non-copyable (e.g., std::unique_ptr) or user-declared move operations in certain cases.
D) When the destructor is trivial.
Answer: C) When class has a member that is non-copyable (e.g., std::unique_ptr) or user-declared move operations in certain cases.

51. Which C++ feature allows functions to behave differently depending on constness of the object?
A) Templates
B) Const-qualified member function overloads
C) Virtual functions
D) Friend functions
Answer: B) Const-qualified member function overloads

52. If class Base has a non-virtual function f() and Derived overrides f(), calling f() through a Base* pointing to Derived calls:
A) Derived::f()
B) Compiler error
C) Base::f() (static binding)
D) Undefined behavior
Answer: C) Base::f() (static binding)

53. What is a move constructor used for?
A) To copy and increase reference counts.
B) To delete resources permanently.
C) To transfer resources from an rvalue (temporary) to a new object (steal resources).
D) To prevent copying.
Answer: C) To transfer resources from an rvalue (temporary) to a new object (steal resources).

54. Which signature is a valid move constructor for class T?
A) T(T&)
B) T(const T&)
C) T(T&&)
D) T(&&T)
Answer: C) T(T&&)

55. Which of the following correctly expresses composition?
A) Class A contains pointer to class B and B may outlive A.
B) Class A inherits class B.
C) Class A contains an instance (by value) of class B and B’s lifetime is tied to A.
D) Class A references B through global variables.
Answer: C) Class A contains an instance (by value) of class B and B’s lifetime is tied to A.

56. What is the difference between new and malloc when creating objects?
A) malloc calls constructors.
B) new allocates memory only, malloc constructs.
C) new allocates memory and calls constructors; malloc only allocates raw memory.
D) No difference.
Answer: C) new allocates memory and calls constructors; malloc only allocates raw memory.

57. Which of the following is true about default member initializers?
A) They replace constructors.
B) They are applied after constructor body executes.
C) They provide initial values used if constructor member initializer list does not initialize them.
D) They cannot be used for static members.
Answer: C) They provide initial values used if constructor member initializer list does not initialize them.

58. What is the output of the following (pseudocode)?
class A { public: int x; A(int v):x(v){} }; A a(5); cout<<a.x;
A) Compilation error
B) 5
C) 0
D) Undefined
Answer: B) 5

59. Which of the following ensures a function cannot be overridden in derived classes?
A) const
B) static
C) virtual
D) final
Answer: D) final

60. Which method of passing an object avoids both copying and allows polymorphism?
A) Pass by value
B) Pass by reference or pointer to base class
C) Pass by pointer to object allocated on stack
D) Pass by raw memory copy
Answer: B) Pass by reference or pointer to base class

61. What is the effect of = default on a special member function?
A) Delete it.
B) Request the compiler to generate the default implementation.
C) Make it inline only.
D) Make it virtual.
Answer: B) Request the compiler to generate the default implementation.

62. Which of the following indicates that a class cannot be instantiated?
A) It has a public constructor.
B) It has a virtual destructor.
C) It has at least one pure virtual function (is abstract).
D) It is declared final.
Answer: C) It has at least one pure virtual function (is abstract).

63. When copying an object that contains a pointer, which approach provides safe independent copies?
A) Shallow copy
B) Move semantics only
C) Deep copy (allocate separate memory and copy contents)
D) Using memset
Answer: C) Deep copy (allocate separate memory and copy contents)

64. Which of the following code snippets demonstrates preventing implicit conversions in constructors?
A) A(int x) {}
B) explicit A(int x) {}
C) A(const A&) {}
D) A() = delete;
Answer: B) explicit A(int x) {}

65. What does object.member do when object is a pointer?
A) Accesses member by reference.
B) Calls operator-> overload.
C) It’s invalid — you must use object->member when object is a pointer.
D) Compiles if member is static.
Answer: C) It’s invalid — you must use object->member when object is a pointer.

66. Which of these statements regarding default member-wise copy is true?
A) It copies only pointers to resources (shallow).
B) It copies each non-static data member using their copy constructors or built-in copy.
C) It always causes runtime error.
D) It performs a bitwise copy regardless of member types.
Answer: B) It copies each non-static data member using their copy constructors or built-in copy.

67. What happens if a class has virtual functions but no virtual destructor, and you delete a derived object through a base pointer?
A) Derived destructor will be called anyway.
B) Undefined behavior — derived destructor may not run (resource leak).
C) Compiler error.
D) Base destructor will call derived destructor.
Answer: B) Undefined behavior — derived destructor may not run (resource leak).

68. Which of the following allows non-member functions to access private data of a class?
A) public members
B) protected members
C) friend declarations
D) virtual functions
Answer: C) friend declarations

69. Which of these is NOT a special member function?
A) Default constructor
B) Copy constructor
C) Move constructor
D) A regular member function named foo
Answer: D) A regular member function named foo

70. What is the behavior of delete on a null pointer?
A) Crashes program
B) No effect (safe – no-op)
C) Undefined behavior
D) Frees arbitrary memory
Answer: B) No effect (safe – no-op)

71. In the context of classes, what does encapsulation primarily provide?
A) Faster execution
B) Public access to internals
C) Hiding internal data and exposing controlled interfaces
D) Ability to overload operators
Answer: C) Hiding internal data and exposing controlled interfaces

72. Which of the following will create a constant object c of class C?
A) C c;
B) const C c;
C) C* c = new C;
D) C& c;
Answer: B) const C c;

73. Which member function can modify a member marked mutable even when called on a const object?
A) Only non-const functions
B) Const member function
C) Virtual member function only
D) Static member function only
Answer: B) Const member function

74. Which statement about anonymous objects is true?
A) They cannot be passed to functions.
B) They have no temporary lifetime.
C) Anonymous temporaries can be created from prvalues and passed to functions.
D) They persist after the expression ends.
Answer: C) Anonymous temporaries can be created from prvalues and passed to functions.

75. Which of the following is true about member function pointers?
A) They point to static memory only.
B) They require an object (or pointer) to be invoked because non-static member functions have hidden this.
C) They are the same as regular function pointers.
D) They cannot point to virtual functions.
Answer: B) They require an object (or pointer) to be invoked because non-static member functions have hidden this.

76. What is the correct syntax to declare a nested class Inner inside class Outer?
A) class Inner; outside Outer
B) class Outer { Inner; };
C) class Outer { public: class Inner { /*…*/ }; };
D) class Inner { class Outer { }; };
Answer: C) class Outer { public: class Inner { /*…*/ }; };

77. Which of these will prevent implicit conversion when assigning int to class Num?
A) Define constructor Num(int) without explicit.
B) Mark single-argument constructor explicit Num(int).
C) Overload assignment operator.
D) Use friend function.
Answer: B) Mark single-argument constructor explicit Num(int).

78. When should you declare a destructor virtual?
A) Only for classes without inheritance.
B) Always, even for leaf classes with no derived classes.
C) When a class is intended to be used polymorphically (deleted via base pointer).
D) Never; virtual destructors are deprecated.
Answer: C) When a class is intended to be used polymorphically (deleted via base pointer).

79. Which of the following best describes a POD (Plain Old Data) type in C++11 terms?
A) A class with virtual functions
B) Contains non-trivial constructors
C) An aggregate that is trivially copyable and standard-layout
D) Only primitive built-in types
Answer: C) An aggregate that is trivially copyable and standard-layout

80. Which of these is NOT a correct reason to use member initializer list?
A) Initialize reference members
B) Initialize const members
C) To delay initialization until after constructor body executes
D) Invoke non-default constructors of members
Answer: C) To delay initialization until after constructor body executes

81. What is stored in a typical object memory layout for a polymorphic class?
A) Only data members
B) A pointer to vtable (if virtual functions exist) plus data members
C) Function code inline with object
D) Only vtable pointer
Answer: B) A pointer to vtable (if virtual functions exist) plus data members

82. Which of the following is true about multiple constructors with different signatures?
A) The compiler will choose arbitrarily.
B) They are overloaded constructors — chosen by matching arguments.
C) Only the first one declared is used.
D) All are executed in order.
Answer: B) They are overloaded constructors — chosen by matching arguments.

83. When a class contains a reference member, what must be done in the constructor?
A) Assign reference in constructor body.
B) Leave it uninitialized.
C) Initialize the reference in the member initializer list.
D) Use = default for constructor.
Answer: C) Initialize the reference in the member initializer list.

84. Which of the following is true about virtual inheritance?
A) It duplicates base subobjects in every derived class.
B) It ensures a single shared base subobject when multiple derived paths exist.
C) It is the default inheritance mode.
D) It is unrelated to diamond problem.
Answer: B) It ensures a single shared base subobject when multiple derived paths exist.

85. Which of the following describes late binding in C++?
A) Resolving function call at compile time.
B) Binding variables to memory late.
C) Resolving virtual function calls at runtime using vtable.
D) Postponing compilation of class header.
Answer: C) Resolving virtual function calls at runtime using vtable.

86. What does std::move do to an lvalue?
A) Converts to const lvalue.
B) Converts it to an rvalue reference (enables move semantics).
C) Deletes the object.
D) Copies the object.
Answer: B) Converts it to an rvalue reference (enables move semantics).

87. Which of the following statements is true about friend classes?
A) Friend classes become base classes.
B) Friend classes can access private/protected members of the other class.
C) Friend declaration is transitive across classes.
D) Friend classes must be defined inside the other class.
Answer: B) Friend classes can access private/protected members of the other class.

88. If a class A has a deleted copy constructor, what happens when you try to pass an A by value to a function?
A) Function will get default-constructed object.
B) Compilation error — copy is not allowed.
C) Runtime copying using memcpy.
D) Implicit move occurs.
Answer: B) Compilation error — copy is not allowed.

89. Which of the following choices best defines class invariants?
A) Temporary values inside functions.
B) Conditions that must hold true for an object throughout its lifetime (between public member calls).
C) Variables used only in constructors.
D) Names of classes that cannot be changed.
Answer: B) Conditions that must hold true for an object throughout its lifetime (between public member calls).

90. What is the result of assigning one pointer-to-member function to another if signatures match?
A) Illegal operation.
B) Allowed — pointer-to-member behave like function pointers for matching members.
C) Causes crash.
D) Converts to regular function pointer.
Answer: B) Allowed — pointer-to-member behave like function pointers for matching members.

91. Which of the following is NOT true about abstract classes?
A) They cannot be instantiated directly.
B) They can have implemented members.
C) They must contain at least one data member.
D) They are used as interfaces or base classes.
Answer: C) They must contain at least one data member.

92. What does the rule of three/five/zero refer to in class design?
A) Number of base classes to inherit from.
B) When to declare or rely on compiler-generated destructor, copy/move constructors and assignment operators.
C) Qty of member functions allowed.
D) Limit on template parameters.
Answer: B) When to declare or rely on compiler-generated destructor, copy/move constructors and assignment operators.

93. Which of these affects the layout of class objects?
A) Declaration order of data members
B) Alignment requirements
C) Presence of virtual functions (vptr)
D) All of the above
Answer: D) All of the above

94. What is the correct way to declare an overloaded function in a class?
A) Change only the return type.
B) Change the parameter types or number (function signature must differ).
C) Have same signature twice.
D) Change only const qualifier on return type.
Answer: B) Change the parameter types or number (function signature must differ).

95. Which of the following allows a function to accept objects of different types sharing a common interface?
A) Templates only
B) Polymorphism via base-class pointers/references
C) Friend functions only
D) Passing by value
Answer: B) Polymorphism via base-class pointers/references

96. What is the effect of marking a member function noexcept?
A) It must not be called.
B) It promises not to throw exceptions; enables certain optimizations and influences exception propagation.
C) It turns the function into a destructor.
D) It makes the function inline.
Answer: B) It promises not to throw exceptions; enables certain optimizations and influences exception propagation.

97. Which of the following is true about overloading operator->?
A) It must return void.
B) It should return a pointer or object that behaves like a pointer so chaining works.
C) It cannot be overloaded for smart-pointer classes.
D) It only works for built-in types.
Answer: B) It should return a pointer or object that behaves like a pointer so chaining works.

98. Which concept prevents undefined behavior when calling virtual functions inside constructors?
A) Use of final on functions
B) Avoiding virtual functions altogether
C) Understand that during base class constructor, virtual calls use base class implementations (no dynamic dispatch to derived).
D) Using friend functions for construction
Answer: C) Understand that during base class constructor, virtual calls use base class implementations (no dynamic dispatch to derived).

99. Which of the following is a correct statement about member function templates inside a class?
A) They cannot be static.
B) They allow per-member-template parameterization independent of class template parameters.
C) They are always instantiated at compile time only if used.
D) They cannot be overloaded.
Answer: B) They allow per-member-template parameterization independent of class template parameters.

100. Which of the following best practice helps avoid resource leaks in class design?
A) Use raw pointers everywhere.
B) Avoid destructors.
C) Use RAII and smart pointers to manage resources automatically.
D) Always use global resource managers.
Answer: C) Use RAII and smart pointers to manage resources automatically.