ProgrammingVery hardProduct-company placement · C++

What is printed?

What is printed?
template <typename T> void f(T) { cout << "T"; }
void f(int)                      { cout << "I"; }
int main() { f(1); f(1.0); f('a'); f<>(1); }
  1. A. ITTT
  2. B. IIII
  3. C. ITIT
  4. D. ITII

Correct answer: A. ITTT

For f(1) the non-template is an exact match and wins the tie. For 1.0 and 'a' the template deduces an exact match (double, char), which beats a conversion or promotion to int. f<>(1) forces the template.

More OOP with C++ and Java questions

Exams that ask this topic