ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
void f(int x) { cout << "int"; }
void f(double x) { cout << "double"; }
int main() { f('a'); }
  1. A. double
  2. B. Ambiguous call: compile error
  3. C. int
  4. D. Both overloads run

Correct answer: C. int

A char argument undergoes integral promotion to int, which ranks as an exact match in overload resolution; converting char to double is a floating-point conversion, a lower rank. So f(int) wins unambiguously and "int" is printed.

More OOP with C++ and Java questions

Exams that ask this topic