Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? int add(int a, int b = 10, int c = 100) { return a + b + c; } int main() { cout << add(1) << ” ” << add(1, 2); }

Correct answer A. 111 103

Explanation

Default arguments fill in any trailing parameters the caller omits. add(1) uses b = 10 and c = 100, giving 111. add(1, 2) supplies b = 2 but still uses the default c = 100, giving 103. Arguments are matched to parameters from left to right, which is why defaults can only appear on the rightmost parameters.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.