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
  1.    15
  2.    10
  3.    compile time error
  4.    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 ?


  1.    int
  2.    void
  3.    float
  4.    char
Explanation:-
Answer: Option B. -> void

None.



Question 3.

If we start our function call with default arguments means, what will be proceeding arguments?


  1.    user argument
  2.    empty arguments
  3.    default arguments
  4.    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?


  1.    trailing arguments
  2.    default arguments
  3.    both a & b
  4.    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
  1.    trailing arguments
  2.    default arguments
  3.    both a & b
  4.    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
  1.    5
  2.    6
  3.    the number you entered
  4.    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?


  1.    default value
  2.    rise an error
  3.    both a & b
  4.    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?


  1.    leftmost
  2.    rightmost
  3.    both a & b
  4.    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
  1.    Flag is true. a = 200
  2.    Flag is false. a = 100
  3.    Flag is false. a = 200
  4.    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