C++ Classes and Objects - MCQs

1 min read

In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for creating objects. It encapsulates both data members (attributes) and member functions (methods) into a single logical unit, ensuring data hiding through access specifiers like private, protected, and public. An object is a distinct instance of a class that occupies memory and allows interaction with the defined class members. Understanding classes and objects is critical for mastering C++ memory management, modular programming, and advanced OOP concepts like inheritance and polymorphism. Grasping object instantiation, constructor invocation, and scope resolution is essential for any aspiring C++ developer.

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

a) class
b) struct
c) object
d) define
Correct Answer: a) class
Explanation:
The 'class' keyword is used to define a class in C++.

2. What is the default access specifier for members of a class in C++?

a) public
b) private
c) protected
d) friend
Correct Answer: b) private
Explanation:
By default, all members of a class are private in C++.

3. What is the default access specifier for members of a struct in C++?

a) public
b) private
c) protected
d) internal
Correct Answer: a) public
Explanation:
By default, all members of a struct are public in C++.

4. Which operator is used to access members of a class through an object?

a) ->
b) .
c) ::
d) *
Correct Answer: b) .
Explanation:
The dot (.) operator is used to access members of a class through an object instance.

5. Which operator is used to access members of a class through a pointer to an object?

a) .
b) ->
c) ::
d) &
Correct Answer: b) ->
Explanation:
The arrow (->) operator is used to access members of a class through a pointer to an object.

6. What is the size of an empty class in C++?

a) 0
b) 1
c) 4
d) 8
Correct Answer: b) 1
Explanation:
An empty class has a size of 1 byte to ensure that two different objects have distinct memory addresses.

7. Which of the following cannot be used to define a member function outside the class?

a) Scope resolution operator
b) Inline keyword inside class
c) Dot operator
d) Class name with ::
Correct Answer: c) Dot operator
Explanation:
The dot operator is for object member access, not for defining functions outside the class. The scope resolution operator (::) is used for that.

8. A function defined inside a class definition is implicitly treated as what?

a) static
b) inline
c) virtual
d) friend
Correct Answer: b) inline
Explanation:
Functions defined inside the class body are implicitly inline, suggesting the compiler to replace the call with the function body.

9. Which of the following is true about member functions of a class?

a) They must be defined inside the class
b) They can be defined outside the class using ::
c) They cannot access private members
d) They are always static
Correct Answer: b) They can be defined outside the class using ::
Explanation:
Member functions can be defined outside the class using the scope resolution operator (::).

10. How do you declare an object of class 'MyClass' named 'obj'?

a) MyClass obj;
b) class MyClass obj;
c) object MyClass obj;
d) MyClass->obj;
Correct Answer: a) MyClass obj;
Explanation:
Objects are declared using the class name followed by the object name and a semicolon.

11. Which access specifier allows members to be accessible only within the class and its friends?

a) public
b) private
c) protected
d) default
Correct Answer: b) private
Explanation:
Private members are accessible only within the class itself and by friend functions/classes.

12. Which access specifier allows members to be accessible in derived classes but not outside?

a) public
b) private
c) protected
d) internal
Correct Answer: c) protected
Explanation:
Protected members are accessible within the class and its derived classes, but not from outside.

13. Can a private member of a class be accessed by a derived class?

a) Yes, directly
b) No, never
c) Yes, if it is static
d) Yes, if it is mutable
Correct Answer: b) No, never
Explanation:
Private members cannot be accessed directly by derived classes. They must use protected or public members.

14. What is the purpose of the 'public' access specifier?

a) Restrict access to the class
b) Allow access from anywhere
c) Allow access only in derived classes
d) Make members static
Correct Answer: b) Allow access from anywhere
Explanation:
Public members can be accessed from anywhere the object is visible.

15. Which of the following is correct about 'struct' and 'class' in C++?

a) struct cannot have member functions
b) class cannot have public members
c) The only difference is the default access specifier
d) struct is deprecated in C++
Correct Answer: c) The only difference is the default access specifier
Explanation:
In C++, struct and class are functionally identical except for the default access specifier (public for struct, private for class).

16. What is a constructor in C++?

a) A function that destroys objects
b) A special member function used to initialize objects
c) A static function
d) A friend function
Correct Answer: b) A special member function used to initialize objects
Explanation:
A constructor is a special member function that initializes objects of a class.

17. Which of the following is true about constructors?

a) They must have a return type
b) They have the same name as the class
c) They cannot have parameters
d) They are called explicitly by the user
Correct Answer: b) They have the same name as the class
Explanation:
Constructors must have the exact same name as the class and do not have a return type.

18. What is a default constructor?

a) A constructor with no parameters
b) A constructor that deletes objects
c) A constructor provided by the OS
d) A static constructor
Correct Answer: a) A constructor with no parameters
Explanation:
A default constructor is a constructor that takes no arguments (or all arguments have default values).

19. When is a copy constructor called?

