ProgrammingVery hardGATE CS · TCS Digital · Advanced
What is printed? (C++17, assume <iostream> and using namespace std)
What is printed? (C++17, assume <iostream> and using namespace std)
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 = 0;
f(x);
f(&x);
f<int>(x);
f('a');
f(0L);
}- A. IPTTT
- B. IPITT
- C. IPIIT
- D. TPTTT
Correct answer: A. IPTTT
f(x): non-template f(int) ties the template on conversions and wins. f(&x): f(T*) is more specialised than f(T). f<int>(x) names a template, so f(int) is excluded. 'a' and 0L match f(T) exactly, beating the promotion/conversion needed for f(int).
More OOP with C++ and Java questions
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct V { V() { cout << "V"; } }; struct A : virtual V { A() {…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct C { int v = 0; C& operator++() { ++v; return this; } C…
- What is printed? (C++17, assume <iostream> and using namespace std) cpp struct T { T() { cout << "c"; } ~T() { cout << "d"; } }; int…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() { super()…
- What is printed? java Long l = 127L; Integer i = 127; System.out.print(l.equals(i) + " " + l.equals(127L) + " " + (l == 127) + " " +…
- What is printed? java static void f(long x) { System.out.print("L"); } static void f(Integer x) { System.out.print("I"); } static void…