C++ questions and puzzles focuses on “Default Arguments”. One shall practice these questions and puzzles to improve their C++ programming skills needed for various interviews placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ questions and puzzles on “Default Arguments” along with answers, explanations and/or solutions:
Q 1. If the user did not supply the value, what value will it take?
A. error
B.. rise an error
C. both default value & rise an error
D. default value
Show Answer
Answer:-D. default valueExplanation
If the user did not supply the value means, the compiler will take the given value in the argument list.Q 2. Where can the default parameter be placed by the user?
A. leftmost
B. topmost
C. both leftmost & rightmost
D. rightmost
Show Answer
Answer:-D. rightmostExplanation
To avoid the ambiguity between the non-default parameters and default parameters.Q 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
Show Answer
Answer:-A. user valueExplanation
The default value will be used when the user value is not given, So in this case, the user value will be taken.Q 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 false. a = 200
B. Flag is false. a = 100
C. Flag is true. a = 200
D. Flag is true. a = 100
Show Answer
Answer:-A. Flag is false. a = 200Explanation
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 = 200Q 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
Show Answer
Answer:-C. the number you enteredExplanation
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:5Q 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
Show Answer
Answer:-A.1st value: 1 10 3 4Explanation
Q 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
Show Answer
Answer:-B. default argumentsExplanation
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.Q 8. If we start our function call with default arguments means, what will be proceeding arguments?
A. default arguments
B. empty arguments
C. user argument
D. user & empty arguments
Show Answer
Answer:-A. default argumentsExplanation
As a rule, the default argument must be followed by default arguments only.Q 9. What is the default return type of a function?
A. int
B. float
C. void
D. char
Show Answer
Answer:-C. voidExplanation
void is the default return value of any function, to handle both empty and non-empty values.Q 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. 40
D. compile time error
Leave a Reply