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.
Answer checked How we write and check questions
Discussion