ProgrammingHardProduct-company MCQ pattern

What happens with this C++ code?

What happens with this C++ code?
void f(int a, int b = 0) { cout << "two"; }
void f(int a)            { cout << "one"; }
int main() { f(5); }
  1. A. Compilation error: the call f(5) is ambiguous
  2. B. Prints two
  3. C. Compilation error at the second declaration
  4. D. Prints one

Correct answer: A. Compilation error: the call f(5) is ambiguous

Both declarations are legal overloads, and f(5, 6) would work. But f(5) matches f(int) and f(int, int = 0) equally well, so the call is ambiguous and the program does not compile.

More OOP with C++ and Java questions

Exams that ask this topic