C++ Programming MCQ – Default Arguments

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 value
Explanation 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. rightmost
Explanation 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 value
Explanation 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?

  1. #include <iostream>
  2. using namespace std;
  3. void func(int a, bool flag = true)
  4. {
  5. if (flag == true )
  6. {
  7. cout << “Flag is true. a = ” << a;
  8. }
  9. else
  10. {
  11. cout << “Flag is false. a = ” << a;
  12. }
  13. }
  14. int main()
  15. {
  16. func(200, false);
  17. return 0;
  18. }

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 = 200
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

Q 5. What will be the output of the following C++ code?

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. string askNumber(string prompt = “Please enter a number: “);
  5. int main()
  6. {
  7. string number = askNumber();
  8. cout << “Here is your number: ” << number;
  9. return 0;
  10. }
  11. string askNumber(string prompt)
  12. {
  13. string number;
  14. cout << prompt;
  15. cin >> number;
  16. return number;
  17. }

A. 5
B. 6
C. the number you entered
D. compile time error

Show Answer Answer:-C. the number you entered
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

Q 6. What will be the output of the following C++ code?

  1. #include <iostream>
  2. using namespace std;
  3. void Values(int n1, int n2 = 10)
  4. {
  5. using namespace std;
  6. cout << “1st value: ” << n1;
  7. cout << “2nd value: ” << n2;
  8. }
  9. int main()
  10. {
  11. Values(1);
  12. Values(3, 4);
  13. return 0;
  14. }

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 4
Explanation

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 arguments
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.

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 arguments
Explanation 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. void
Explanation 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?

  1. #include <iostream>
  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 << func(5);
  12. return 0;
  13. }

A. 15
B. 10
C. 40
D. compile time error

Show Answer Answer:-D. compile time error
Explanation In function parameters the default arguments should always be the rightmost parameters.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page