a) When an object is created from scratch
b) When an object is initialized with another object of the same class
c) When an object is destroyed
d) When a static member is accessed
Correct Answer: b) When an object is initialized with another object of the same class
Explanation:
A copy constructor is called when a new object is initialized using an existing object of the same class.

20. What is the signature of a copy constructor?

a) ClassName(ClassName obj)
b) ClassName(const ClassName &obj)
c) ClassName(ClassName *obj)
d) ClassName(ClassName &&obj)
Correct Answer: b) ClassName(const ClassName &obj)
Explanation:
A copy constructor takes a constant reference to an object of the same class to avoid infinite recursion and allow const objects.

21. Can a constructor be virtual?

a) Yes
b) No
c) Only if it is static
d) Only in abstract classes
Correct Answer: b) No
Explanation:
Constructors cannot be virtual because the object is not fully created yet, and the vptr is not initialized.

22. What happens if a class does not define any constructor?

a) Compilation error
b) The compiler provides a default constructor
c) The object cannot be created
d) Runtime error
Correct Answer: b) The compiler provides a default constructor
Explanation:
If no constructor is defined, the compiler implicitly generates a default constructor.

23. Can a constructor have a return type?

a) Yes, int
b) Yes, void
c) No, never
d) Yes, the class type
Correct Answer: c) No, never
Explanation:
Constructors do not have a return type, not even void.

24. What is constructor overloading?

a) Having multiple constructors with different parameters
b) Overriding a constructor in a derived class
c) Deleting a constructor
d) Making a constructor static
Correct Answer: a) Having multiple constructors with different parameters
Explanation:
Constructor overloading means having multiple constructors in a class with different parameter lists.

25. Which constructor is called when an object is passed by value to a function?

a) Default constructor
b) Copy constructor
c) Parameterized constructor
d) Destructor
Correct Answer: b) Copy constructor
Explanation:
When an object is passed by value, a copy is made, invoking the copy constructor.

26. What is a destructor in C++?

a) A function that initializes objects
b) A special member function that cleans up before an object is destroyed
c) A function that copies objects
d) A static function
Correct Answer: b) A special member function that cleans up before an object is destroyed
Explanation:
A destructor is called automatically when an object goes out of scope or is explicitly deleted.

27. How is a destructor declared?

a) ~ClassName()
b) ClassName()
c) !ClassName()
d) delete ClassName()
Correct Answer: a) ~ClassName()
Explanation:
A destructor is declared with a tilde (~) followed by the class name and no parameters.

28. Can a destructor have parameters?

a) Yes
b) No
c) Only one parameter
d) Only if it is virtual
Correct Answer: b) No
Explanation:
Destructors take no parameters and cannot be overloaded.

29. Can a destructor be overloaded?

a) Yes
b) No
c) Only in derived classes
d) Only if virtual
Correct Answer: b) No
Explanation:
Since destructors take no parameters, they cannot be overloaded.

30. When is a destructor called for a local object?

a) When the program starts
b) When the object goes out of scope
c) When the object is created
d) Never
Correct Answer: b) When the object goes out of scope
Explanation:
Destructors for local objects are called automatically when they go out of scope.

31. In what order are destructors called for multiple local objects?

a) Same order as creation
b) Reverse order of creation
c) Random order
d) Alphabetical order
Correct Answer: b) Reverse order of creation
Explanation:
Destructors are called in the reverse order of their construction (LIFO).

32. What is the purpose of the 'this' pointer?

a) Points to the class definition
b) Points to the current object invoking the member function
c) Points to the base class
d) Points to the static members
Correct Answer: b) Points to the current object invoking the member function
Explanation:
The 'this' pointer is an implicit pointer available to all non-static member functions, pointing to the object that called the function.

33. What is the type of the 'this' pointer in a non-const member function?

a) ClassName*
b) const ClassName*
c) ClassName&
d) const ClassName&
Correct Answer: a) ClassName*
Explanation:
In a non-const member function, 'this' is a pointer of type ClassName*.

34. What is the type of the 'this' pointer in a const member function?

a) ClassName*
b) const ClassName*
c) ClassName&
d) const ClassName&
Correct Answer: b) const ClassName*
Explanation:
In a const member function, 'this' is a pointer of type const ClassName*, preventing modification of object data.

35. Can static member functions use the 'this' pointer?

a) Yes
b) No
c) Only if declared virtual
d) Only in derived classes
Correct Answer: b) No
Explanation:
Static member functions do not belong to any specific object, so they do not have a 'this' pointer.

36. What does returning '*this' from a member function allow?

a) Method chaining
b) Static binding
c) Memory deallocation
d) Friend access
Correct Answer: a) Method chaining
Explanation:
Returning '*this' (a reference to the object) allows method chaining, like obj.func1().func2().

37. What is a static data member?

a) A member unique to each object
b) A member shared by all objects of the class
c) A constant member
d) A private member
Correct Answer: b) A member shared by all objects of the class
Explanation:
Static data members are shared among all instances of the class; only one copy exists.

38. How is a static data member initialized?

