C++ default parameters: identify which declarations are invalid and choose the best summary.

Difficulty: Easy

Correct Answer: Both B and C are incorrect.

Explanation:


Introduction / Context:
Default parameters in C++ must be supplied from the rightmost side leftward. Once a parameter has a default, all parameters to its right must also have defaults. Violating this rule results in declarations that cannot be called unambiguously because you would need to “skip” a middle parameter at the call site, which is not allowed.


Given Data / Assumptions:

  • We review three function declarations using default arguments.
  • Parameters without defaults must appear before parameters with defaults.
  • Defaults are inserted at the call site for omitted trailing arguments.


Concept / Approach:

Declaration A is valid: defaults are at the end (b and c), so callers may pass 1, 2, or 3 arguments. Declaration B is invalid because it gives a default to the first parameter but not to the second; callers would have to omit the first but still supply the second, which C++ cannot express. Declaration C is also invalid because a non-defaulted parameter b appears between defaulted parameters; callers could not omit the middle argument while taking the third’s default. Therefore, the best summary is that B and C are incorrect.


Step-by-Step Solution:

Analyze A: ok → trailing defaults only. Analyze B: defaulted a, non-default b → violates trailing rule. Analyze C: non-default b between defaulted a and c → violates trailing rule. Choose “Both B and C are incorrect.”


Verification / Alternative check:

Attempt to compile B and C: compilers emit errors. A compiles and is callable as Sum(1), Sum(1,2), and Sum(1,2,3).


Why Other Options Are Wrong:

“All are correct” is wrong because B and C break the rule.

Any single choice misses that both B and C fail.


Common Pitfalls:

  • Assuming you can skip a middle parameter; you cannot.
  • Redefining defaults across declarations—place them once in a header.


Final Answer:

Both B and C are incorrect.

Discussion & Comments

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