- UNIT-I
.
Unit-1 MCQ's
Object Oriented Programming Using C++
1. What is a class in OOP?
a) A function
b) A user-defined data type
c) A pre-defined structure
d) A variable
Answer: b) A user-defined data type
2. What is an object in OOP?
a) A template for a class
b) A function
c) An instance of a class
d) A data type
Answer: c) An instance of a class
3. Which keyword is used to define a class in C++?
a) structure
b) define
c) class
d) object
Answer: c) class
4. What is the default access specifier for class members in C++?
a) public
b) private
c) protected
d) default
Answer: b) private
5. What is the correct syntax to create an object of a class in C++?
a) class obj;
b) object obj;
c) ClassName obj;
d) new ClassName();
Answer: c) ClassName obj;
6. What is a constructor?
a) A function used to allocate memory
b) A function automatically called when an object is created
c) A function used to destroy an object
d) A function that must return a value
Answer: b) A function automatically called when an object is created
7. What is a destructor?
a) A function used to allocate memory
b) A function automatically called when an object is destroyed
c) A function that constructs an object
d) A function that must return a value
Answer: b) A function automatically called when an object is destroyed
8. How many constructors can a class have?
a) Only one
b) At most two
c) Any number
d) None
Answer: c) Any number
9. What is the return type of a constructor?
a) void
b) int
c) char
d) It does not have a return type
Answer: d) It does not have a return type
10. What is an instance variable?
a) A variable declared inside a function
b) A variable declared inside a class but outside a method
c) A static variable
d) A global variable
Answer: b) A variable declared inside a class but outside a method
11. Which access specifier allows members to be accessed only within the same class?
a) public
b) private
c) protected
d) global
Answer: b) private
12. Which access specifier allows members to be accessed in derived classes?
a) public
b) private
c) protected
d) static
Answer: c) protected
13. What is a copy constructor?
a) A constructor that copies values from one object to another
b) A constructor that creates an empty object
c) A constructor that deletes an object
d) A constructor that accepts no parameters
Answer: a) A constructor that copies values from one object to another
14. Which of the following statements about classes and objects is FALSE?
a) A class is a blueprint for objects
b) Objects are instances of a class
c) A class must have at least one object
d) Objects can access public members of a class
Answer: c) A class must have at least one object
15. Which keyword is used to dynamically allocate memory for an object in C++?
a) malloc
b) calloc
c) new
d) create
Answer: c) new
16. How do you declare a constant member function in C++?
a) void display();
b) void display() const;
c) const void display();
d) void const display();
Answer: b) void display() const;
17. Which of the following statements is TRUE about static members in a class?
a) They belong to the class, not to any specific object
b) They are only accessible within the same class
c) They must always be private
d) They are automatically deleted when an object is destroyed
Answer: a) They belong to the class, not to any specific object
18. How many objects can be created from a single class?
a) Only one
b) At most two
c) Unlimited
d) None
Answer: c) Unlimited
19. What happens when an object is passed by reference to a function?
a) A new object is created
b) The function works on the original object
c) The function creates a copy of the object
d) The object is deleted
Answer: b) The function works on the original object
20. What is an abstract class?
a) A class with no variables
b) A class that cannot be instantiated
c) A class with only private methods
d) A class that must be static
Answer: b) A class that cannot be instantiated
21. What is a friend function in C++?
a) A function that can access private and protected members of a class
b) A function that belongs to two classes
c) A function that allows two objects to share data
d) A function that is inherited by all classes
Answer: a) A function that can access private and protected members of a class
22. Can a constructor be private in C++?
a) Yes
b) No
Answer: a) Yes
23. What is a pure virtual function in C++?
a) A function without a body
b) A function that cannot be overridden
c) A function that is always static
d) A function that is always inline
Answer: a) A function without a body
24. Can an object access a private member of its class directly?
a) Yes
b) No
Answer: b) No
25. Which function is called automatically when an object goes out of scope?
a) Constructor
b) Destructor
c) Member function
d) Static function
Answer: b) Destructor
26. Which of the following access specifiers allows access within the same class but restricts it from outside?
a) public
b) private
c) protected
d) static
Answer: b) private
27. Information hiding in C++ prevents:
a) Unauthorized access to data
b) Use of functions in a program
c) The creation of objects
d) The use of variables
Answer: a) Unauthorized access to data
28. Which keyword is used to access private members of a class within the same class?
a) private
b) public
c) this
d) static
Answer: c) this
29. How does information hiding enhance security in OOP?
a) By making all variables global
b) By keeping sensitive data hidden from external access
c) By allowing direct access to all class members
d) By using only global functions
Answer: b) By keeping sensitive data hidden from external access
30. How can information hiding be bypassed in C++?
a) By using friend functions
b) By declaring variables as public
c) By using global variables
d) By making class members static
Answer: a) By using friend functions
31. What is the main objective of information hiding?
a) To expose implementation details
b) To provide a clear interface while hiding implementation details
c) To increase execution time
d) To allow multiple inheritance
Answer: b) To provide a clear interface while hiding implementation details
32. In C++, what access level do private members of a base class have in a derived class?
a) public
b) private
c) protected
d) static
Answer: b) private
33. Which OOP feature allows a class to hide its implementation details while exposing functionality?
a) Polymorphism
b) Inheritance
c) Information hiding
d) Function overloading
Answer: c) Information hiding
34. Which of the following does NOT help achieve information hiding?
a) Using private
access specifier
b) Using getter and setter methods
c) Declaring variables as public
d) Using encapsulation
Answer: c) Declaring variables as public
35. What is the role of encapsulation in information hiding?
a) It allows unrestricted access to class members
b) It hides data from direct access and binds it with methods
c) It prevents object creation
d) It enables multiple inheritance
Answer: b) It hides data from direct access and binds it with methods
36. Which of the following statements about information hiding is TRUE?
a) It allows direct modification of private members
b) It helps in maintaining security and integrity of data
c) It is not useful in OOP
d) It makes code harder to manage
Answer: b) It helps in maintaining security and integrity of data
37. In C++, what is the main reason to use information hiding?
a) To increase performance
b) To provide a controlled interface and prevent unauthorized access
c) To make all data accessible
d) To allow multiple inheritance
Answer: b) To provide a controlled interface and prevent unauthorized access
38. How does a private variable differ from a public variable in terms of accessibility?
a) Private variables can be accessed from anywhere
b) Public variables cannot be accessed outside the class
c) Private variables can only be accessed within the class
d) Private variables can be accessed from derived classes
Answer: c) Private variables can only be accessed within the class
39. Which of the following techniques is used to access private members of a class in C++?
a) Friend functions
b) Static methods
c) Global variables
d) Operator overloading
Answer: a) Friend functions
40. What is the impact of information hiding on software maintenance?
a) It makes code harder to understand
b) It simplifies debugging and reduces complexity
c) It makes software less secure
d) It prevents modular programming
Answer: b) It simplifies debugging and reduces complexity
41. Which of the following is an essential characteristic of an Abstract Data Type (ADT)?
a) It specifies only operations, not implementation
b) It defines exact memory allocation
c) It provides direct hardware access
d) It can only use primitive data types
Answer: a) It specifies only operations, not implementation
42. Which of the following data structures can be considered an ADT?
a) Stack
b) Queue
c) Linked List
d) All of the above
Answer: d) All of the above
43. Why are ADTs important in programming?
a) They increase hardware dependency
b) They provide a clear interface for data manipulation
c) They restrict code reusability
d) They slow down program execution
Answer: b) They provide a clear interface for data manipulation
44. Which ADT uses the First In, First Out (FIFO) principle?
a) Stack
b) Queue
c) Hash Table
d) Tree
Answer: b) Queue
45. Which of the following is NOT an operation associated with a Stack ADT?
a) Push
b) Pop
c) Peek
d) Enqueue
Answer: d) Enqueue
46. What is the primary difference between a Stack and a Queue?
a) Stack follows FIFO, Queue follows LIFO
b) Stack follows LIFO, Queue follows FIFO
c) Stack allows insertion only, Queue allows deletion only
d) Stack requires more memory than a Queue
Answer: b) Stack follows LIFO, Queue follows FIFO
47. Which of the following best describes an ADT?
a) A collection of data and the operations allowed on that data
b) A concrete implementation of a data structure
c) A programming language feature
d) A type of file system
Answer: a) A collection of data and the operations allowed on that data
48. What type of data structure is used to implement recursion?
a) Queue
b) Stack
c) Graph
d) Tree
Answer: b) Stack
49. A Priority Queue is an extension of which ADT?
a) Stack
b) Queue
c) List
d) Tree
Answer: b) Queue
50. In a circular queue, what happens when the rear reaches the last position?
a) The queue overflows
b) The rear moves to the front if there is space
c) The front moves to the rear position
d) The queue gets deleted
Answer: b) The rear moves to the front if there is space
51. How does an ADT improve modularity?
a) By keeping implementation details hidden
b) By exposing all data members to users
c) By making data structures hardware-dependent
d) By reducing the number of operations allowed
Answer: a) By keeping implementation details hidden
52. Which of the following is an example of a non-linear ADT?
a) Stack
b) Queue
c) Tree
d) Array
Answer: c) Tree
53. Which of the following ADTs can be implemented using both arrays and linked lists?
a) Stack
b) Queue
c) Linked List
d) Both a and b
Answer: d) Both a and b
54. Which operation in a queue is used to remove an element?
a) Push
b) Dequeue
c) Pop
d) Insert
Answer: b) Dequeue
55. What is a Deque (Double-Ended Queue)?
a) A queue where elements can only be inserted at the rear
b) A queue where elements can be inserted and removed from both ends
c) A queue where elements can only be removed from the front
d) A queue with a fixed number of elements
Answer: b) A queue where elements can be inserted and removed from both ends
56. Which of the following ADTs does NOT allow random access?
a) Stack
b) Array
c) Linked List
d) Queue
Answer: a) Stack
57. Which of the following operations is unique to a priority queue?
a) Insert
b) Delete
c) Sorting elements based on priority
d) Traversal
Answer: c) Sorting elements based on priority
58. What is the primary use of a Linked List in ADTs?
a) To provide dynamic memory allocation
b) To allow direct indexing of elements
c) To implement database storage
d) To increase hardware dependency
Answer: a) To provide dynamic memory allocation
59. What is the worst-case time complexity of searching for an element in a linked list?
a) O(1)
b) O(log n)
c) O(n)
d) O(n²)
Answer: c) O(n)
60. Why is an Abstract Data Type important in software design?
a) It reduces hardware dependency
b) It allows data structures to be used without knowing their internal implementation
c) It slows down program execution
d) It restricts the use of object-oriented programming
Answer: b) It allows data structures to be used without knowing their internal implementation
61. What is a class in object-oriented programming (OOP)?
a) A template for creating objects
b) A function that stores data
c) A type of variable
d) A programming language
Answer: a) A template for creating objects
62. What is an object in OOP?
a) A function that performs an action
b) An instance of a class
c) A keyword in C++
d) A type of data structure
Answer: b) An instance of a class
63. Which keyword is used to define a class in C++?
a) object
b) class
c) struct
d) template
Answer: b) class
64. What are attributes in a class?
a) Functions that define behavior
b) Data members that store object properties
c) Loops used in class methods
d) A special type of variable
Answer: b) Data members that store object properties
65. In C++, how do you create an object of a class named Car
?
a) object Car;
b) Car myCar;
c) class myCar;
d) new Car();
Answer: b) Car myCar;
66. How do attributes help in object-oriented programming?
a) They define an object's properties
b) They allow inheritance
c) They make code more complex
d) They eliminate the need for functions
Answer: a) They define an object's properties
67. Which of the following is NOT a valid characteristic of a class?
a) It defines objects
b) It contains attributes and methods
c) It must always have a constructor
d) It is a blueprint for objects
Answer: c) It must always have a constructor
68. In C++, what is the default access modifier for class members?
a) public
b) private
c) protected
d) static
Answer: b) private
69. What is the relationship between a class and an object?
a) A class is an instance of an object
b) An object is an instance of a class
c) Both are the same
d) Objects and classes are unrelated
Answer: b) An object is an instance of a class
70. Which of the following correctly defines a class with an attribute in C++?
a) Correct syntax
b) Incorrect syntax
c) Requires public
access modifier
d) Requires a constructor
Answer: a) Correct syntax
71. What is the term for combining data and functions inside a class?
a) Inheritance
b) Encapsulation
c) Abstraction
d) Polymorphism
Answer: b) Encapsulation
72. What is a constructor in C++?
a) A function that initializes an object
b) A function that destroys an object
c) A function that defines a class
d) A function that accesses private attributes
Answer: a) A function that initializes an object
73. How can attributes of a class be accessed in C++?
a) Using functions inside the class
b) Using the return
statement
c) Directly modifying private variables
d) Using delete
keyword
Answer: a) Using functions inside the class
74. What does an object contain?
a) Only attributes
b) Only methods
c) Both attributes and methods
d) Only constructors
Answer: c) Both attributes and methods
75. Which of the following correctly initializes an attribute inside a class?
a) Correct syntax
b) Incorrect syntax
c) Requires constructor initialization
d) Attributes cannot be initialized inside a class
Answer: a) Correct syntax
76. What happens when an object is created from a class?
a) Memory is allocated for attributes and methods
b) The class is compiled again
c) The class becomes an object
d) Nothing happens until the object is used
Answer: a) Memory is allocated for attributes and methods
77. Which access modifier allows attributes to be accessed only within the same class?
a) private
b) public
c) protected
d) static
Answer: a) private
78. How do you define a public attribute inside a class?
a) Correct syntax
b) Incorrect syntax
c) speed
should be private
d) Requires a constructor
Answer: a) Correct syntax
79. Which keyword is used to create an object dynamically in C++?
a) new
b) malloc
c) allocate
d) init
Answer: a) new
80. Which of the following is NOT true about attributes in OOP?
a) They define the state of an object
b) They are also called data members
c) They must be public
d) They can be private, public, or protected
Answer: c) They must be public
81. What is the purpose of an attribute in a class?
a) To store information about an object
b) To execute functions
c) To inherit other classes
d) To implement polymorphism
Answer: a) To store information about an object
82. Which of the following best represents an object?
a) A blueprint for creating a class
b) A real-world entity created from a class
c) A function that modifies attributes
d) A variable that stores methods
Answer: b) A real-world entity created from a class
83. How do you access an attribute inside a class method?
a) Using this->attributeName
b) Using className.attributeName
c) Using global.attributeName
d) Using return.attributeName
Answer: a) Using this->attributeName
84. What happens if a class does not have any attributes?
a) It cannot have objects
b) It can still have methods
c) It will not compile
d) It becomes an interface
Answer: b) It can still have methods
85. What is the default value of an integer attribute in C++?
a) 0
b) NULL
c) Garbage value
d) Undefined
Answer: c) Garbage value
86. What is a method in object-oriented programming (OOP)?
a) A variable that holds data
b) A function that belongs to a class
c) A data structure
d) A keyword in C++
Answer: b) A function that belongs to a class
87. How are methods defined inside a class in C++?
a) Inside the class body
b) Outside the class body
c) Both inside and outside the class body
d) Methods cannot be defined in C++
Answer: c) Both inside and outside the class body
88. Which keyword is used to declare a function inside a class in C++?
a) function
b) method
c) void
d) No special keyword is required
Answer: d) No special keyword is required
89. What is the return type of a constructor in C++?
a) int
b) void
c) It does not have a return type
d) float
Answer: c) It does not have a return type
90. How do you define a method outside a class in C++?
a) By using the ::
(scope resolution) operator
b) By defining it inside the class body
c) By declaring it inside main()
d) By using the return
keyword
Answer: a) By using the ::
(scope resolution) operator
91. Which of the following correctly declares a C++ class?
a) Correct declaration
b) Incorrect, requires a public section
c) Incorrect, requires a constructor
d) Incorrect, class
keyword is not valid
Answer: a) Correct declaration
92. What does a C++ class declaration consist of?
a) Class name
b) Data members and member functions
c) Access specifiers
d) All of the above
Answer: d) All of the above
93. What is the purpose of the public
keyword in a class?
a) To restrict access to members
b) To allow access to members from outside the class
c) To declare a constructor
d) To make variables constant
Answer: b) To allow access to members from outside the class
94. Which statement is TRUE about a class constructor?
a) It must have a return type
b) It is automatically called when an object is created
c) It must be explicitly called in the main function
d) It cannot be overloaded
Answer: b) It is automatically called when an object is created
95. What is the "state" of an object in OOP?
a) The set of attributes describing the object
b) The functions it contains
c) The methods it executes
d) The access modifiers it uses
Answer: a) The set of attributes describing the object
96. What defines the "identity" of an object?
a) The values of its attributes
b) Its unique memory address
c) Its methods
d) Its class name
Answer: b) Its unique memory address
97. What defines the "behavior" of an object?
a) The attributes it contains
b) The actions it can perform through methods
c) The class it belongs to
d) The memory address it is stored in
Answer: b) The actions it can perform through methods
98. In C++, how do you call a method on an object?
a) objectName.methodName();
b) methodName.objectName();
c) className.methodName();
d) methodName();
Answer: a) objectName.methodName();
99. Which of the following correctly defines a class method in C++?
a) Correct definition
b) Incorrect, missing return type
c) Incorrect, methods must be private
d) Incorrect, void
is not a valid type
Answer: a) Correct definition
100. What is a default constructor?
a) A constructor with parameters
b) A constructor that takes no arguments
c) A constructor that must be defined manually
d) A constructor that always initializes attributes to zero
Answer: b) A constructor that takes no arguments
101. How can we pass arguments to a class method?
a) By using method parameters
b) By using return
keyword
c) By modifying private attributes directly
d) By calling another method inside the class
Answer: a) By using method parameters
102. Which of the following statements about methods is TRUE?
a) Methods define the behavior of an object
b) Methods cannot return values
c) Methods must be private
d) Methods and attributes are the same
Answer: a) Methods define the behavior of an object
103. Which method is used to destroy an object in C++?
a) Constructor
b) Destructor
c) delete()
d) remove()
Answer: b) Destructor
104. How is a destructor defined in C++?
a) Using ~
followed by the class name
b) Using destroy
keyword
c) Using delete
keyword
d) Using remove
keyword
Answer: a) Using ~
followed by the class name
105. What is function overloading in C++?
a) Defining multiple functions with the same name but different parameters
b) Replacing an existing function in the base class
c) Calling a function inside another function
d) Using multiple return types in a function
Answer: a) Defining multiple functions with the same name but different parameters
106. What happens when an object is instantiated from a class?
a) Memory is allocated for its attributes
b) A method must be called to create it
c) It inherits all methods from another class
d) Nothing happens until it is used
Answer: a) Memory is allocated for its attributes
107. How can an object call a private method in C++?
a) It cannot call a private method directly
b) By using the public
keyword
c) By using this->
d) By calling it from another class
Answer: a) It cannot call a private method directly
108. What is the main advantage of defining methods inside a class?
a) They provide controlled access to attributes
b) They slow down execution speed
c) They increase program size
d) They eliminate the need for constructors
Answer: a) They provide controlled access to attributes
109. What is the default return type of a method in C++?
a) void
b) int
c) None (it must be specified)
d) float
Answer: c) None (it must be specified)
110. How do you declare a method outside a class but define it later?
a) By using a method prototype in the class
b) By defining it inside main()
c) By using friend
keyword
d) By using static
keyword
Answer: a) By using a method prototype in the class
110. What is a constructor in C++?
a) A special function that initializes an object
b) A function that deletes objects
c) A function used only for inheritance
d) A function that returns an integer
Answer: a) A special function that initializes an object
111. What is a destructor in C++?
a) A function that creates objects
b) A special function that is called when an object is destroyed
c) A function that changes the value of private members
d) A function that must always return void
Answer: b) A special function that is called when an object is destroyed
112. How is a constructor named in C++?
a) Same as the class name
b) Using the constructor
keyword
c) Using the new
keyword
d) Using the initialize
keyword
Answer: a) Same as the class name
113. How is a destructor named in C++?
a) Using the delete
keyword
b) Using the ~
followed by the class name
c) Using the destroy
keyword
d) Same as the class name without the ~
symbol
Answer: b) Using the ~
followed by the class name
114. How many times is a constructor called when an object is created?
a) 0
b) 1
c) 2
d) Depends on the number of member functions
Answer: b) 1
115. How many destructors can a class have?
a) Only one
b) More than one
c) None
d) As many as required
Answer: a) Only one
116. When is a destructor automatically called?
a) When an object is created
b) When an object goes out of scope or is deleted
c) When a function is called
d) When a program starts
Answer: b) When an object goes out of scope or is deleted
117. Can a destructor take parameters in C++?
a) Yes
b) No
c) Only in certain cases
d) Only if declared as public
Answer: b) No
118. What is the return type of a constructor?
a) int
b) void
c) float
d) A constructor does not have a return type
Answer: d) A constructor does not have a return type
119. What is the return type of a destructor?
a) void
b) int
c) float
d) A destructor does not have a return type
Answer: d) A destructor does not have a return type
120. What is a parameterized constructor?
a) A constructor that does not accept parameters
b) A constructor that accepts arguments
c) A constructor that has a return value
d) A constructor that calls another function
Answer: b) A constructor that accepts arguments
121. What is a default constructor?
a) A constructor that takes no parameters
b) A constructor that initializes all variables to zero
c) A constructor that always takes at least one parameter
d) A constructor that must be explicitly defined
Answer: a) A constructor that takes no parameters
122. What is a copy constructor?
a) A constructor that copies values from one object to another
b) A constructor that initializes values to zero
c) A constructor that always takes two arguments
d) A constructor that returns a copy of an object
Answer: a) A constructor that copies values from one object to another
123. What is the purpose of a copy constructor?
a) To initialize a new object from an existing object
b) To destroy an object
c) To allocate memory dynamically
d) To overload a function
Answer: a) To initialize a new object from an existing object
124. How is a copy constructor defined in C++?
a) ClassName(ClassName &obj);
b) ClassName(ClassName obj);
c) ClassName &operator=(ClassName &obj);
d) void ClassName(ClassName &obj);
Answer: a) ClassName(ClassName &obj);
125. What happens if you do not define a constructor in a class?
a) The program will not compile
b) The compiler provides a default constructor
c) The object cannot be created
d) A syntax error occurs
Answer: b) The compiler provides a default constructor
126. Which constructor gets called when an object is created using another object?
a) Default constructor
b) Copy constructor
c) Parameterized constructor
d) Destructor
Answer: b) Copy constructor
127. Can a destructor be overloaded?
a) Yes
b) No
c) Only if the class has multiple objects
d) Only if the class has virtual functions
Answer: b) No
128. Can a constructor be private in C++?
a) Yes, but only in special cases
b) No, constructors must be public
c) Yes, it is commonly used in Singleton design pattern
d) No, it causes a compilation error
Answer: c) Yes, it is commonly used in Singleton design pattern
129. Which constructor is called when an object is created without parameters?
a) Default constructor
b) Parameterized constructor
c) Copy constructor
d) Destructor
Answer: a) Default constructor
130. How do you explicitly invoke a destructor?
a) Using delete
keyword
b) Using ~ClassName()
c) Using free()
function
d) You cannot explicitly invoke a destructor
Answer: d) You cannot explicitly invoke a destructor
131. What happens when a class contains a dynamically allocated memory but does not define a destructor?
a) Memory leak occurs
b) Compiler automatically deallocates memory
c) The program runs normally without issues
d) The constructor deallocates memory automatically
Answer: a) Memory leak occurs
132. Can a destructor be declared as virtual
?
a) Yes, for proper cleanup in inheritance
b) No, destructors cannot be virtual
c) Only if the class has static members
d) Only in friend classes
Answer: a) Yes, for proper cleanup in inheritance
133. What happens if a destructor is virtual
in a base class?
a) The derived class destructor is called first
b) The base class destructor is called first
c) The destructor is ignored
d) Compilation error occurs
Answer: a) The derived class destructor is called first
135. What does "instantiation of an object" mean in C++?
a) Declaring a class
b) Creating an object of a class
c) Deleting an object
d) Declaring a function
Answer: b) Creating an object of a class
136. Which keyword is used to create an object dynamically in C++?
a) malloc
b) alloc
c) new
d) create
Answer: c) new
137. What happens when an object is instantiated?
a) A class definition is executed
b) Memory is allocated for the object
c) The constructor is called
d) Both (b) and (c)
Answer: d) Both (b) and (c)
138. How do you instantiate an object of a class named Car
?
a) Car myCar();
b) Car myCar;
c) Car = new myCar;
d) Car object();
Answer: b) Car myCar;
139. How do you instantiate an object dynamically in C++?
a) Car myCar();
b) Car* myCar = new Car();
c) Car myCar = new Car();
d) new Car myCar;
Answer: b) Car* myCar = new Car();
140. What is the correct way to deallocate an object created using new
?
a) delete obj;
b) free(obj);
c) remove(obj);
d) dealloc(obj);
Answer: a) delete obj;
141. What happens if you forget to delete a dynamically allocated object?
a) It will be deleted automatically
b) It causes a segmentation fault
c) It leads to a memory leak
d) The destructor will handle it
Answer: c) It leads to a memory leak
142. What is the default access specifier for class members in C++?
a) public
b) private
c) protected
d) default
Answer: b) private
143. Can multiple objects of a class be instantiated?
a) No, only one object per class
b) Yes, but only if using pointers
c) Yes, multiple objects can be instantiated
d) No, classes cannot have objects
Answer: c) Yes, multiple objects can be instantiated
144. What is a default parameter in C++?
a) A parameter that must always be provided
b) A parameter with a predefined value if not provided
c) A parameter that cannot be changed
d) A function parameter that returns a value
Answer: b) A parameter with a predefined value if not provided
145. Where should default parameter values be specified in C++?
a) Inside the function body
b) In the function declaration
c) In the function call
d) Anywhere in the program
Answer: b) In the function declaration
146. What is the correct syntax for setting a default parameter value?
a) void func(int a = 10);
b) void func(int a) { a = 10; }
c) void func(int = 10 a);
d) void func = (int a 10);
Answer: a) void func(int a = 10);
147. In which scenario are default parameters useful?
a) When a function must always take a value
b) When a function can work with or without a specific parameter
c) When function overloading is not allowed
d) When creating multiple copies of a function
Answer: b) When a function can work with or without a specific parameter
148. Can default parameter values be used with constructors?
a) Yes
b) No
c) Only in certain cases
d) Only with destructors
Answer: a) Yes
149. What is the output of the following C++ code?
a) Hello, !
b) Hello, User!
c) Compilation error
d) Hello, name!
Answer: b) Hello, User!
150. Can default parameters be used with function overloading?
a) Yes, but there must be no ambiguity
b) No, it is not allowed
c) Only if all parameters have default values
d) Only with void
functions
Answer: a) Yes, but there must be no ambiguity
151. What is the issue with the following function declaration?
a) No issue
b) Default parameter must be on the rightmost side
c) b
must also have a default value
d) Both (b) and (c)
Answer: d) Both (b) and (c)
152. Can default values be assigned to all parameters of a function?
a) Yes
b) No
c) Only for primitive data types
d) Only for the first parameter
Answer: a) Yes
153. Which of the following is correct about default parameter values?
a) They can be overridden at function call
b) They cannot be changed
c) They must be provided in function definition
d) They work only with constructors
Answer: a) They can be overridden at function call
154. What happens if a function with a default parameter is called without arguments?
a) The function call fails
b) The default value is used
c) A runtime error occurs
d) It depends on the compiler
Answer: b) The default value is used
155. Can default parameter values be used in both function declaration and definition?
a) Yes, but only in one place
b) No, they must be used in both
c) Only in the function definition
d) Only in the function declaration
Answer: a) Yes, but only in one place
156. What is the effect of setting a default parameter value?
a) It forces the function to always use the default value
b) It allows calling the function with or without that parameter
c) It prevents function overloading
d) It makes the function return void
Answer: b) It allows calling the function with or without that parameter
157. What is the maximum number of default parameters a function can have?
a) 1
b) 2
c) No limit
d) Only in constructors
Answer: c) No limit
158. What are the two main types of objects in C++?
a) Static and Dynamic
b) Public and Private
c) Primitive and Non-primitive
d) Temporary and Permanent
Answer: a) Static and Dynamic
159. Which of the following statements about static objects is correct?
a) They are allocated memory at runtime
b) They exist throughout the program's execution
c) They are allocated on the heap
d) They must be deleted manually
Answer: b) They exist throughout the program's execution
160. What is a temporary object in C++?
a) An object that exists only for the duration of an expression
b) An object that is manually allocated
c) An object that is static
d) An object that is used in constructors only
Answer: a) An object that exists only for the duration of an expression
161. What is garbage collection in C++?
a) Automatic removal of unused objects
b) Manual deletion of dynamically allocated objects
c) A process that compacts memory
d) A feature to track object references
Answer: b) Manual deletion of dynamically allocated objects
162. How does C++ manage memory deallocation?
a) Using an automatic garbage collector
b) Using delete
or delete[]
c) Using free()
function
d) Using a background thread
Answer: b) Using delete
or delete[]
163. What happens when a dynamically allocated object is not deleted?
a) The compiler deletes it automatically
b) A segmentation fault occurs
c) It leads to a memory leak
d) It gets deallocated at the end of the program
Answer: c) It leads to a memory leak
164. Which keyword is used for dynamic memory allocation in C++?
a) malloc
b) alloc
c) new
d) memory
Answer: c) new
165. Which operator is used to release dynamically allocated memory?
a) free
b) delete
c) remove
d) erase
Answer: b) delete
166. What is the correct syntax to allocate an array dynamically?
a) int *arr = new int[10];
b) int arr = new int[10];
c) int arr[] = new int(10);
d) int *arr = malloc(10 * sizeof(int));
Answer: a) int *arr = new int[10];
167. How do you delete a dynamically allocated array?
a) delete arr;
b) delete[] arr;
c) free(arr);
d) remove(arr);
Answer: b) delete[] arr;
168. What is the default return type of new
in C++?
a) int*
b) void*
c) char*
d) It depends on the allocated data type
Answer: d) It depends on the allocated data type
169. What will happen if new
fails to allocate memory?
a) It throws a bad_alloc
exception
b) It returns NULL
c) It crashes the program
d) It creates an empty object
Answer: a) It throws a bad_alloc
exception
170. What is an abstract class in C++?
a) A class that has no member functions
b) A class that contains at least one pure virtual function
c) A class that cannot be used as a base class
d) A class that can only have private members
Answer: b) A class that contains at least one pure virtual function
171. What is a pure virtual function?
a) A function that has no return type
b) A function with default implementation
c) A function declared as virtual void func() = 0;
d) A function that is not inherited
Answer: c) A function declared as virtual void func() = 0;
172. What happens if a class contains a pure virtual function?
a) The class must implement the function
b) The class cannot have any other functions
c) The class becomes an abstract class
d) The class cannot have constructors
Answer: c) The class becomes an abstract class
173. Can an abstract class have a constructor?
a) Yes
b) No
c) Only if it does not contain any member variables
d) Only if it is private
Answer: a) Yes
174. Can an abstract class have normal functions?
a) Yes
b) No
c) Only if they are private
d) Only if they are static
Answer: a) Yes
175. Can we create an object of an abstract class?
a) Yes
b) No
c) Only if it contains at least one constructor
d) Only if it is inherited
Answer: b) No
176. What must a derived class do when inheriting from an abstract class?
a) Override all pure virtual functions
b) Declare itself as abstract
c) Use the final
keyword
d) Avoid overriding functions
Answer: a) Override all pure virtual functions
177. What will happen if a derived class does not override a pure virtual function?
a) It will be automatically overridden
b) It will cause a runtime error
c) The derived class will also become abstract
d) It will lead to undefined behavior
Answer: c) The derived class will also become abstract
178. Which of the following best describes an abstract class?
a) A class that can never be instantiated
b) A class that contains only private methods
c) A class that provides a full implementation
d) A class that cannot be inherited
Answer: a) A class that can never be instantiated
179. What keyword is used to declare an abstract class method?
a) static
b) virtual
c) abstract
d) override
Answer: b) virtual
180. What happens if a pure virtual function is not implemented in the derived class?
a) Compilation error
b) Runtime error
c) The base class executes the function instead
d) The function gets deleted
Answer: a) Compilation error
No comments:
Post a Comment