Test Series - oops

Test Number 69/78

Q: Which among the following is true for constructors overloading?
A. Constructors can’t be overloaded
B. Constructors can be overloaded using different signatures
C. Constructors can be overloaded with same signatures
D. Constructors can be overloaded with different return types
Solution: The constructors can be overloaded only if the definitions have different signatures. Constructors doesn’t have any return type so can’t be overloaded using return type. If constructors have same signature then it will produce a compile time error.
Q: If a constructors should be capable of creating objects without argument and with arguments, which is a good alternative for this purpose?
A. Use zero argument constructor
B. Use constructor with one parameter
C. Use constructor with all default arguments
D. Use default constructor
Solution: The constructor should use all the default arguments. This will allow the constructor to be called even if no arguments are passed. And if arguments are passed, those will be accepted instead of the default values.
Q: Which among the following can be used in place of default constructor?
A. constructorName(int x, int y=0)
B. constructorName(int x=0, int y=0)
C. constructorName(int x=0, int y)
D. constructorName(int x, int y)
Solution: For a parameterized constructor to be used as a default constructor, it must have all the default arguments. This makes the constructor to have optional arguments which are not mandatory to be passed.
Q: Can a class have more than one function with all the default arguments?
A. Yes, always
B. Yes, if argument list is different
C. No, because constructors overloading doesn’t depend on argument list
D. No, never
Solution: A single class can never have more than once constructor with all the default arguments. This is because it will make all those constructors as a default constructor. And when an object is created with zero arguments then it will create ambiguity.
Q: Which is the correct syntax for using default arguments with the constructor?
A. default constructorName(default int x=0)
B. constructorName(default int x=0)
C. constructorName(int x=0)
D. constructorName()
Solution: The constructors using the default arguments must initialize the arguments in the argument list. This is to make the constructor use the default value when no arguments are passed. If no arguments are listed then it is a default constructor.
Q: Which constructor among the following will be called if a call is made like className(5,’a’);?
A. className(int x=5,char c=’a’);
B. int className(int x, char c, char d);
C. className(int x, char c, int y);
D. char className(char c,int x);
Solution: The syntax given is passing two parameters to the constructor call. One value is of integer type and another of character type. Hence the constructor with arguments of int and char type should be called. There is only one option that first accepts integer value and then a character value. Hence that constructor will be called.
Q: Which constructor definition will produce a compile time error?
A. className(int x=0);
B. className(char c);
C. className(int x=0,char c);
D. className(char c,int x=0);
Solution: The default arguments, just like with member functions, must be listed at last in the argument list. Hence this will produce a compile time error. The compiler doesn’t allow the definition to be executed.
Q: If there is a constructor with all the default arguments and arguments are not passed then _______
A. The default values given will not be used
B. Then all the null values will be used
C. Then all the default values given will be used
D. Then compiler will produce an error
Solution: The constructors will use the default values listed for use. The null values are not used because those are not specified. Though if it was compiler provided default constructor, then it would have initialized the members to null or zero values.
Q: Which is the correct statement for default constructors?
A. The constructors with all the default arguments
B. The constructors with all the null and zero values
C. The constructors which can’t be defined by programmer
D. The constructors with zero arguments
Solution: The closest answer to the question is that a default constructor is a constructor with zero arguments. But this is not the actual case. Actually the constructors provided by the compiler are default constructors. And the constructors with zero arguments defined by the programmer are zero argument constructors.
Q: Which is a good alternative instead of having one zero argument constructor and one single argument constructor with default argument?
A. No constructor defined
B. One default value constructor
C. Defining the default constructor
D. Using one constructor with two arguments
Solution: The constructor with one default argument can be the best alternative. This is because the constructor with one default value will do the work for both the default constructor and one argument constructor.

You Have Score    /10