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); }- A. ITTT
- B. IIII
- C. ITIT
- 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
- What is printed? cpp template <typename T> struct Is { static const int v = 0; }; template <> struct Is<int> { static const int v = 1; }…
- What is printed? cpp struct L { L() { cout << "L"; } }; void f() { static L l; cout << "f"; } int main() { f(); f(); f(); }
- What is printed? cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int main() { T(); cout << "1"; const T& r = T(); cout <<…
- Which lines fail to compile? cpp struct U { U() { } U(const U&) = delete; }; void take(U u) { } int main() { U a; // L1 U b = a; // L2 U&…
- Which line fails to compile? cpp struct P { int v = 1; int get() { return v; } int cget() const { return v; } }; int main() { const P p{}…
- Which lines fail to compile? cpp struct S { int x = 1; static int y; static int f() { return y; } // L1 static int g() { return x; } // L2…