ProgrammingVery hardProduct-company placement · C++
What is printed?
What is printed?
template <size_t N> size_t len(int (&)[N]) { return N; }
int main() {
int a[7];
cout << len(a); // L1
int* p = a;
cout << len(p); // L2
}- A. It prints 77
- B. It prints 7 and then the pointer size
- C. Compile-time error at L1
- D. Compile-time error at L2; without L2 it prints 7
Correct answer: D. Compile-time error at L2; without L2 it prints 7
Taking the array by reference preserves its type int[7], so N is deduced as 7. A pointer has lost the size, so it cannot bind to a reference-to-array and deduction fails.
More OOP with C++ and Java questions
- 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…
- OrderService creates new MySqlRepository() inside its own constructor. It is refactored to receive a Repository interface through its…