This set of Object Oriented Programming (OOPs) using C++ Multiple Choice Questions & Answers (MCQs) focuses on “Default Arguments”.
1. What are default arguments?
a) Arguments which are not mandatory to be passed
b) Arguments with default value that aren’t mandatory to be passed
c) Arguments which are not passed to functions
d) Arguments which always take same data value
Explanation: The arguments which are assigned with some default value. Since some value is already given, it is not mandatory to pass those arguments. They can be used directly.
2. Which is the correct condition for the default arguments?
a) Those must be declared as last arguments in argument list
b) Those must be declared first in the argument list
c) Those can be defined anywhere in the argument list
d) Those are declared inside the function definition
Explanation: The default arguments must be declared at last in the argument list. This is to ensure that the arguments doesn’t create ambiguity. The normal arguments should be passed first.
3. If a member function have to be made both zero argument and parameterized constructor, which among the following can be the best option?
a) Two normal and one default argument
b) At least one default argument
c) Exactly one default argument
d) Make all the arguments default
Explanation: All the arguments must be made default. This will make sure that none of the arguments are mandatory to be passed. Which in turn means that the function can work without any argument and can be passed with arguments too.
4. Which among the following function can be called without arguments?
a) void add(int x, int y=0)
b) void add(int=0)
c) void add(int x=0, int y=0)
d) void add(char c)
Explanation: For the function to be called without arguments, either it must have zero arguments or it must have all the default arguments. Here the function in option void add(int x=0, int y=0) have all the default arguments and hence can be called directly with zero argument.
5. If a function have all the default arguments but still some values are passed to the function then ______________
a) The function will use the values passed to it
b) The function will use the default values as those are local
c) The function can use any value whichever is higher
d) The function will choose the minimum values
Explanation: The function will use the values passed explicitly to it. The default values will be ignored. The default values are used only in case the values are not passed explicitly to the function.
6. Which among the following is correct?
a) void test(int x=0, int y, int z=0)
b) void test(int x=0, int=0)
c) void test(int x, int y=0)
d) void test(int x=’c, int y)
Explanation: The default arguments must be mentioned at last in the argument list. Also, the type of values assigned must match with the argument type. All the default arguments must be mentioned at last, none of the normal arguments should come in between the default arguments list.
7. What function will be called with the independent syntax “test(5,6,7);”?
a) void test(int x, int y)
b) void test(int x=0, int y, int z)
c) int test(int x=0, y=0, z=0)
d) void test(int x, int y, int z=0)
Explanation: There are three arguments that are getting passed to the function test(). Only the last option have all the default argument at last in the argument list. And the total number of the arguments is three. The third option is wrong because the return type is int and the syntax given is independent which means it doesn’t return any value.
8. Which among the following is a wrong call to the function void test(int x, int y=0, int z=0)?
Explanation: The function must be passed with at least one argument. There is two default arguments and one normal argument which must be passed with some value. Hence the third call to the function is wrong as it doesn’t pass even a single parameter to the function
9. Default arguments are _________________________
a) Only allowed in the parameter list of the function declaration
b) Only allowed in the return type of the function declaration
c) Only allowed with the class name definition
d) Only allowed with the integer type values
Explanation: The default arguments are only allowed in the parameter list of the function arguments. This rule was not applicable in the beginning versions of c++ but later from c++ 14th version it has been implemented. This is the only way to use default arguments.
10. Which among the following is false for default arguments?
a) Those are not allowed with a declaration of pointer to functions
b) Those are not allowed with the reference to functions
c) Those are not allowed with the typedef declarations
d) Those are allowed with pointer and reference to function declaration
Explanation: The statements given are true because that is a feature given to make the programming more flexible and have some security with accidental changes at same time. The last option is false because it is not a rule defined. It is an opposite statement to the rules defined for default arguments.
11. The non-template functions can be added with default arguments to already declared functions ____________________
a) If and only if the function is declared again in the same scope
b) If and only if the function is declared only once in the same scope
c) If and only if the function is declared in different scope
d) If and only if the function is declared twice in the program
Explanation: The non-template functions can also be added with default arguments. This can be done even if the functions were defined earlier. This is because the call to the function won’t be affected. The function can still be used in the same way as it was used earlier.
12. The using declaration __________
a) Doesn’t carry over the default values
b) Carries over the known default arguments
c) Carries over only the normal arguments
d) Carries over only few default arguments
Explanation: The using-declaration carries over all the known default arguments. This is a common feature as the usage doesn’t gets affected even if the default arguments are added. This comes under flexible programming.
13. The names given to the default arguments are only looked up and ________________ and are bound during declaration.
a) Checked for availability
b) Checked for random access
c) Checked for accessibility
d) Checked for feasibility
Explanation: The names given to the default arguments are bound at time of declaration but are only checked for accessibility and to get bounded. This is mainly to bind those members during declaration.
14. The default argument get bound during declaration ________________
a) And are never executed
b) And are executed simultaneously
c) But are executed only if priority is given
d) But are executed during function call
Explanation: The default argument are bound at the time of declaration. That is an implicit functioning. But those are executed only when the function is called. Otherwise, those will never get executed.
15. The virtual function overrides ____________
a) Do not acquire base class declaration of default arguments
b) Do acquire base class declaration of default arguments
c) Do not link with the default arguments of base class
d) Do link with the default argument but only of derived classes
Explanation: The virtual function overrides do not acquire the base class declaration of default arguments. Even if a call to the virtual function is made, static type of the object decides the default arguments to be used.
Sanfoundry Global Education & Learning Series – Object Oriented Programming (OOPs).
To practice all areas of Object Oriented Programming (OOPs) using C++, here is complete set of 1000+ Multiple Choice Questions and Answers.