Test Series - oops

Test Number 13/78

Q: 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
Solution: Whenever the compiler creates a temporary object, copy constructor is used to copy the values from existing object to the temporary object.
Q: 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
Solution: 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.
Q: 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
Solution: 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.
Q: The arguments to a copy constructor _____________
A. Must be const
B. Must not be cosnt
C. Must be integer type
D. Must be static
Solution: 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.
Q: Copy constructors are overloaded constructors.
A. True
B. False
C. 1
D. 0
Solution: 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.
Q: Which among the following best describes constructor overloading?
A. Defining one constructor in each class of a program
B. Defining more than one constructor in single class
C. Defining more than one constructor in single class with different signature
D. Defining destructor with each constructor
Solution: If more than one constructors are defined in a class with same signature, then that results in error. The signatures must be different. So that the constructors can be differentiated.
Q: Can constructors be overloaded in derived class?
A. Yes, always
B. Yes, if derived class has no constructor
C. No, programmer can’t do it
D. No, never
Solution: The constructor must be having the same name as that of a class. Hence a constructor of one class can’t even be defined in another class. Since the constructors can’t be defined in derived class, it can’t be overloaded too, in derived class.
Q: Does constructor overloading include different return types for constructors to be overloaded?
A. Yes, if return types are different, signature becomes different
B. Yes, because return types can differentiate two functions
C. No, return type can’t differentiate two functions
D. No, constructors doesn’t have any return type
Solution: The constructors doesn’t have any return type. When we can’t have return type of a constructor, overloading based on the return type is not possible. Hence only parameters can be different.
Q: Which among the following is possible way to overload constructor?
A. Define default constructor, 1 parameter constructor and 2 parameter constructor
B. Define default constructor, zero argument constructor and 1 parameter constructor
C. Define default constructor, and 2 other parameterized constructors with same signature
D. Define 2 default constructors
Solution: All the constructors defined in a class must have a different signature in order to be overloaded. Here one default and other parameterized constructors are used, wherein one is of only one parameter and other accepts two. Hence overloading is possible.
Q: Which constructor will be called from the object created in the code below?

class A
{ 
	int i;
	A()
	{ 
		i=0; cout<<i; 
	}
	A(int x=0)
	{ 
		i=x;  cout<<I;  
	}
};
A obj1;
A. Default constructor
B. Parameterized constructor
C. Compile time error
D. Run time error
Solution: When a default constructor is defined and another constructor with 1 default value argument is defined, creating object without parameter will create ambiguity for the compiler. The compiler won’t be able to decide which constructor should be called, hence compile time error.

You Have Score    /10