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); }- A. IP
- B. PP
- C. II
- 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
- Which lines fail to compile? cpp void g(int& x) { } void h(const int& x) { } int main() { int a = 1; g(5); // L1 h(5); // L2 g(a); // L3…
- What is printed? cpp void f(int&) { cout << "N"; } void f(const int&) { cout << "C"; } int main() { int a = 1; f(a); f(5); }
- What is printed? cpp void f(int&) { cout << "L"; } void f(int&&) { cout << "R"; } int main() { int a = 1; int&& r = 5; f(a); f(5)…
- What is printed? cpp struct A { A() { cout << "D"; } A(const A&) { cout << "C"; } A(A&&) noexcept { cout << "M"; } }; int main() {…
- What is printed? cpp int main() { uniqueptr<int> p = makeunique<int>(7); uniqueptr<int> q = std::move(p); cout << (p == nullptr) << q; }
- What is printed? cpp int main() { auto a = makeshared<int>(5); auto b = a; { auto c = a; cout << a.usecount(); } cout << a.usecount()…