ProgrammingHardProduct-company placement · C++
Which line fails to compile?
Which line fails to compile?
int main() {
int a[3] = {1, 2, 3}, b[3];
b = a; // L1
array<int, 3> c{1, 2, 3}, d;
d = c; // L2
}- A. L2 only
- B. L1 only
- C. Both
- D. Neither
Correct answer: B. L1 only
Built-in arrays cannot be assigned as a whole (a decays to a pointer). std::array wraps the array in a class, so it gets copy assignment, and can also be passed and returned by value.
More OOP with C++ and Java questions
- What is printed? cpp template <sizet N> sizet len(int (&)[N]) { return N; } int main() { int a[7]; cout << len(a); // L1 int p = a; cout…
- What is printed? cpp struct S { void f() & { cout << "L"; } void f() && { cout << "R"; } }; int main() { S s; s.f(); S().f()…
- What is printed? cpp int main() { int x = 0; auto f = [x]() mutable { return ++x; }; f(); f(); cout << f() << x; }
- What is printed? cpp int main() { std::function<int()> f; try { cout << f(); } catch (const badfunctioncall&) { cout << "E"; } }
- What is printed? cpp struct W { W(int, int) { cout << "2"; } W(initializerlist<int>) { cout << "L"; } }; int main() { W a(1, 2); W b{1…
- Two classes hold sharedptr members pointing to each other (a parent owns its child and the child owns its parent). What happens when the…