- UNIT-I
.
Unit-1 MCQ's
Object Oriented Programming Using C++
1. What is inheritance in C++?
a) A way to define new classes from existing classes
b) A mechanism to copy class members
c) A technique to encapsulate data
d) A method to delete objects
Answer: a) A way to define new classes from existing classes
2. What is the main advantage of inheritance?
a) Reduces memory usage
b) Enables reusability of code
c) Increases program speed
d) None of the above
Answer: b) Enables reusability of code
3. Which keyword is used for inheritance in C++?
a) inherit
b) extends
c) super
d) :
(colon)
Answer: d) :
(colon)
4. What is a base class?
a) A class that is inherited from another class
b) A class that is used to define objects
c) A class that provides functionalities to derived classes
d) A class that cannot be inherited
Answer: c) A class that provides functionalities to derived classes
5. What is a derived class?
a) A class that inherits from another class
b) A class that cannot have objects
c) A class that only has virtual functions
d) A class that does not use constructors
Answer: a) A class that inherits from another class
6. What type of inheritance is used when a class inherits from multiple base classes?
a) Single Inheritance
b) Multiple Inheritance
c) Hierarchical Inheritance
d) Multilevel Inheritance
Answer: b) Multiple Inheritance
7. Which type of inheritance represents a class hierarchy?
a) Multiple Inheritance
b) Hybrid Inheritance
c) Hierarchical Inheritance
d) Single Inheritance
Answer: c) Hierarchical Inheritance
8. What is multilevel inheritance?
a) A derived class inheriting from another derived class
b) A class inheriting multiple base classes
c) A class that cannot be instantiated
d) A class with multiple constructors
Answer: a) A derived class inheriting from another derived class
9. What happens when a derived class does not override a base class method?
a) It causes a compilation error
b) The base class method is used by default
c) The derived class cannot be instantiated
d) It results in undefined behavior
Answer: b) The base class method is used by default
10. How do you prevent a class from being inherited?
a) By using private
keyword
b) By declaring the class as final
c) By using sealed
keyword
d) By setting all functions as private
Answer: b) By declaring the class as final
11. Which keyword is used to access the base class constructor from a derived class?
a) this
b) super
c) base
d) :
(colon)
Answer: d) :
(colon)
12. What is the role of protected
access specifier in inheritance?
a) Allows private members to be accessed in derived class
b) Allows access only within the class
c) Allows access within the derived class but not outside
d) Makes a function virtual
Answer: c) Allows access within the derived class but not outside
13. What is method overriding in C++?
a) Defining a function with the same name in the derived class
b) Using multiple constructors in a class
c) Hiding a function from the base class
d) Using different function signatures in the same class
Answer: a) Defining a function with the same name in the derived class
14. What is function overloading?
a) Using the same function name with different parameters
b) Defining multiple classes with the same name
c) Using functions from different classes
d) A function that cannot be overridden
Answer: a) Using the same function name with different parameters
15. What is polymorphism in C++?
a) The ability of a function to perform different tasks
b) The ability of objects to communicate
c) The ability of functions to execute at compile time
d) The ability to inherit multiple classes
Answer: a) The ability of a function to perform different tasks
16. Which type of polymorphism is achieved using function overloading?
a) Runtime polymorphism
b) Compile-time polymorphism
c) Dynamic polymorphism
d) None of the above
Answer: b) Compile-time polymorphism
17. Which type of polymorphism is achieved using function overriding?
a) Static polymorphism
b) Dynamic polymorphism
c) Compile-time polymorphism
d) None of the above
Answer: b) Dynamic polymorphism
18. What keyword is used to define a virtual function?
a) override
b) virtual
c) dynamic
d) abstract
Answer: b) virtual
19. What is the purpose of a virtual function in C++?
a) Allows function overloading
b) Allows function overriding in derived classes
c) Prevents a function from being overridden
d) Increases memory efficiency
Answer: b) Allows function overriding in derived classes
20. What is the purpose of a pure virtual function?
a) Makes a function optional in derived classes
b) Allows a class to have both implemented and unimplemented functions
c) Forces derived classes to provide an implementation
d) Prevents the base class from being instantiated
Answer: c) Forces derived classes to provide an implementation
21. What is public inheritance in C++?
a) The base class members retain their access level in the derived class
b) All base class members become private in the derived class
c) All base class members become protected in the derived class
d) The derived class hides all base class members
Answer: a) The base class members retain their access level in the derived class
22. What happens to private members of a base class when inherited publicly?
a) They remain private in the derived class
b) They become protected
c) They become public
d) They are not accessible in the derived class
Answer: d) They are not accessible in the derived class
23. In protected inheritance, how are the public members of the base class treated in the derived class?
a) They remain public
b) They become private
c) They become protected
d) They are not accessible
Answer: c) They become protected
24. In private inheritance, how are the protected members of the base class treated in the derived class?
a) They remain protected
b) They become public
c) They become private
d) They are not inherited
Answer: c) They become private
25. In public inheritance, what happens to protected members of the base class?
a) They remain protected in the derived class
b) They become public in the derived class
c) They become private in the derived class
d) They are not inherited
Answer: a) They remain protected in the derived class
26. Which type of inheritance provides the most restricted access?
a) Public inheritance
b) Private inheritance
c) Protected inheritance
d) Multiple inheritance
Answer: b) Private inheritance
27. If a base class has a public member function, how is it inherited in private derivation?
a) As a private member
b) As a public member
c) As a protected member
d) It is not inherited
Answer: a) As a private member
28. If a base class has a protected member function, how is it inherited in protected derivation?
a) As a private member
b) As a public member
c) As a protected member
d) It is not inherited
Answer: c) As a protected member
29. In private inheritance, which base class members can be accessed directly in the derived class?
a) Only public members
b) Only protected and public members
c) Only private members
d) None, all members are inaccessible
Answer: b) Only protected and public members
30. Which access specifier allows derived classes to access the base class members but prevents direct access by objects?
a) Public
b) Private
c) Protected
d) Static
Answer: c) Protected
31. In protected inheritance, who can access the base class's protected members?
a) Only the base class itself
b) The derived class and its further derived classes
c) Any class in the program
d) Only objects of the derived class
Answer: b) The derived class and its further derived classes
32. What is the default inheritance mode in C++ if no access specifier is mentioned?
a) Public
b) Private
c) Protected
d) None, it causes an error
Answer: b) Private
33. If a base class has a private data member, how can it be accessed in the derived class?
a) It can be accessed directly
b) It cannot be accessed
c) Through a public or protected member function of the base class
d) By redeclaring it in the derived class
Answer: c) Through a public or protected member function of the base class
34. What happens if you try to access a private member of a base class in the derived class?
a) It compiles successfully
b) It results in a compilation error
c) The private member becomes protected
d) The private member becomes public
Answer: b) It results in a compilation error
35. In C++, what is the purpose of protected inheritance?
a) To make base class members accessible as public in the derived class
b) To prevent inheritance altogether
c) To allow derived classes to access base class members while preventing external access
d) To completely hide base class members from derived classes
Answer: c) To allow derived classes to access base class members while preventing external access
36. What will happen if a base class function is declared as private?
a) It can be accessed in the derived class
b) It cannot be accessed in the derived class
c) It can be accessed only using the super
keyword
d) It can be accessed only through another private function
Answer: b) It cannot be accessed in the derived class
37. What happens if a function in the derived class has the same name as a function in the base class?
a) It overrides the base class function
b) It hides the base class function
c) It causes a compilation error
d) Both functions are executed
Answer: b) It hides the base class function
38. In C++, when is private inheritance useful?
a) When we want to expose all base class members to derived classes
b) When we want to prevent base class members from being accessible outside the derived class
c) When we want to allow objects of derived classes to access base class members directly
d) When we want to make all functions virtual
Answer: b) When we want to prevent base class members from being accessible outside the derived class
39. What is the key difference between private and protected inheritance?
a) Private inheritance hides base class members from derived class
b) Private inheritance makes all base class members private in the derived class, whereas protected inheritance keeps them protected
c) Protected inheritance makes all base class members public in the derived class
d) Private inheritance is used only for multiple inheritance
Answer: b) Private inheritance makes all base class members private in the derived class, whereas protected inheritance keeps them protected
40. When a derived class is inherited as public
, what access do base class public
members get in the derived class?
a) Private
b) Public
c) Protected
d) None
Answer: b) Public
41. What is aggregation in C++?
a) A relationship where one class is part of another class
b) A relationship where one class owns another class
c) A relationship where objects can exist independently but are related
d) A relationship where one class completely controls another class
Answer: c) A relationship where objects can exist independently but are related
42. Which type of relationship does aggregation represent?
a) "is-a" relationship
b) "has-a" relationship
c) "uses-a" relationship
d) "inherits-a" relationship
Answer: b) "has-a" relationship
43. How is aggregation implemented in C++?
a) Using public inheritance
b) Using pointers or references to other objects
c) Using private inheritance
d) Using friend functions
Answer: b) Using pointers or references to other objects
44. Which of the following statements about aggregation is correct?
a) Aggregation requires deep copying of objects
b) Aggregated objects can exist independently of the main object
c) Aggregation is a strict form of inheritance
d) Aggregation does not allow access to aggregated objects
Answer: b) Aggregated objects can exist independently of the main object
45. What is the key difference between aggregation and composition?
a) In aggregation, the contained object can exist independently, while in composition, it cannot
b) In composition, the contained object can exist independently, while in aggregation, it cannot
c) Aggregation is a stronger relationship than composition
d) There is no difference between aggregation and composition
Answer: a) In aggregation, the contained object can exist independently, while in composition, it cannot
46. How do we denote aggregation in UML diagrams?
a) A solid line with an open diamond
b) A solid line with a filled diamond
c) A solid arrow pointing to the base class
d) A dashed arrow pointing to the base class
Answer: a) A solid line with an open diamond
47. What happens when an object is deleted in aggregation?
a) The aggregated object is also deleted
b) The aggregated object remains in memory
c) The aggregated object becomes private
d) The aggregated object cannot be accessed anymore
Answer: b) The aggregated object remains in memory
48. In aggregation, how is the lifetime of the contained object managed?
a) The container class controls the destruction of the contained object
b) The contained object is destroyed when the container class object is destroyed
c) The contained object has an independent lifecycle
d) The contained object must be deleted manually
Answer: c) The contained object has an independent lifecycle
49. Which of the following best describes aggregation?
a) Stronger than composition
b) Weaker than composition
c) Same as inheritance
d) No relationship between objects
Answer: b) Weaker than composition
50. Which statement about aggregation is false?
a) Aggregation is a type of association
b) Aggregation allows objects to be shared
c) Aggregation implies ownership
d) Aggregation can be implemented using pointers
Answer: c) Aggregation implies ownership
51. In C++, how do we establish aggregation between two classes?
a) By using friend
functions
b) By making one class a data member of another class using pointers
c) By inheriting one class from another
d) By using virtual functions
Answer: b) By making one class a data member of another class using pointers
52. Which of the following correctly represents aggregation in a class?
a) Correct use of aggregation
b) Incorrect, should use a pointer instead
c) Incorrect, should use inheritance
d) Incorrect, should use friend class
Answer: b) Incorrect, should use a pointer instead
53. What is a major advantage of aggregation?
a) Objects can be reused in multiple classes
b) Objects must be destroyed manually
c) Aggregation makes classes dependent on each other
d) Aggregation increases memory usage
Answer: a) Objects can be reused in multiple classes
54. Which of the following is NOT true about aggregation?
a) It allows object reusability
b) It creates a "part-of" relationship
c) It is a strong form of composition
d) It allows objects to exist independently
Answer: c) It is a strong form of composition
55. What is the correct syntax for implementing aggregation?
a) Correct implementation of aggregation
b) Incorrect, should use Engine engine;
c) Incorrect, should use inheritance
d) Incorrect, should use a friend function
Answer: a) Correct implementation of aggregation
56. Which of the following best describes an aggregated object?
a) It must always be created before the container class
b) It is always destroyed with the container class
c) It can be shared among multiple classes
d) It cannot be accessed outside the container class
Answer: c) It can be shared among multiple classes
57. What is the correct way to delete an aggregated object?
a) delete obj;
in the destructor
b) No need to delete manually
c) The compiler automatically deletes the object
d) The destructor of class A
deletes it
Answer: a) delete obj;
in the destructor
58. How does aggregation help in software design?
a) It promotes code reuse
b) It forces inheritance
c) It makes all classes tightly coupled
d) It prevents object creation
Answer: a) It promotes code reuse
59. What is the main limitation of aggregation?
a) Objects cannot be reused
b) Objects cannot exist independently
c) Objects must be deleted manually in dynamic allocation
d) Objects can only be created using static memory allocation
Answer: c) Objects must be deleted manually in dynamic allocation
60. In aggregation, what happens if the container class object is destroyed?
a) The aggregated object is also destroyed
b) The aggregated object remains in memory
c) The aggregated object becomes private
d) The aggregated object must be deleted manually
Answer: b) The aggregated object remains in memory
61. What is classification hierarchy in object-oriented programming?
a) A structure where objects are arranged based on their common characteristics
b) A way to manage memory in C++
c) A method to allocate dynamic memory
d) A technique for implementing multiple inheritance
Answer: a) A structure where objects are arranged based on their common characteristics
62. In a classification hierarchy, what kind of relationship exists between classes?
a) "has-a" relationship
b) "is-a" relationship
c) "uses-a" relationship
d) "owns-a" relationship
Answer: b) "is-a" relationship
63. What does composition in C++ represent?
a) "is-a" relationship
b) "has-a" relationship
c) "uses-a" relationship
d) "creates-a" relationship
Answer: b) "has-a" relationship
64. How does composition differ from aggregation?
a) In composition, the contained object cannot exist independently, while in aggregation, it can
b) In aggregation, the contained object cannot exist independently, while in composition, it can
c) Composition and aggregation are identical
d) Composition requires inheritance, but aggregation does not
Answer: a) In composition, the contained object cannot exist independently, while in aggregation, it can
65. Which type of hierarchy is best suited for modeling real-world inheritance?
a) Composition hierarchy
b) Classification hierarchy
c) Procedural hierarchy
d) Data-driven hierarchy
Answer: b) Classification hierarchy
66. Which keyword is commonly used in C++ to implement a classification hierarchy?
a) class
b) new
c) private
d) friend
Answer: a) class
67. What is the primary purpose of a classification hierarchy?
a) To allow data sharing between unrelated classes
b) To group objects based on shared properties and behaviors
c) To enforce data encapsulation
d) To create global functions
Answer: b) To group objects based on shared properties and behaviors
68. Which of the following best describes composition?
a) A strong form of aggregation where objects cannot exist independently
b) A weak relationship where objects exist independently
c) A method for accessing private members of a class
d) A technique used only in procedural programming
Answer: a) A strong form of aggregation where objects cannot exist independently
69. In C++, how is composition typically implemented?
a) By using member objects inside another class
b) By using pointers to another class
c) By inheriting from another class
d) By declaring global variables
Answer: a) By using member objects inside another class
70. In a classification hierarchy, what is the relationship between a base class and a derived class?
a) Composition
b) Aggregation
c) Inheritance
d) Association
Answer: c) Inheritance
71. How does a composition hierarchy affect object lifetimes?
a) Contained objects are automatically destroyed when the parent object is destroyed
b) Contained objects remain in memory even after the parent object is destroyed
c) Contained objects must be deleted manually
d) Contained objects cannot be accessed after the parent object is deleted
Answer: a) Contained objects are automatically destroyed when the parent object is destroyed
72. What is a key disadvantage of using a classification hierarchy?
a) Increases code reusability
b) Can lead to deep inheritance trees that are difficult to manage
c) Reduces the need for polymorphism
d) Makes object-oriented design impossible
Answer: b) Can lead to deep inheritance trees that are difficult to manage
73. What is an advantage of using a composition hierarchy over a classification hierarchy?
a) Promotes better encapsulation and code reusability
b) Eliminates the need for constructors
c) Allows direct modification of private members
d) Supports multiple inheritance
Answer: a) Promotes better encapsulation and code reusability
74. What happens if a composed object is dynamically allocated?
a) It must be deleted manually
b) It is automatically destroyed when the parent object is destroyed
c) It remains in memory forever
d) It cannot be deleted
Answer: a) It must be deleted manually
75. What is the main similarity between composition and classification hierarchies?
a) Both promote code reuse
b) Both require the use of pointers
c) Both are implemented using friend
functions
d) Both enforce strict data encapsulation
Answer: a) Both promote code reuse
76. Which of the following statements about classification hierarchies is true?
a) They allow objects to inherit attributes and behaviors from parent classes
b) They require all objects to be dynamically allocated
c) They use "has-a" relationships instead of "is-a" relationships
d) They do not support method overriding
Answer: a) They allow objects to inherit attributes and behaviors from parent classes
77. How does polymorphism relate to classification hierarchies?
a) It allows derived classes to override base class methods
b) It forces all classes to use the same constructor
c) It prevents code reusability
d) It eliminates the need for virtual functions
Answer: a) It allows derived classes to override base class methods
78. In a classification hierarchy, what is the primary role of a base class?
a) To define common attributes and behaviors for derived classes
b) To act as a container for object aggregation
c) To prevent object instantiation
d) To implement private inheritance
Answer: a) To define common attributes and behaviors for derived classes
79. What is a key advantage of composition over inheritance in a classification hierarchy?
a) It reduces dependencies between classes
b) It increases complexity
c) It enforces deep inheritance trees
d) It prevents code reuse
Answer: a) It reduces dependencies between classes
80. When should composition be preferred over a classification hierarchy?
a) When an object "has-a" another object rather than "is-a"
b) When all objects share the same methods
c) When an object requires multiple inheritance
d) When object creation is not required
Answer: a) When an object "has-a" another object rather than "is-a"
61. What is a classification hierarchy in object-oriented programming?
a) A structure where objects are arranged based on their common characteristics
b) A way to manage memory in C++
c) A method to allocate dynamic memory
d) A technique for implementing multiple inheritance
Answer: a) A structure where objects are arranged based on their common characteristics
62. What kind of relationship exists between classes in a classification hierarchy?
a) "has-a" relationship
b) "is-a" relationship
c) "uses-a" relationship
d) "owns-a" relationship
Answer: b) "is-a" relationship
63. What does composition in C++ represent?
a) "is-a" relationship
b) "has-a" relationship
c) "uses-a" relationship
d) "creates-a" relationship
Answer: b) "has-a" relationship
64. How does composition differ from aggregation?
a) In composition, the contained object cannot exist independently, while in aggregation, it can
b) In aggregation, the contained object cannot exist independently, while in composition, it can
c) Composition and aggregation are identical
d) Composition requires inheritance, but aggregation does not
Answer: a) In composition, the contained object cannot exist independently, while in aggregation, it can
65. What is the primary purpose of a classification hierarchy?
a) To allow data sharing between unrelated classes
b) To group objects based on shared properties and behaviors
c) To enforce data encapsulation
d) To create global functions
Answer: b) To group objects based on shared properties and behaviors
66. Which of the following best describes composition?
a) A strong form of aggregation where objects cannot exist independently
b) A weak relationship where objects exist independently
c) A method for accessing private members of a class
d) A technique used only in procedural programming
Answer: a) A strong form of aggregation where objects cannot exist independently
67. In C++, how is composition typically implemented?
a) By using member objects inside another class
b) By using pointers to another class
c) By inheriting from another class
d) By declaring global variables
Answer: a) By using member objects inside another class
68. What is the relationship between a base class and a derived class in a classification hierarchy?
a) Composition
b) Aggregation
c) Inheritance
d) Association
Answer: c) Inheritance
69. How does a composition hierarchy affect object lifetimes?
a) Contained objects are automatically destroyed when the parent object is destroyed
b) Contained objects remain in memory even after the parent object is destroyed
c) Contained objects must be deleted manually
d) Contained objects cannot be accessed after the parent object is deleted
Answer: a) Contained objects are automatically destroyed when the parent object is destroyed
70. What is a key disadvantage of using a classification hierarchy?
a) Increases code reusability
b) Can lead to deep inheritance trees that are difficult to manage
c) Reduces the need for polymorphism
d) Makes object-oriented design impossible
Answer: b) Can lead to deep inheritance trees that are difficult to manage
71. What is an advantage of using a composition hierarchy over a classification hierarchy?
a) Promotes better encapsulation and code reusability
b) Eliminates the need for constructors
c) Allows direct modification of private members
d) Supports multiple inheritance
Answer: a) Promotes better encapsulation and code reusability
72. What happens if a composed object is dynamically allocated?
a) It must be deleted manually
b) It is automatically destroyed when the parent object is destroyed
c) It remains in memory forever
d) It cannot be deleted
Answer: a) It must be deleted manually
73. Which of the following statements about classification hierarchies is true?
a) They allow objects to inherit attributes and behaviors from parent classes
b) They require all objects to be dynamically allocated
c) They use "has-a" relationships instead of "is-a" relationships
d) They do not support method overriding
Answer: a) They allow objects to inherit attributes and behaviors from parent classes
74. How does polymorphism relate to classification hierarchies?
a) It allows derived classes to override base class methods
b) It forces all classes to use the same constructor
c) It prevents code reusability
d) It eliminates the need for virtual functions
Answer: a) It allows derived classes to override base class methods
75. What is the primary role of a base class in a classification hierarchy?
a) To define common attributes and behaviors for derived classes
b) To act as a container for object aggregation
c) To prevent object instantiation
d) To implement private inheritance
Answer: a) To define common attributes and behaviors for derived classes
76. What is a key advantage of composition over inheritance in a classification hierarchy?
a) It reduces dependencies between classes
b) It increases complexity
c) It enforces deep inheritance trees
d) It prevents code reuse
Answer: a) It reduces dependencies between classes
77. When should composition be preferred over a classification hierarchy?
a) When an object "has-a" another object rather than "is-a"
b) When all objects share the same methods
c) When an object requires multiple inheritance
d) When object creation is not required
Answer: a) When an object "has-a" another object rather than "is-a"
78. In a classification hierarchy, how do objects of derived classes behave?
a) They inherit all the attributes and methods of their parent class
b) They lose their unique attributes
c) They cannot be instantiated
d) They must override all base class methods
Answer: a) They inherit all the attributes and methods of their parent class
79. What is a downside of deep classification hierarchies?
a) Increased code complexity and maintenance difficulty
b) Reduced flexibility in code reuse
c) Elimination of encapsulation
d) Increased speed of program execution
Answer: a) Increased code complexity and maintenance difficulty
80. Which relationship best describes classification hierarchy?
a) "is-a" relationship
b) "has-a" relationship
c) "part-of" relationship
d) "depends-on" relationship
Answer: a) "is-a" relationship
81. What is polymorphism in C++?
a) The ability of different objects to respond differently to the same function call
b) The ability to define multiple variables of the same type
c) A technique to allocate memory dynamically
d) A method to hide data from other classes
Answer: a) The ability of different objects to respond differently to the same function call
82. Which of the following is a type of polymorphism in C++?
a) Compile-time polymorphism
b) Runtime polymorphism
c) Both a and b
d) None of the above
Answer: c) Both a and b
83. Which of the following is an example of compile-time polymorphism?
a) Function overloading
b) Virtual functions
c) Operator overloading
d) Both a and c
Answer: d) Both a and c
84. Which of the following is an example of runtime polymorphism?
a) Function overloading
b) Operator overloading
c) Virtual functions
d) Constructor overloading
Answer: c) Virtual functions
85. How is compile-time polymorphism achieved in C++?
a) Using function overloading
b) Using operator overloading
c) Using templates
d) All of the above
Answer: d) All of the above
86. How is runtime polymorphism achieved in C++?
a) Using function overloading
b) Using virtual functions and method overriding
c) Using constructor overloading
d) Using inline functions
Answer: b) Using virtual functions and method overriding
87. What keyword is used to enable runtime polymorphism in C++?
a) static
b) virtual
c) friend
d) const
Answer: b) virtual
88. What is function overloading?
a) Defining multiple functions with the same name but different parameters
b) Using the same function for different classes
c) Redefining a function in a derived class
d) None of the above
Answer: a) Defining multiple functions with the same name but different parameters
89. Which operator can be overloaded in C++?
a) +
b) -
c) *
d) All of the above
Answer: d) All of the above
90. What is operator overloading?
a) Giving additional meaning to an existing operator
b) Using an operator without any change
c) Creating new operators
d) Overriding base class methods
Answer: a) Giving additional meaning to an existing operator
91. Which function is overridden in runtime polymorphism?
a) A static function
b) A virtual function
c) A friend function
d) A global function
Answer: b) A virtual function
92. What is function overriding?
a) Redefining a base class function in a derived class
b) Using multiple functions with the same name but different parameters
c) Defining a function inside another function
d) Overloading an operator
Answer: a) Redefining a base class function in a derived class
93. What determines which function is called in runtime polymorphism?
a) Compiler
b) Function name
c) Object type at runtime
d) None of the above
Answer: c) Object type at runtime
94. Which of the following cannot be overloaded in C++?
a) ==
b) ()
c) ::
d) +
Answer: c) ::
95. Which function in C++ is resolved at runtime?
a) Overloaded function
b) Virtual function
c) Friend function
d) Inline function
Answer: b) Virtual function
96. What is the purpose of the virtual
keyword?
a) To define a function that can be overridden in a derived class
b) To prevent function overriding
c) To make a function inline
d) To declare a function as a friend function
Answer: a) To define a function that can be overridden in a derived class
97. Which function is responsible for achieving dynamic binding in C++?
a) Friend function
b) Static function
c) Virtual function
d) Inline function
Answer: c) Virtual function
98. What is the role of a virtual table (vtable) in C++?
a) It stores pointers to virtual functions of a class
b) It is used for function overloading
c) It helps in memory allocation
d) It prevents runtime polymorphism
Answer: a) It stores pointers to virtual functions of a class
99. What happens if a base class function is not declared as virtual but is overridden in the derived class?
a) Base class function is called using a base class pointer
b) Derived class function is always called
c) Compiler gives an error
d) None of the above
Answer: a) Base class function is called using a base class pointer
100. Which of the following best defines method overriding?
a) Using the same function name but different arguments
b) Redefining a base class function in a derived class with the same function signature
c) Defining a function inside a base class
d) Defining a global function with the same name
Answer: b) Redefining a base class function in a derived class with the same function signature
101. What is method polymorphism?
a) The ability to create multiple methods with the same name but different implementations
b) The ability to define private methods in a class
c) The process of converting data types
d) The use of inline functions in a class
Answer: a) The ability to create multiple methods with the same name but different implementations
102. Which of the following is NOT a type of method polymorphism?
a) Method overloading
b) Method overriding
c) Method extension
d) None of the above
Answer: c) Method extension
103. What is another term for compile-time polymorphism?
a) Dynamic binding
b) Late binding
c) Early binding
d) Runtime polymorphism
Answer: c) Early binding
104. Which of the following is an example of method polymorphism?
a) Function overloading
b) Function overriding
c) Both a and b
d) None of the above
Answer: c) Both a and b
105. What is function overloading?
a) Creating multiple functions with the same name but different parameters
b) Creating a function that takes variable arguments
c) Creating multiple classes with the same function
d) Defining the same function inside multiple files
Answer: a) Creating multiple functions with the same name but different parameters
106. What is function overriding?
a) A derived class redefines a base class function with the same name and parameters
b) Using different functions with the same name in a class
c) Creating multiple constructors in a class
d) A function calling another function recursively
Answer: a) A derived class redefines a base class function with the same name and parameters
107. Which keyword is used to override a method in Java but not required in C++?
a) override
b) final
c) virtual
d) const
Answer: a) override
108. In method overriding, which function is called when using a base class pointer to refer to a derived class object?
a) The base class function
b) The derived class function
c) Both functions are executed
d) Compiler error
Answer: b) The derived class function
109. In C++, how is runtime polymorphism achieved?
a) Using virtual functions
b) Using function overloading
c) Using static functions
d) Using friend functions
Answer: a) Using virtual functions
110. What is the default binding mechanism used in C++ when calling functions?
a) Dynamic binding
b) Static binding
c) Late binding
d) None of the above
Answer: b) Static binding
111. What is method hiding in C++?
a) When a derived class defines a function with the same name but different parameters as in the base class
b) When a function is made private in a class
c) When a function is hidden using a friend function
d) When a function is defined inside another function
Answer: a) When a derived class defines a function with the same name but different parameters as in the base class
112. Which of the following statements is TRUE about function overriding?
a) Overridden functions must have the same function signature
b) Overridden functions must have different return types
c) Overridden functions must be declared as static
d) Overridden functions cannot be virtual
Answer: a) Overridden functions must have the same function signature
113. Which of the following enables runtime polymorphism?
a) Function overloading
b) Operator overloading
c) Virtual functions
d) Inline functions
Answer: c) Virtual functions
114. What is method dispatch?
a) The process of calling the correct function at runtime in polymorphism
b) The process of defining multiple functions with the same name
c) The process of creating virtual tables
d) The process of accessing static members
Answer: a) The process of calling the correct function at runtime in polymorphism
115. What happens if a base class function is not declared as virtual
, but a derived class function with the same name is present?
a) The base class function is always called
b) The derived class function is called
c) Both functions execute together
d) Compiler error
Answer: a) The base class function is always called
116. Which of the following statements is FALSE about method polymorphism?
a) Function overloading occurs at compile time
b) Function overriding occurs at runtime
c) Virtual functions are used for function overloading
d) Static binding is used in function overloading
Answer: c) Virtual functions are used for function overloading
117. What is the purpose of a virtual table (vtable) in C++?
a) It stores pointers to virtual functions for dynamic dispatch
b) It is used to store static functions
c) It is used for compile-time polymorphism
d) It prevents function overloading
Answer: a) It stores pointers to virtual functions for dynamic dispatch
118. Which of the following statements is TRUE about function overloading?
a) Overloaded functions must have different parameter lists
b) Overloaded functions must have different return types
c) Overloaded functions must always be in different classes
d) Overloaded functions must always be declared as virtual
Answer: a) Overloaded functions must have different parameter lists
119. What happens when an overloaded function has default arguments?
a) It may cause ambiguity when calling the function
b) It results in a compilation error
c) The function must be declared virtual
d) The function must be declared static
Answer: a) It may cause ambiguity when calling the function
120. Which concept allows a base class reference to refer to a derived class object and call overridden functions?
a) Static binding
b) Dynamic binding
c) Function overloading
d) Operator overloading
Answer: b) Dynamic binding
121. What is operator overloading in C++?
a) Using operators with default behavior
b) Using operators with user-defined behavior
c) Using operators only for arithmetic operations
d) Using multiple operators together in a function
Answer: b) Using operators with user-defined behavior
122. Which keyword is used for operator overloading?
a) override
b) virtual
c) friend
d) operator
Answer: d) operator
123. Which of the following operators CANNOT be overloaded in C++?
a) +
b) -
c) ::
d) *
Answer: c) ::
124. How is operator overloading implemented in C++?
a) By defining a new function with the keyword operator
b) By modifying the behavior of an existing operator directly
c) By defining a friend
function only
d) By defining a global function
Answer: a) By defining a new function with the keyword operator
125. Which of the following operators can be overloaded?
a) sizeof
b) .
(dot operator)
c) ->
(arrow operator)
d) +=
Answer: d) +=
126. What is the return type of an overloaded operator++
when implemented as a member function?
a) void
b) int
c) class type
d) float
Answer: c) class type
127. What is the purpose of overloading the assignment (=
) operator?
a) To allocate dynamic memory before assignment
b) To allow assignment between objects of different classes
c) To perform deep copy if necessary
d) All of the above
Answer: d) All of the above
128. Which operator must be overloaded as a member function?
a) >>
b) <<
c) =
d) +
Answer: c) =
129. Which of the following statements is TRUE about operator overloading?
a) Only unary operators can be overloaded
b) Operator overloading is always implemented using friend
functions
c) Operators cannot be overloaded for built-in types
d) Overloading an operator changes its precedence
Answer: c) Operators cannot be overloaded for built-in types
130. How can we overload an operator using a friend function?
a) By making the operator function a friend
of the class
b) By defining the operator function inside the class
c) By using the virtual
keyword
d) By defining the function as static
Answer: a) By making the operator function a friend
of the class
131. What is the difference between overloading operator++
in prefix and postfix form?
a) The prefix version takes no parameters, while the postfix version takes an int
parameter
b) The prefix version takes an int
parameter, while the postfix version takes no parameters
c) There is no difference
d) The postfix version returns an integer
Answer: a) The prefix version takes no parameters, while the postfix version takes an int
parameter
132. Which operator is typically overloaded for stream input (>>
) and output (<<
)?
a) friend function
b) virtual function
c) static function
d) member function
Answer: a) friend function
133. What is required to overload an operator in C++?
a) The operator function must be defined inside the class
b) The operator function must have at least one operand as a class object
c) The operator function must be defined using static
keyword
d) The operator function must be recursive
Answer: b) The operator function must have at least one operand as a class object
134. Which of the following statements is FALSE about operator overloading?
a) Overloaded operators retain their original precedence
b) Overloaded operators can change the number of operands they take
c) At least one operand must be a user-defined type
d) Some operators cannot be overloaded
Answer: b) Overloaded operators can change the number of operands they take
135. How is the binary +
operator overloaded as a member function?
a) ClassName operator+(const ClassName &obj);
b) ClassName operator+();
c) friend ClassName operator+(const ClassName &obj);
d) void operator+(ClassName &obj);
Answer: a) ClassName operator+(const ClassName &obj);
136. Why do we use friend
functions in operator overloading?
a) To overload operators that require two operands where the left operand is not a class object
b) To increase performance
c) To access private members without inheritance
d) To make functions inline
Answer: a) To overload operators that require two operands where the left operand is not a class object
137. Which operator is commonly overloaded to implement deep copy?
a) +
b) =
c) -
d) *
Answer: b) =
138. What is the correct way to overload the ++
operator as a postfix version?
a) ClassName operator++();
b) ClassName operator++(int);
c) ClassName operator++(double);
d) void operator++();
Answer: b) ClassName operator++(int);
139. What is the best way to return the result of an overloaded +
operator?
a) By returning *this
b) By returning void
c) By returning a new object
d) By returning a pointer
Answer: c) By returning a new object
140. What is the purpose of operator overloading in C++?
a) To extend the functionality of built-in operators for user-defined data types
b) To replace the need for functions
c) To make C++ code shorter
d) To change the precedence of operators
Answer: a) To extend the functionality of built-in operators for user-defined data types
No comments:
Post a Comment