ProgrammingVery hardProduct-company placement · C++

Which line fails to compile?

Which line fails to compile?
int main() {
    int x = 1;
    const int& r = x;
    auto a = r;          // L1
    a++;                 // L2
    decltype(r) b = x;   // L3
    b++;                 // L4
}
  1. A. L2 only
  2. B. L2 and L4
  3. C. L3 and L4
  4. D. L4 only

Correct answer: D. L4 only

auto deduction drops references and top-level const, so a is a plain int copy. decltype(r) keeps the declared type exactly, const int&, so b cannot be incremented.

More OOP with C++ and Java questions

Exams that ask this topic