ProgrammingVery hardTCS NQT Prime · Infosys SP
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)
void f(bool) { cout << "b"; }
void f(string) { cout << "s"; }
int main() { f("hi"); }- A. b
- B. s
- C. Compile error: ambiguous
- D. bs
Correct answer: A. b
A string literal decays to const char*, and pointer-to-bool is a standard conversion, which beats the user-defined conversion to std::string.
More OOP with C++ and Java questions
- What is printed? (C++17; assume <iostream>, <string>, <vector> and using namespace std) cpp template <class T> void f(T) { cout << "T"; }…
- 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; }…