Default parameters in C++: which statement is correct about how many parameters may be defaulted? Choose the rule that best reflects the language design.

Difficulty: Easy

Correct Answer: All the parameters of a function can be default parameters.

Explanation:


Introduction / Context:
Default arguments let callers omit values and rely on sensible defaults. Understanding how many parameters may carry defaults is essential for designing friendly APIs without ambiguity.


Given Data / Assumptions:

  • A “default parameter” means a parameter that has a default value specified in a declaration.
  • Defaulted parameters must be trailing (once a default appears, all following parameters must also have defaults).
  • We are considering standard C++ rules.


Concept / Approach:
C++ places no hard numeric limit on how many parameters can be defaulted. In fact, every parameter can have a default so long as ordering rules are obeyed. There is no requirement that at least one parameter be defaulted, and certainly more than one may be defaulted.


Step-by-Step Solution:
1) Legal example: void f(int a = 1, int b = 2, int c = 3); // all defaulted2) Also legal: void g(int a, int b = 2, int c = 3); // defaults are trailing3) Illegal: void h(int a = 1, int b, int c = 3); // non-trailing b violates the rule4) Therefore, saying “all parameters can be default” is correct.


Verification / Alternative check:
Compile the examples above to see that fully defaulted parameter lists work, while the non-trailing default fails.



Why Other Options Are Wrong:
“Only one” is false; many parameters may have defaults.“Minimum one must be default” is false; defaults are optional.“No parameter can be default” contradicts the language feature.



Common Pitfalls:
Forgetting that once a default appears, all subsequent parameters must also have defaults; mixing defaults with overloading in ways that cause ambiguity.



Final Answer:
All the parameters of a function can be default parameters.

More Questions from Functions

Discussion & Comments

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