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
}- A. L2 only
- B. L2 and L4
- C. L3 and L4
- 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
- Which line fails to compile? cpp struct A { virtual ~A() { } }; struct B { virtual ~B() { } }; struct C : A, B { }; int main() { A a = new…
- What does the C++ standard say about this code? cpp const int x = 5; int p = constcast<int>(&x); p = 6;
- For the call f(g(), h()) in C++17, which statement is correct?
- What happens when this is compiled? cpp struct P { int a; private: int b; }; int main() { P p{1, 2}; }
- Which line fails to compile? cpp struct A { int x = 1; }; struct B : protected A { }; struct C : B { int get() { return x; } }; int main()…
- std::sort(v.begin(), v.end(), [](int a, int b) { return a <= b; }); What is wrong with this call?