a) Inside the class definition
b) Outside the class definition in a source file
c) In the constructor
d) In the destructor
Correct Answer: b) Outside the class definition in a source file
Explanation:
Static data members must be defined and initialized outside the class, typically in a .cpp file.

39. How do you access a static member function?

a) Only through an object
b) Using the class name and scope resolution operator
c) Using the this pointer
d) It cannot be accessed
Correct Answer: b) Using the class name and scope resolution operator
Explanation:
Static member functions can be called using the class name directly, e.g., ClassName::func().

40. Can a static member function access non-static data members?

a) Yes
b) No
c) Only if they are public
d) Only if they are mutable
Correct Answer: b) No
Explanation:
Static member functions do not have a 'this' pointer, so they cannot access non-static members directly.

41. What is a friend function?

a) A member function of the class
b) A non-member function that can access private members of the class
c) A static member function
d) A virtual function
Correct Answer: b) A non-member function that can access private members of the class
Explanation:
A friend function is not a member of the class but is granted access to its private and protected members.

42. Which keyword is used to declare a friend function?

a) friend
b) ally
c) partner
d) access
Correct Answer: a) friend
Explanation:
The 'friend' keyword is used inside the class to grant access to a non-member function.

43. Is a friend function a member of the class?

a) Yes
b) No
c) Only if it is static
d) Only if it is virtual
Correct Answer: b) No
Explanation:
Friend functions are normal external functions, not members of the class.

44. Can a class be declared as a friend of another class?

a) Yes
b) No
c) Only if it is derived
d) Only if it is abstract
Correct Answer: a) Yes
Explanation:
An entire class can be declared as a friend, making all its member functions friends of the other class.

45. Is friendship inherited in C++?

a) Yes
b) No
c) Only for public friends
d) Only for protected friends
Correct Answer: b) No
Explanation:
Friendship is not inherited. Derived classes do not automatically inherit friend status.

46. Is friendship mutual in C++?

a) Yes
b) No
c) Only for classes
d) Only for functions
Correct Answer: b) No
Explanation:
Friendship is not mutual. If A is a friend of B, B is not automatically a friend of A.

47. Can a friend function be declared in the private section of a class?

a) Yes
b) No
c) Only if it is static
d) Only if it is inline
Correct Answer: a) Yes
Explanation:
The access specifier where a friend is declared does not matter; it grants access regardless of where it is placed.

48. What is a const member function?

a) A function that returns a const value
b) A function that cannot modify the object's non-mutable data members
c) A static function
d) A friend function
Correct Answer: b) A function that cannot modify the object's non-mutable data members
Explanation:
A const member function promises not to modify the object's state (except mutable members).

49. How is a const member function declared?

a) const void func()
b) void func() const
c) void const func()
d) const void func() const
Correct Answer: b) void func() const
Explanation:
The 'const' keyword is placed after the parameter list in the function declaration.

50. Can a const object call a non-const member function?

a) Yes
b) No
c) Only if it is static
d) Only if it is friend
Correct Answer: b) No
Explanation:
A const object can only call const member functions to ensure its state is not modified.

51. What is the 'mutable' keyword used for?

a) Making a function virtual
b) Allowing a member variable to be modified in a const function
c) Making a class abstract
d) Declaring a friend
Correct Answer: b) Allowing a member variable to be modified in a const function
Explanation:
Mutable members can be modified even within const member functions or const objects.

52. Can static data members be declared mutable?

a) Yes
b) No
c) Only if const
d) Only if public
Correct Answer: b) No
Explanation:
Static members are not part of the object's state in the same way, and the compiler forbids making them mutable.

53. What is shallow copying?

a) Copying only public members
b) Copying the object by copying the values of its members, including pointers
c) Copying an object without a copy constructor
d) Deleting an object
Correct Answer: b) Copying the object by copying the values of its members, including pointers
Explanation:
Shallow copy copies pointer addresses, meaning both objects point to the same dynamically allocated memory.

54. What is deep copying?

a) Copying only private members
b) Copying the object and allocating new memory for pointer members
c) Copying an object using the default constructor
d) Deleting an object
Correct Answer: b) Copying the object and allocating new memory for pointer members
Explanation:
Deep copy allocates new memory and copies the actual data, ensuring independent objects.

55. When is a deep copy necessary?

a) When the class has no pointers
b) When the class manages dynamic memory via pointers
c) When the class is abstract
d) When the class has static members
Correct Answer: b) When the class manages dynamic memory via pointers
Explanation:
If a class has pointers to dynamic memory, a deep copy is needed to avoid double deletion and shared state issues.

56. What is the assignment operator in C++?

a) ==
b) =
c) :=
d) ->
Correct Answer: b) =
Explanation:
The '=' operator is used to assign the value of one object to another existing object.

57. What does the compiler provide if you don't define an assignment operator?

a) Compilation error
b) A default shallow copy assignment operator
c) A deep copy assignment operator
d) Nothing
Correct Answer: b) A default shallow copy assignment operator
Explanation:
The compiler generates a default assignment operator that performs a member-wise shallow copy.

58. What is the Rule of Three in C++?

