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"); }
  1. A. b
  2. B. s
  3. C. Compile error: ambiguous
  4. 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

Exams that ask this topic