Test Series - oops

Test Number 11/78

Q: How many types of constructors are available, in general, in any language?
A. 2
B. 3
C. 4
D. 5
Solution: There are 3 types of constructors in general, namely, default constructors, parameterized constructors and copy constructors. Default one is called whenever an object is created without arguments.
Q: Which constructor is called while assigning some object with another?
A. Default
B. Parameterized
C. Copy
D. Direct assignment is used
Solution: Copy constructor is used while an object is assigned with another. This is mandatory since we can’t decide which member should be assigned to which member value. By using copy constructor, we can assign the values in required form.
Q: It’s necessary to pass object by reference in copy constructor because ____________
A. Constructor is not called in pass by reference
B. Constructor is called in pass by reference only
C. It passes the address of new constructor to be created
D. It passes the address of new object to be created
Solution: Object must be passed by reference to copy constructor because constructor is not called in pass by reference. Otherwise, in pass by value, a temporary object will be created which in turn will try to call its constructor that is already being used. This results in creating infinite number of objects and hence memory shortage error will be shown.
Q: Which specifier applies only to the constructors?
A. Public
B. Protected
C. Implicit
D. Explicit
Solution: The keyword explicit can be used while defining the constructor only. This is used to suppress the implicit call to the constructor. It ensures that the constructors are being called with the default syntax only (i.e. only by using object and constructor name).
Q: Which among the following is true?
A. Default constructor can’t be defined by the programmer
B. Default parameters constructor isn’t equivalent to the default constructor
C. Default constructor can be called explicitly
D. Default constructor is and always called implicitly only
Solution: Default constructors can be called explicitly anytime. They are specifically used to allocate memory space for the object in memory, in general. It is not necessary that these should always be called implicitly.
Q: Which type of constructor can’t have a return type?
A. Default
B. Parameterized
C. Copy
D. Constructors don’t have a return type
Solution: Constructors don’t return any value. Those are special functions, whose return type is not defined, not even void. This is so because the constructors are meant to initialize the members of class and not to perform some task which can return some value to newly created object.
Q: Why do we use static constructors?
A. To initialize the static members of class
B. To initialize all the members with static value
C. To delete the static members when not required
D. To clear all the static members initialized values
Solution: Static constructors help in initializing the static members of the class. This is provided because the static members are not considered to be property of the object, rather they are considered as the property of class.
Q: When and how many times a static constructor is called?
A. Created at time of object destruction
B. Called at first time when an object is created and only one time
C. Called at first time when an object is created and called with every new object creation
D. Called whenever an object go out of scope
Solution: Those are called at very first call of object creation. That is called only one time because the value of static members must be retained and continued from the time it gets created.
Q: Which among the following is true for static constructor?
A. Static constructors are called with every new object
B. Static constructors are used initialize data members to zero always
C. Static constructors can’t be parameterized constructors
D. Static constructors can be used to initialize the non-static members also
Solution: Static constructors can’t be parameterized constructors. Those are used to initialize the value of static members only. And that must be a definite value. Accepting arguments may make it possible that static members loses their value with every new object being created.
Q: If constructors of a class are defined in private access, then __________
A. The class can’t be inherited
B. The class can be inherited
C. Instance can be created only in another class
D. Instance can be created anywhere in the program
Solution: If the constructors are defined in private access, then the class can’t be inherited by other classes. This is useful when the class contains static members only. The instances can never be created.

You Have Score    /10