a) A class needs three constructors
b) If a class defines one of destructor, copy constructor, or copy assignment, it should probably define all three
c) A class can have three base classes
d) A class can have three friend functions
Correct Answer: b) If a class defines one of destructor, copy constructor, or copy assignment, it should probably define all three
Explanation:
The Rule of Three states that if you need a custom destructor, copy constructor, or copy assignment operator, you likely need all three to manage resources properly.

59. What is inheritance in C++?

a) Creating a new class from an existing class
b) Deleting a class
c) Copying an object
d) Overloading a function
Correct Answer: a) Creating a new class from an existing class
Explanation:
Inheritance allows a class (derived) to acquire properties and methods of another class (base).

60. Which symbol is used to denote inheritance in C++?

a) ->
b) :
c) ::
d) .
Correct Answer: b) :
Explanation:
A single colon (:) is used to denote inheritance, e.g., class Derived : public Base.

61. What is the default inheritance access specifier for a class?

a) public
b) private
c) protected
d) friend
Correct Answer: b) private
Explanation:
When a class inherits from another class without specifying an access specifier, it defaults to private inheritance.

62. What is the default inheritance access specifier for a struct?

a) public
b) private
c) protected
d) internal
Correct Answer: a) public
Explanation:
When a struct inherits from another struct/class without specifying an access specifier, it defaults to public inheritance.

63. In public inheritance, what happens to public members of the base class?

a) They become private
b) They remain public
c) They become protected
d) They are deleted
Correct Answer: b) They remain public
Explanation:
In public inheritance, public members of the base class remain public in the derived class.

64. In private inheritance, what happens to protected members of the base class?

a) They remain protected
b) They become public
c) They become private
d) They are deleted
Correct Answer: c) They become private
Explanation:
In private inheritance, both public and protected members of the base class become private in the derived class.

65. What is multiple inheritance?

a) Inheriting from one base class
b) Inheriting from more than one base class
c) Inheriting multiple times from the same class
d) Inheriting static members only
Correct Answer: b) Inheriting from more than one base class
Explanation:
Multiple inheritance allows a derived class to inherit from two or more base classes.

66. What is the diamond problem in multiple inheritance?

a) Memory leak
b) Ambiguity when two base classes inherit from the same grandparent class
c) Infinite recursion
d) Private access violation
Correct Answer: b) Ambiguity when two base classes inherit from the same grandparent class
Explanation:
The diamond problem occurs when a class inherits from two classes that both inherit from a common base, causing ambiguity.

67. How is the diamond problem resolved in C++?

a) Using private inheritance
b) Using virtual inheritance
c) Using static members
d) Using friend classes
Correct Answer: b) Using virtual inheritance
Explanation:
Virtual inheritance ensures that only one instance of the common base class is inherited.

68. What is polymorphism in C++?

a) Having multiple classes
b) The ability of a function or object to take on many forms
c) Hiding data
d) Creating objects
Correct Answer: b) The ability of a function or object to take on many forms
Explanation:
Polymorphism allows entities like functions or objects to be processed differently based on their context or type.

69. What is compile-time polymorphism?

a) Virtual functions
b) Function overloading and operator overloading
c) Inheritance
d) Dynamic binding
Correct Answer: b) Function overloading and operator overloading
Explanation:
Compile-time polymorphism is achieved through function overloading, operator overloading, and templates.

70. What is run-time polymorphism?

a) Function overloading
b) Function overriding using virtual functions
c) Operator overloading
d) Static binding
Correct Answer: b) Function overriding using virtual functions
Explanation:
Run-time polymorphism is achieved using virtual functions and function overriding in inheritance.

71. Which keyword is used to achieve run-time polymorphism?

a) static
b) virtual
c) inline
d) friend
Correct Answer: b) virtual
Explanation:
The 'virtual' keyword is used in the base class to allow derived classes to override the function.

72. What is function overriding?

a) Defining multiple functions with the same name in the same class
b) Redefining a base class function in the derived class with the same signature
c) Overloading operators
d) Hiding a function
Correct Answer: b) Redefining a base class function in the derived class with the same signature
Explanation:
Function overriding occurs when a derived class provides a specific implementation of a virtual function already defined in its base class.

73. What is function hiding in C++?

a) Overriding a virtual function
b) Defining a function in the derived class with the same name but different signature as the base class
c) Making a function private
d) Deleting a function
Correct Answer: b) Defining a function in the derived class with the same name but different signature as the base class
Explanation:
If a derived class defines a function with the same name but different parameters, it hides all base class functions with that name.

74. Can a virtual function be static?

a) Yes
b) No
c) Only if public
d) Only if protected
Correct Answer: b) No
Explanation:
Virtual functions must be non-static member functions because they rely on the object's vptr for dynamic binding.

75. Can a virtual function be a friend?

a) Yes
b) No
c) Only if static
d) Only if inline
Correct Answer: b) No
Explanation:
A virtual function must be a member of a class, while a friend function is not a member.

76. Can constructors be virtual?

