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

int sum(int a, int b = 10)
{
    return a + b;
}

int main()
{
    cout << sum(5);
}
                        

Options

  • A. 5
  • B. 10
  • C. 15
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

C. 15

Detailed Explanation

Answer: (C) 15

The function sum() has a default value of 10 for parameter b.

When the function is called as sum(5), only the first argument is supplied.

Therefore, a = 5 and the default value b = 10 is used.

Hence:

sum(5) = 5 + 10 = 15