ProgrammingHardProduct-company placement · C++

Which lines fail to compile?

Which lines fail to compile?
void g(int& x)       { }
void h(const int& x) { }
int main() {
    int a = 1;
    g(5);       // L1
    h(5);       // L2
    g(a);       // L3
    g(a + 1);   // L4
}
  1. A. L1 only
  2. B. L1, L2 and L4
  3. C. L4 only
  4. D. L1 and L4

Correct answer: D. L1 and L4

A non-const lvalue reference cannot bind to a temporary (5 or a + 1). A const reference can, by binding to a materialised temporary.

More OOP with C++ and Java questions

Exams that ask this topic