C++ overload resolution on (float, char, char): compute and print K from character arithmetic.
#include
class CuriousTab
{
int K;
public:
void CuriousTabFunction(float, int, char);
void CuriousTabFunction(float, char, char);
};
int main()
{
CuriousTab objIB;
objIB.CuriousTabFunction(15.09f, 'A', char('A' + 'A'));
return 0;
}
void CuriousTab::CuriousTabFunction(float, char y, char z)
{
K = int(z);
K = int(y);
K = y + z; // integer promotions apply
cout << "K = " << K << endl;
}
-
AThe program will print the output K = 130.
-
BThe program will print the output K = 195.
-
CThe program will print the output K = -21.
-
DThe program will print the output K = -61.
-
EThe program will report compile time error.
Answer
Correct Answer: The program will print the output K = -61.
Explanation
Introduction / Context: The call selects the overload CuriousTabFunction(float, char, char) because both the second and third arguments are chars, which is a better match than promoting the second argument to int. The body then performs character arithmetic, relying on integer promotion rules, which can expose the signedness of char on the target platform.
Given Data / Assumptions:
- Invocation: (15.09f, 'A', char('A' + 'A')).
- 'A' has code 65.
- char('A' + 'A') computes a char from 65 + 65 = 130. On implementations where plain char is signed (common on x86), char(130) becomes -126.
- K is assigned y + z after integer promotions.
Concept / Approach: Overload selection chooses (float, char, char). Then y = 65 and z is the char resulting from 130, which on signed-char targets is -126. Adding with integer promotion gives 65 + (-126) = -61. The program prints this integer value.
Step-by-Step Solution:
Overload resolution: (float, char, char) is a better match than (float, int, char).Map y = 65.Compute z = char(130) → typically -126 when char is signed.Compute K = y + z = 65 + (-126) = -61.cout prints "K = -61".Verification / Alternative check: On systems where plain char is unsigned, char(130) stays 130 and K becomes 65 + 130 = 195. The provided options reflect both possibilities; the conventional assumption for such questions is signed char, yielding -61.
Why Other Options Are Wrong:
- K = 130: Would be printed only if the function printed the intermediate assignment K = int(z) instead of the final sum.
- K = 195: Occurs on platforms with unsigned char; not the typical assumption here.
- K = -21: Does not match any consistent interpretation of char sums here.
- Compile time error: Both overloads are valid; one is selected unambiguously.
Common Pitfalls: Forgetting that plain char may be signed or unsigned, expecting cout with char to print a glyph rather than an integer (here it prints an int), or assuming the (float, int, char) overload is chosen.
Final Answer: The program will print the output K = -61.