C++ default parameter override: what exact output is produced by this call?\n\n#include<iostream.h>\nvoid MyFunction(int a, int b = 40)\n{\n cout << " a = " << a << " b = " << b << endl;\n}\nint main()\n{\n MyFunction(20, 30);\n return 0;\n}

Difficulty: Easy

Correct Answer: a = 20 b = 30

Explanation:


Introduction / Context:
This is a straightforward default-argument question. The function MyFunction supplies a default only for its second parameter, but the call provides both arguments explicitly, so the default is not used.


Given Data / Assumptions:

  • Signature: void MyFunction(int a, int b = 40).
  • Call site: MyFunction(20, 30).
  • The function prints the two values in a fixed format.


Concept / Approach:
Default arguments are only consulted for parameters omitted at the call. Any explicitly supplied argument replaces the default entirely. The stream concatenates literal text with values, so the line printed reflects the exact bound values.


Step-by-Step Solution:
1) Bind a = 20. 2) Bind b = 30 (explicit at call site). 3) Function executes and prints “ a = 20 b = 30”.


Verification / Alternative check:
If the call were MyFunction(20), the output would be “ a = 20 b = 40”.


Why Other Options Are Wrong:
“a = 20 b = 40” incorrectly applies the default; “Garbage” and “Compilation fails” have no basis because types match and the program is valid.


Common Pitfalls:
Thinking defaults are always printed regardless of arguments; forgetting that the literal spaces are as coded.


Final Answer:
a = 20 b = 30

More Questions from Functions

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion