ProgrammingVery hardProduct-company placement · GATE CS
What is printed?
What is printed?
template <class T> void f(T) { cout << "T"; }
template <class T> void f(T*) { cout << "P"; }
void f(int) { cout << "I"; }
int main() {
int x = 1;
f(x); f(&x); f(1.0); f<>(x);
}- A. IPTT
- B. TPTT
- C. ITTT
- D. IPIT
Correct answer: A. IPTT
f(x): the non-template and f<int> are equally good, and a non-template wins ties. f(&x): both templates match exactly, and partial ordering prefers the more specialised f(T*). f(1.0): f<double> is exact, better than converting to int. f<>(x) allows only templates.
More OOP with C++ and Java questions
- What is printed? cpp struct S { void f() { cout << "N"; } void f() const { cout << "C"; } }; int main() { S a; const S b{}; const S& r =…
- What is printed? cpp int a = 1, b = 2; int& r = a; r = b; r = 5; cout << a << b;
- What is printed? cpp struct E { virtual const char n() const { return "E"; } virtual ~E() { } }; struct F : E { const char n() const…
- What is printed? (E and F are as shown.) cpp struct E { virtual ~E() { } }; struct F : E { }; int main() { try { try { throw F(); } catch…
- What happens? java class T { static String s() { return "S"; } String i() { return "I"; } } // in main: T t = null…
- What is printed? java class O { int x = 1; class I { int x = 2; void p(int x) { System.out.print(x + "" + this.x + O.this.x); } } } // in…