ProgrammingVery hardMicrosoft/Amazon SDE MCQ · GATE CS
What is printed?
What is printed?
template <class T> void f(T) { cout << "T"; }
template <> void f<int*>(int*) { cout << "S"; }
template <class T> void f(T*) { cout << "P"; }
int main() {
int x = 0;
f(&x);
}- A. S
- B. P
- C. T
- D. Compile error: ambiguous call
Correct answer: B. P
The explicit specialisation belongs to the first template f(T), declared before f(T*) existed. Overload resolution considers only primary templates and picks the more specialised f(T*), so the specialisation is never used.
More OOP with C++ and Java questions
- What is printed? cpp struct T { T() { } T(const T&) { cout << "C"; } T& operator=(const T&) { cout << "A"; return this; } }; int main() {…
- What is printed? cpp struct T { ~T() { cout << "d"; } }; T make() { return T(); } int main() { const T& r = make(); cout << "x"; }
- What is printed? cpp struct K { int g() { static int c = 0; return ++c; } }; int main() { K a, b; a.g(); b.g(); cout << a.g(); }
- Which line fails to compile? cpp class B { protected: int v = 1; }; class D : public B { public: int me() { return v; } // line 1 int…
- What is printed? cpp int main() { auto a = makeshared<int>(1); auto b = a; { auto c = b; cout << a.usecount(); } weakptr<int> w = a; cout…
- What is printed? cpp void f(int) { cout << "i"; } void f(char) { cout << "c"; } void f(double) { cout << "d"; } int main() { char a = 'a'…