C++ Programming MCQ – Default Arguments

This section on 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 (campus interviews, walk-in interviews, company interviews), placements, entrance exams and other competitive exams. These programming puzzles can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ questions come with the detailed explanation of the answers which helps in better understanding of C++ concepts.

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

Answer: a
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

Answer: b
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

Answer: a
Explanation: The default value will be used when the user value is not given, So in this case, the user value will be taken.
advertisement
advertisement

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 true. a = 200
b) Flag is false. a = 100
c) Flag is false. a = 200
d) Flag is true. a = 100
View Answer

Answer: c
Explanation: In this program, we are passing the value, as it evaluates to false, it produces the output as following.
Output:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
$ g++ def.cpp
$ a.out
Flag is false. a = 200

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

advertisement
  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
View Answer

Answer: c
Explanation: In this program, we are getting a number and printing it.
Output:

advertisement
$ 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?

  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
View Answer

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

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

Answer: b
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

Answer: c
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

Answer: b
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?

  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) compile time error
d) 30
View Answer

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.