Default Arguments(C++ Programming ) Questions and Answers
Question 1.
What is the output of this program?
1. #include 2. using namespace std; 3. int func(int m = 10, int n) 4. { 5. int c; 6. c = m + n; 7. return c; 8. } 9. int main() 10. { 11. cout
15
10
compile time error
none of the mentioned
Explanation:-
Answer: Option C. -> compile time error
We can't use the user argument infront of the default argument.
Question 2.
What is the default return type of a function ?
int
void
float
char
Explanation:-
Answer: Option B. -> void
None.
Question 3.
If we start our function call with default arguments means, what will be proceeding arguments?
user argument
empty arguments
default arguments
none of the mentioned
Explanation:-
Answer: Option C. -> default arguments
As a rule, the default argument must be followed by default arguments only.
Question 4.
What we can’t place followed by the non-default arguments?
trailing arguments
default arguments
both a & b
none of the mentioned
Explanation:-
Answer: Option B. -> default arguments
None.
Question 5.
What is the output of this program?
1. #include 2. using namespace std; 3. void Values(int n1, int n2 = 10) 4. { 5. using namespace std; 6. cout
trailing arguments
default arguments
both a & b
none of the mentioned
Explanation:-
Answer: Option B. -> default arguments
Answer: (a)
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
Question 6.
What is the output of this program?
1. #include 2. #include 3. using namespace std; 4. string askNumber(string prompt = "Please enter a number: "); 5. int main() 6. { 7. string number = askNumber(); 8. cout
5
6
the number you entered
compile time error
Explanation:-
Answer: Option C. -> the number you entered
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
Question 7.
If the user didn’t supply the value, what value will it take?
default value
rise an error
both a & b
none of the mentioned
Explanation:-
Answer: Option A. -> default value
If the user didn’t supply the value means, the compiler will take the given value in the argument list.
Question 8.
Where can the default parameter be placed by the user?
leftmost
rightmost
both a & b
none of the mentioned
Explanation:-
Answer: Option B. -> rightmost
None.
Question 9.
What is the output of this program?
1. #include 2. using namespace std; 3. void func(int a, bool flag = true) 4. { 5. if (flag == true ) { 6. cout
Flag is true. a = 200
Flag is false. a = 100
Flag is false. a = 200
Flag is true. a = 100
Explanation:-
Answer: Option C. -> Flag is false. a = 200
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