a) Yes
b) No
c) Only if default
d) Only if parameterized
Correct Answer: b) No
Explanation:
Constructors cannot be virtual because the object is not fully constructed when the constructor is called, so the vptr is not set.

77. Should destructors of base classes be virtual?

a) No, never
b) Yes, if the class is intended to be used as a base class
c) Only if they are private
d) Only if they are static
Correct Answer: b) Yes, if the class is intended to be used as a base class
Explanation:
Making base class destructors virtual ensures that the derived class destructor is called when deleting through a base class pointer.

78. What is a pure virtual function?

a) A virtual function with no implementation in the base class
b) A static virtual function
c) A friend virtual function
d) An inline virtual function
Correct Answer: a) A virtual function with no implementation in the base class
Explanation:
A pure virtual function is declared by assigning 0 in its declaration, e.g., virtual void func() = 0.

79. What is an abstract class?

a) A class with no data members
b) A class that contains at least one pure virtual function
c) A class with only static members
d) A class with no constructors
Correct Answer: b) A class that contains at least one pure virtual function
Explanation:
A class becomes abstract if it has at least one pure virtual function, and it cannot be instantiated.

80. Can you create an object of an abstract class?

a) Yes
b) No
c) Only if it has a default constructor
d) Only if it is public
Correct Answer: b) No
Explanation:
Abstract classes cannot be instantiated directly; they must be derived from.

81. Can an abstract class have constructors?

a) Yes
b) No
c) Only if pure virtual
d) Only if static
Correct Answer: a) Yes
Explanation:
Abstract classes can have constructors, which are called when a derived class object is created.

82. What is the purpose of an abstract class?

a) To create objects directly
b) To provide a common interface and base for derived classes
c) To hide all data
d) To make all functions static
Correct Answer: b) To provide a common interface and base for derived classes
Explanation:
Abstract classes define a common interface that derived classes must implement, enforcing a contract.

83. What is a nested class?

a) A class defined inside another class
b) A class that inherits from another class
c) A class with no members
d) A class with only static members
Correct Answer: a) A class defined inside another class
Explanation:
A nested class is a class defined within the scope of another enclosing class.

84. Does a nested class have access to the private members of the enclosing class?

a) Yes, automatically
b) No, unless declared as a friend
c) Only if it is public
d) Only if it is static
Correct Answer: b) No, unless declared as a friend
Explanation:
A nested class does not have special access to the enclosing class's private members unless explicitly granted friend status.

85. What is the scope of a nested class?

a) Global scope
b) The scope of the enclosing class
c) Local scope
d) Namespace scope
Correct Answer: b) The scope of the enclosing class
Explanation:
A nested class is scoped within the enclosing class and must be accessed using the enclosing class name.

86. What is a local class?

a) A class defined inside a function
b) A class defined inside another class
c) A class with local variables
d) A class with private members
Correct Answer: a) A class defined inside a function
Explanation:
A local class is defined inside a function body and its scope is limited to that function.

87. Can a local class access local variables of the enclosing function?

a) Yes, all of them
b) No, unless they are static or enum
c) Only if they are public
d) Only if they are pointers
Correct Answer: b) No, unless they are static or enum
Explanation:
Local classes cannot access non-static local variables of the enclosing function, but can access static variables and enums.

88. What does the 'sizeof' operator return when applied to a class with only static members?

a) 0
b) 1
c) Size of the static members
d) Size of the vptr
Correct Answer: b) 1
Explanation:
Static members are not part of the object's size. An empty class or one with only static members has a size of 1 byte.

89. What is memory padding in classes?

a) Adding extra functions
b) Adding extra bytes to align data members in memory
c) Deleting unused members
d) Making members public
Correct Answer: b) Adding extra bytes to align data members in memory
Explanation:
Compilers add padding bytes between data members to ensure proper memory alignment for hardware efficiency.

90. How does the order of data members affect class size?

a) It does not affect size
b) It can affect size due to memory padding
c) It always increases size
d) It always decreases size
Correct Answer: b) It can affect size due to memory padding
Explanation:
Reordering members from largest to smallest can minimize padding and reduce the overall size of the class.

91. What is the size of a class with one virtual function?

a) 0
b) 1
c) Size of data members only
d) Size of data members plus the size of a vptr
Correct Answer: d) Size of data members plus the size of a vptr
Explanation:
A class with virtual functions contains a hidden virtual pointer (vptr), adding its size (usually 4 or 8 bytes) to the object.

92. What is a vptr?

a) A virtual pointer to the object's data
b) A hidden pointer added to objects of classes with virtual functions, pointing to the vtable
c) A pointer to the base class
d) A static pointer
Correct Answer: b) A hidden pointer added to objects of classes with virtual functions, pointing to the vtable
Explanation:
The vptr (virtual pointer) is inserted by the compiler to point to the virtual table (vtable) for dynamic dispatch.

93. What is a vtable?

