Test Series - oops

Test Number 12/78

Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: Which returning an object, we can use ____________
A. Default constructor
B. Zero argument constructor
C. Parameterized constructor
D. Copy constructor
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: 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*/ }
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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.

You Have Score    /10