ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
void f(long)   { cout << "L"; }
void f(double) { cout << "D"; }
int main() { f(5L); f(2.5f); f(5); }
  1. A. LDL
  2. B. Compile-time error: the call f(5) is ambiguous
  3. C. LDD
  4. D. LLL

Correct answer: B. Compile-time error: the call f(5) is ambiguous

5L and 2.5f have an exact match and a promotion (float → double). For an int, both int → long and int → double are standard conversions of the same rank, so f(5) is ambiguous and the program does not compile.

More OOP with C++ and Java questions

Exams that ask this topic