ProgrammingHardProduct-company placement · C++

What is printed?

What is printed?
void f(int&)       { cout << "N"; }
void f(const int&) { cout << "C"; }
int main() { int a = 1; f(a); f(5); }
  1. A. CC
  2. B. NN
  3. C. NC
  4. D. Compile-time error: ambiguous call f(a)

Correct answer: C. NC

For a modifiable lvalue both overloads work, but binding to the less cv-qualified reference is a better conversion, so f(int&) wins. A literal can bind only to const int&.

More OOP with C++ and Java questions

Exams that ask this topic