a) A table of object data
b) A static array of function pointers for virtual functions
c) A table of constructors
d) A table of friend functions
Correct Answer: b) A static array of function pointers for virtual functions
Explanation:
The vtable (virtual table) is a static array created by the compiler for each class with virtual functions, storing pointers to the actual functions.

94. When is the vptr initialized?

a) Before the constructor runs
b) During the constructor execution
c) After the destructor runs
d) At compile time
Correct Answer: b) During the constructor execution
Explanation:
The vptr is initialized by the compiler-generated code inside the constructor, before the user-defined constructor body executes.

95. Can you call a virtual function from a constructor?

a) Yes, and it calls the derived class version
b) Yes, but it calls the base class version
c) No, it causes a compilation error
d) No, it causes a runtime error
Correct Answer: b) Yes, but it calls the base class version
Explanation:
When a virtual function is called from a base class constructor, the vptr points to the base class's vtable, so the base class version is executed.

96. What is the 'final' specifier used for in C++11?

a) Making a class abstract
b) Preventing a class from being inherited or a virtual function from being overridden
c) Making a member static
d) Making a member friend
Correct Answer: b) Preventing a class from being inherited or a virtual function from being overridden
Explanation:
The 'final' specifier prevents further derivation of a class or overriding of a virtual function in derived classes.

97. What is the 'override' specifier used for in C++11?

a) Making a function virtual
b) Explicitly indicating that a function is intended to override a base class virtual function
c) Hiding a base class function
d) Making a function static
Correct Answer: b) Explicitly indicating that a function is intended to override a base class virtual function
Explanation:
The 'override' specifier ensures the function actually overrides a base class virtual function, catching signature mismatches at compile time.

98. What is object slicing?

a) Deleting part of an object
b) When a derived class object is assigned to a base class object, losing derived-specific data
c) Copying an object
d) Destroying an object
Correct Answer: b) When a derived class object is assigned to a base class object, losing derived-specific data
Explanation:
Object slicing occurs when a derived object is passed by value to a base class parameter, truncating the derived part.

99. How can object slicing be prevented?

a) Using pointers or references
b) Using static members
c) Using friend functions
d) Using private inheritance
Correct Answer: a) Using pointers or references
Explanation:
Passing objects by pointer or reference prevents slicing because the original derived object is not copied into a base object.

100. What is the 'explicit' keyword used for?

a) Making a function virtual
b) Preventing implicit conversions via single-argument constructors
c) Making a class abstract
d) Making a member static
Correct Answer: b) Preventing implicit conversions via single-argument constructors
Explanation:
The 'explicit' keyword prevents the compiler from using a single-argument constructor for implicit type conversions.

101. Can a copy constructor be declared explicit?

a) Yes
b) No
c) Only if it is static
d) Only if it is virtual
Correct Answer: a) Yes
Explanation:
Yes, a copy constructor can be explicit, which prevents implicit copying (e.g., passing by value without explicit casting).

102. What is a delegating constructor in C++11?

a) A constructor that calls another constructor in the same class
b) A constructor that calls a base class constructor
c) A static constructor
d) A friend constructor
Correct Answer: a) A constructor that calls another constructor in the same class
Explanation:
Delegating constructors allow one constructor to call another constructor of the same class to reuse initialization code.

103. What is an inline function?

a) A function that is always executed in a separate thread
b) A function where the compiler attempts to embed the function body at the call site
c) A static function
d) A virtual function
Correct Answer: b) A function where the compiler attempts to embed the function body at the call site
Explanation:
Inline functions suggest to the compiler to replace the function call with the actual code to reduce function call overhead.

104. Are inline functions guaranteed to be inlined?

a) Yes, always
b) No, it is a request to the compiler
c) Only if they are virtual
d) Only if they are static
Correct Answer: b) No, it is a request to the compiler
Explanation:
The 'inline' keyword is a hint; the compiler may choose not to inline the function based on its complexity or other factors.

105. What is the difference between 'delete' and 'delete[]'?

a) No difference
b) 'delete' is for single objects, 'delete[]' is for arrays
c) 'delete[]' is for single objects, 'delete' is for arrays
d) 'delete' is for static objects
Correct Answer: b) 'delete' is for single objects, 'delete[]' is for arrays
Explanation:
'delete' calls the destructor for a single object, while 'delete[]' calls destructors for each element in an array.

106. What happens if you use 'delete' instead of 'delete[]' on an array of objects?

a) Nothing
b) Undefined behavior, potentially missing destructor calls
c) Compilation error
d) Runtime error always
Correct Answer: b) Undefined behavior, potentially missing destructor calls
Explanation:
Using 'delete' on an array results in undefined behavior, often leading to memory leaks because only the first object's destructor is called.

107. What is a namespace in C++?

a) A class for memory management
b) A declarative region that provides a scope for identifiers
c) A type of inheritance
d) A type of polymorphism
Correct Answer: b) A declarative region that provides a scope for identifiers
Explanation:
Namespaces are used to organize code into logical groups and prevent name collisions in large projects.

108. Which keyword is used to create a namespace?

