ProgrammingHardProduct-company placement · C++

What happens?

What happens?
template <class T> T mx(T a, T b) { return a > b ? a : b; }
int main() {
    cout << mx(3, 7);            // L1
    cout << mx(1, 2.5);          // L2
    cout << mx<double>(1, 2.5);  // L3
}
  1. A. It prints 72.52.5
  2. B. It prints 722.5
  3. C. Compile-time error at L2: T is deduced as both int and double
  4. D. Compile-time error at L3

Correct answer: C. Compile-time error at L2: T is deduced as both int and double

Template argument deduction does not perform conversions: from 1 it deduces int, from 2.5 double, a conflict. Specifying T explicitly (L3) turns it into ordinary overload matching, where 1 converts to double.

More OOP with C++ and Java questions

Exams that ask this topic