Assuming standard headers and using namespace std, what does this C++ code print? void f(int) { cout << “I”; } void f(double) { cout << “D”; } int main() { f(3); f(3.0f); f(‘a’); }
Correct answer A. IDI
Explanation
f(3) matches f(int) exactly. A float argument is promoted to double, and promotion ranks better than conversion to int, so f(3.0f) picks f(double). A char is promoted to int, which beats the char-to-double conversion, so f('a') calls f(int). Overload resolution ranks exact matches first, then promotions, then standard conversions.
Answer checked How we write and check questions
Discussion