a) namespace
b) space
c) scope
d) region
Correct Answer: a) namespace
Explanation:
The 'namespace' keyword is used to define a namespace, e.g., namespace MySpace { ... }.

109. What is the 'using' directive?

a) A way to inherit from a namespace
b) A way to bring all names from a namespace into the current scope
c) A way to delete a namespace
d) A way to create a namespace
Correct Answer: b) A way to bring all names from a namespace into the current scope
Explanation:
The 'using namespace' directive allows you to use names from a namespace without qualifying them.

110. What is the 'using' declaration?

a) Bringing all names from a namespace
b) Bringing a specific name from a namespace into the current scope
c) Deleting a namespace
d) Creating a namespace
Correct Answer: b) Bringing a specific name from a namespace into the current scope
Explanation:
A 'using' declaration brings a single specific identifier from a namespace into the current scope, e.g., using std::cout;

111. What is the standard namespace in C++?

a) std
b) standard
c) cpp
d) default
Correct Answer: a) std
Explanation:
The standard library components in C++ are defined within the 'std' namespace.

112. Can namespaces be nested?

a) Yes
b) No
c) Only up to two levels
d) Only if they are inline
Correct Answer: a) Yes
Explanation:
Namespaces can be nested within other namespaces to create hierarchical scopes.

113. What is an unnamed namespace?

a) A namespace with no name, giving internal linkage to its members
b) A namespace that cannot be used
c) A namespace for static members
d) A namespace for friend functions
Correct Answer: a) A namespace with no name, giving internal linkage to its members
Explanation:
An unnamed namespace restricts the visibility of its members to the current translation unit, similar to 'static' in C.

114. What is the 'inline' namespace feature in C++11?

a) A namespace that is always inlined
b) A namespace whose members are automatically available in the enclosing scope
c) A namespace for inline functions
d) A namespace for static members
Correct Answer: b) A namespace whose members are automatically available in the enclosing scope
Explanation:
An inline namespace allows its members to be used as if they were in the enclosing namespace, useful for versioning.

115. What is the Rule of Zero?

a) A class should have no members
b) A class should not define custom destructors, copy/move constructors, or assignment operators if possible
c) A class should have zero constructors
d) A class should have zero friends
Correct Answer: b) A class should not define custom destructors, copy/move constructors, or assignment operators if possible
Explanation:
The Rule of Zero suggests relying on compiler-generated special member functions by using smart pointers and standard containers.

116. What is the Rule of Five in C++11?

a) A class needs five constructors
b) If a class defines one of destructor, copy constructor, copy assignment, move constructor, or move assignment, it should probably define all five
c) A class can have five base classes
d) A class can have five friend functions
Correct Answer: b) If a class defines one of destructor, copy constructor, copy assignment, move constructor, or move assignment, it should probably define all five
Explanation:
The Rule of Five extends the Rule of Three to include move semantics introduced in C++11.

117. What is a move constructor?

a) A constructor that copies an object
b) A constructor that transfers resources from a temporary object
c) A static constructor
d) A friend constructor
Correct Answer: b) A constructor that transfers resources from a temporary object
Explanation:
A move constructor takes an rvalue reference and transfers ownership of resources, avoiding expensive deep copies.

118. What is an rvalue reference?

a) A reference to a left value
b) A reference denoted by &&, typically binding to temporary objects
c) A static reference
d) A const reference
Correct Answer: b) A reference denoted by &&, typically binding to temporary objects
Explanation:
Rvalue references (&&) bind to temporary objects (rvalues) and enable move semantics.

119. What is std::move?

a) A function that moves an object in memory
b) A utility that casts an lvalue to an rvalue reference
c) A function that deletes an object
d) A function that copies an object
Correct Answer: b) A utility that casts an lvalue to an rvalue reference
Explanation:
std::move does not actually move anything; it casts its argument to an rvalue reference, enabling move operations.

120. What is the 'noexcept' specifier?

a) Indicates a function does not throw exceptions
b) Indicates a function throws all exceptions
c) Indicates a function is virtual
d) Indicates a function is static
Correct Answer: a) Indicates a function does not throw exceptions
Explanation:
The 'noexcept' specifier tells the compiler that a function will not throw exceptions, allowing for optimizations.

121. Should move constructors and move assignment operators be noexcept?

a) Yes, highly recommended
b) No, never
c) Only if they are static
d) Only if they are virtual
Correct Answer: a) Yes, highly recommended
Explanation:
Marking move operations as noexcept is recommended because standard library containers may fall back to copying if moves can throw.

122. What is a defaulted function in C++11?

a) A function with default parameters
b) A special member function explicitly requested to be generated by the compiler using = default
c) A static function
d) A friend function
Correct Answer: b) A special member function explicitly requested to be generated by the compiler using = default
Explanation:
Using '= default' explicitly asks the compiler to generate the default implementation of a special member function.

123. What is a deleted function in C++11?

a) A function that is removed from memory
b) A function that is explicitly prohibited from being called using = delete
c) A static function
d) A friend function
Correct Answer: b) A function that is explicitly prohibited from being called using = delete
Explanation:
Using '= delete' prevents a function from being called, often used to disable copying or specific conversions.

