Assuming standard headers and using namespace std, what does this C++ code print? void f(int) { cout << “int”; } void f(char*) { cout << “ptr”; } int main() { f(nullptr); }
Correct answer D. ptr
Explanation
nullptr has type std::nullptr_t, which converts implicitly to any pointer type but not to int, so only f(char*) is viable and ptr is printed. With the old NULL macro (often defined as 0), the call could pick f(int) or be ambiguous. This is the main reason modern C++ prefers nullptr for null pointers.
Answer checked How we write and check questions
Discussion