Here is a listing of C++ questions and puzzles on “Default Arguments” along with answers, explanations and/or solutions:
1. If the user did not supply the value, what value will it take?
a) default value
b) rise an error
c) both default value & rise an error
d) error
View Answer
Explanation: If the user did not supply the value means, the compiler will take the given value in the argument list.
2. Where can the default parameter be placed by the user?
a) leftmost
b) rightmost
c) both leftmost & rightmost
d) topmost
View Answer
Explanation: To avoid the ambiguity between the non-default parameters and default parameters.
3. Which value will it take when both user and default values are given?
a) user value
b) default value
c) custom value
d) defined value
View Answer
Explanation: The default value will be used when the user value is not given, So in this case, the user value will be taken.
4. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void func(int a, bool flag = true)
{
if (flag == true )
{
cout << "Flag is true. a = " << a;
}
else
{
cout << "Flag is false. a = " << a;
}
}
int main()
{
func(200, false);
return 0;
}
a) Flag is true. a = 200
b) Flag is false. a = 100
c) Flag is false. a = 200
d) Flag is true. a = 100
View Answer
Explanation: In this program, we are passing the value, as it evaluates to false, it produces the output as following.
Output:
$ g++ def.cpp $ a.out Flag is false. a = 200
5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
string askNumber(string prompt = "Please enter a number: ");
int main()
{
string number = askNumber();
cout << "Here is your number: " << number;
return 0;
}
string askNumber(string prompt)
{
string number;
cout << prompt;
cin >> number;
return number;
}
a) 5
b) 6
c) the number you entered
d) compile time error
View Answer
Explanation: In this program, we are getting a number and printing it.
Output:
$ g++ def1.cpp $ a.out Please enter a number: 5 Here is your number:5
6. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
void Values(int n1, int n2 = 10)
{
using namespace std;
cout << "1st value: " << n1;
cout << "2nd value: " << n2;
}
int main()
{
Values(1);
Values(3, 4);
return 0;
}
a)
1st value: 1 10 3 4
b)
1st value: 1 10 3 10
c) compile time error
d) runtime error
View Answer
Explanation: In this program, We are passing the values as by default values rules it is working.
Output:
$ g++ def2.cpp $ a.out 1st value: 1 2nd value: 10 1st value: 3 2nd value: 4
7. What we can’t place followed by the non-default arguments?
a) trailing arguments
b) default arguments
c) both trailing & default arguments
d) leading arguments
View Answer
Explanation: To avoid the ambiguity in arguments.
eg. if func(int a=3, int b);
so if we call func(5), here will 5 will be value of a or b, because 5 is first parameter so a should be 5 but as only one argument is given b should be 5. So to remove such ambiguity default parameters are kept at the end or rightmost side.
8. If we start our function call with default arguments means, what will be proceeding arguments?
a) user argument
b) empty arguments
c) default arguments
d) user & empty arguments
View Answer
Explanation: As a rule, the default argument must be followed by default arguments only.
9. What is the default return type of a function?
a) int
b) void
c) float
d) char
View Answer
Explanation: void is the default return value of any function, to handle both empty and non-empty values.
10. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
int func(int m = 10, int n)
{
int c;
c = m + n;
return c;
}
int main()
{
cout << func(5);
return 0;
}
a) 15
b) 10
c) compile time error
d) 30
View Answer
Explanation: In function parameters the default arguments should always be the rightmost parameters.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Practice Computer Science MCQs
- Check Programming Books
- Apply for C++ Internship
- Apply for Computer Science Internship
- Check C++ Books