ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
void f(int)   { cout << "I"; }
void f(char*) { cout << "P"; }
int main() { f(0); f(nullptr); }
  1. A. IP
  2. B. PP
  3. C. II
  4. D. Compile-time error: f(0) is ambiguous

Correct answer: A. IP

The literal 0 is an int, an exact match for f(int). nullptr has type std::nullptr_t, which converts to any pointer type but not to int, so it selects f(char*). This is why nullptr replaced 0/NULL.

More OOP with C++ and Java questions

Exams that ask this topic