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
}
  1. A. It prints 77
  2. B. It prints 7 and then the pointer size
  3. C. Compile-time error at L1
  4. 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

Exams that ask this topic