250+ TOP MCQs on Copy Constructor and Answers

Basic Object Oriented Programming Questions and Answers on “Copy Constructor”.

1. Copy constructor is a constructor which ________________
a) Creates an object by copying values from any other object of same class
b) Creates an object by copying values from first object created for that class
c) Creates an object by copying values from another object of another class
d) Creates an object by initializing it with another previously created object of same class
Answer: d
Clarification: The object that has to be copied to new object must be previously created. The new object gets initialized with the same values as that of the object mentioned for being copied. The exact copy is made with values.

2. The copy constructor can be used to ____________
a) Initialize one object from another object of same type
b) Initialize one object from another object of different type
c) Initialize more than one object from another object of same type at a time
d) Initialize all the objects of a class to another object of another class
Answer: a
Clarification: The copy constructor has the most basic function to initialize the members of an object with same values as that of some previously created object. The object must be of same class.

3. If two classes have exactly same data members and member function and only they differ by class name. Can copy constructor be used to initialize one class object with another class object?
a) Yes, possible
b) Yes, because the members are same
c) No, not possible
d) No, but possible if constructor is also same
Answer: c
Clarification: The restriction for copy constructor is that it must be used with the object of same class. Even if the classes are exactly same the constructor won’t be able to access all the members of another class. Hence we can’t use object of another class for initialization.

4. The copy constructors can be used to ________
a) Copy an object so that it can be passed to a class
b) Copy an object so that it can be passed to a function
c) Copy an object so that it can be passed to another primitive type variable
d) Copy an object for type casting
Answer: b
Clarification: When an object is passed to a function, actually its copy is made in the function. To copy the values, copy constructor is used. Hence the object being passed and object being used in function are different.

5. Which returning an object, we can use ____________
a) Default constructor
b) Zero argument constructor
c) Parameterized constructor
d) Copy constructor
Answer: d
Clarification: While returning an object we can use the copy constructor. When we assign the return value to another object of same class then this copy constructor will be used. And all the members will be assigned the same values as that of the object being returned.

6. If programmer doesn’t define any copy constructor then _____________
a) Compiler provides an implicit copy constructor
b) Compiler gives an error
c) The objects can’t be assigned with another objects
d) The program gives run time error if copying is used
Answer: a
Clarification: The compiler provides an implicit copy constructor. It is not mandatory to always create an explicit copy constructor. The values are copied using implicit constructor only.

7. If a class implements some dynamic memory allocations and pointers then _____________
a) Copy constructor must be defined
b) Copy constructor must not be defined
c) Copy constructor can’t be defined
d) Copy constructor will not be used
Answer: a
Clarification: In the case where dynamic memory allocation is used, the copy constructor definition must be given. The implicit copy constructor is not capable of manipulating the dynamic memory and pointers. Explicit definition allows to manipulate the data as required.

8. What is the syntax of copy constructor?
a) classname (classname &obj){ /*constructor definition*/ }
b) classname (cont classname obj){ /*constructor definition*/ }
c) classname (cont classname &obj){ /*constructor definition*/ }
d) classname (cont &obj){ /*constructor definition*/ }
Answer: c
Clarification: The syntax must contain the class name first, followed by the classname as type and &object within parenthesis. Then comes the constructor body. The definition can be given as per requirements.

9. Object being passed to a copy constructor ___________
a) Must be passed by reference
b) Must be passed by value
c) Must be passed with integer type
d) Must not be mentioned in parameter list
Answer: a
Clarification: This is mandatory to pass the object by reference. Otherwise, the object will try to create another object to copy its values, in turn a constructor will be called, and this will keep on calling itself. This will cause the compiler to give out of memory error.

10. Out of memory error is given when the object _____________ to the copy constructor.
a) Is passed with & symbol
b) Is passed by reference
c) Is passed as
d) Is not passed by reference
Answer: d
Clarification: All the options given, directly or indirectly indicate that the object is being passed by reference. And if object is not passed by reference then the out of memory error is produced. Due to infinite constructor call of itself.

11. Copy constructor will be called whenever the compiler __________
a) Generates implicit code
b) Generates member function calls
c) Generates temporary object
d) Generates object operations
Answer: c
Clarification: Whenever the compiler creates a temporary object, copy constructor is used to copy the values from existing object to the temporary object.

12. The deep copy is possible only with the help of __________
a) Implicit copy constructor
b) User defined copy constructor
c) Parameterized constructor
d) Default constructor
Answer: b
Clarification: While using explicit copy constructor, the pointers of copied object point to the intended memory location. This is assured since the programmers themselves manipulate the addresses.

13. Can a copy constructor be made private?
a) Yes, always
b) Yes, if no other constructor is defined
c) No, never
d) No, private members can’t be accessed
Answer: a
Clarification: The copy constructor can be defined as private. If we make it private then the objects of the class can’t be copied. It can be used when a class used dynamic memory allocation.

14. The arguments to a copy constructor _____________
a) Must be const
b) Must not be cosnt
c) Must be integer type
d) Must be static
Answer: a
Clarification: The object should not be modified in the copy constructor. Because the object itself is being copied. When the object is returned from a function, the object must be a constant otherwise the compiler creates a temporary object which can die anytime.

15. Copy constructors are overloaded constructors.
a) True
b) False
Answer: a
Clarification: The copy constructors are always overloaded constructors. They have to be. All the classes have a default constructor and other constructors are basically overloaded constructors.

To practice basic questions and answers on all areas of Object Oriented Programming,

Leave a Reply

Your email address will not be published. Required fields are marked *