Practice mode C++

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.