ProgrammingVery hardInfosys DSE · Wipro Elite
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std)
template <class T> void f(T) { cout << "T"; }
void f(int) { cout << "I"; }
int main() { f(1); f('a'); f(1.0); }- A. III
- B. ITT
- C. ITI
- D. TTT
Correct answer: B. ITT
f(1) ties on exact match so the non-template wins; for 'a' and 1.0 the template deduces an exact match, beating the promotion/conversion needed for f(int).
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp struct A { static int n; static void p() {…
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp int main() { vector<int> a(3, 1), b{3, 1}…
- What is printed? java class A { A() { init(); } void init() { System.out.print("A"); } } class B extends A { int v = 5; B() {…
- What is printed? java class S { static { System.out.print("s"); } { System.out.print("i"); } S() { System.out.print("c"); } } // main: new…
- What does this print? java static StringBuilder f() { StringBuilder sb = new StringBuilder("a"); try { sb.append("b"); return sb; }…
- What is printed? java String a = "hi"; String b = new String("hi"); String c = b.intern(); System.out.print((a == b) + " " + (a == c) + "…