124. How do you prevent a class from being copied?

a) Make all members private
b) Delete the copy constructor and copy assignment operator
c) Make the class abstract
d) Use the 'final' keyword
Correct Answer: b) Delete the copy constructor and copy assignment operator
Explanation:
Deleting the copy constructor and copy assignment operator (= delete) explicitly prevents copying of the class.

125. What is a trivial class?

a) A class with no members
b) A class that has a trivial default constructor, copy/move constructors, and destructor
c) A class with only static members
d) A class with only friend functions
Correct Answer: b) A class that has a trivial default constructor, copy/move constructors, and destructor
Explanation:
A trivial class has special member functions that perform no action, making it compatible with C-style memory operations like memcpy.

126. What is a standard-layout class?

a) A class with a specific memory layout compatible with C structs
b) A class with no members
c) A class with only static members
d) A class with only virtual functions
Correct Answer: a) A class with a specific memory layout compatible with C structs
Explanation:
Standard-layout classes have a memory layout that is compatible with C, allowing interoperability with C APIs.

127. What is a POD (Plain Old Data) type in C++?

a) A class that is both trivial and standard-layout
b) A class with only private members
c) A class with only virtual functions
d) A class with no constructors
Correct Answer: a) A class that is both trivial and standard-layout
Explanation:
POD types are trivial and standard-layout, meaning they behave like simple C structs and can be safely copied with memcpy.

128. What is the 'decltype' specifier?

a) Declares a new type
b) Inspects the declared type of an entity or the type and value category of an expression
c) Deletes a type
d) Defines a class
Correct Answer: b) Inspects the declared type of an entity or the type and value category of an expression
Explanation:
'decltype' is used to deduce the exact type of an expression, including references and const qualifiers.

129. What is 'auto' type deduction?

a) Automatic memory management
b) Compiler deduces the type of a variable from its initializer
c) Automatic inheritance
d) Automatic polymorphism
Correct Answer: b) Compiler deduces the type of a variable from its initializer
Explanation:
The 'auto' keyword allows the compiler to automatically deduce the type of a variable from its initialization expression.

130. What is a type alias in C++?

a) A new type created with 'typedef' or 'using'
b) A class with no members
c) A static member
d) A friend member
Correct Answer: a) A new type created with 'typedef' or 'using'
Explanation:
Type aliases provide alternative names for existing types, improving code readability using 'typedef' or the 'using' keyword.

131. Which is preferred for type aliases in modern C++?

a) typedef
b) using
c) auto
d) decltype
Correct Answer: b) using
Explanation:
The 'using' keyword is preferred over 'typedef' in modern C++ because it is clearer and supports templates (alias templates).

132. What is an alias template?

a) A template that cannot be instantiated
b) A type alias for a template, defined using 'using'
c) A static template
d) A friend template
Correct Answer: b) A type alias for a template, defined using 'using'
Explanation:
Alias templates allow creating a shorthand name for a specific instantiation of a template, e.g., using MyVec = std::vector;

133. What is the 'constexpr' specifier?

a) Indicates a value or function can be evaluated at compile time
b) Indicates a constant variable
c) Indicates a static variable
d) Indicates a virtual function
Correct Answer: a) Indicates a value or function can be evaluated at compile time
Explanation:
'constexpr' specifies that the value of a variable or the return value of a function can be computed at compile time.

134. Can a class member function be constexpr?

a) Yes
b) No
c) Only if it is static
d) Only if it is virtual
Correct Answer: a) Yes
Explanation:
Member functions can be constexpr if they meet certain constraints, allowing objects to be constructed at compile time.

135. What is the 'consteval' specifier in C++20?

a) Indicates a function must be evaluated at compile time
b) Indicates a function can be evaluated at compile time
c) Indicates a constant variable
d) Indicates a static variable
Correct Answer: a) Indicates a function must be evaluated at compile time
Explanation:
'consteval' specifies that every call to the function must produce a compile-time constant, unlike 'constexpr' which allows runtime evaluation.

136. What is the 'constinit' specifier in C++20?

a) Indicates a variable must be initialized with a constant expression
b) Indicates a variable is constant
c) Indicates a variable is static
d) Indicates a variable is virtual
Correct Answer: a) Indicates a variable must be initialized with a constant expression
Explanation:
'constinit' ensures a variable with static or thread-local storage duration is initialized with a constant expression, avoiding dynamic initialization.
← Previous: 400+ C++ Interview Questions MCQ
Next →: C++ Exception / Error Handling - MCQs
NewLatest C++ Basics MCQs

Latest C++ Basics MCQs

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

By MCQs Generator
New
No Image

C++ Inheritance and Polymorphism - MCQs

1. Which keyword is used to define a class in C++? a) class b) struct c) object d) define Show…

By MCQs Generator
New
No Image

400+ C++ Interview Questions MCQ

1. Which keyword is used to define a class in C++? a) class b) struct c) object d) define Show…

By MCQs Generator