ProgrammingModerateProduct-company placement · C++

What is printed?

What is printed?
template <int N> struct F { static const int v = N * F<N - 1>::v; };
template <>      struct F<0> { static const int v = 1; };
int main() { cout << F<5>::v; }
  1. A. 24
  2. B. 0
  3. C. Compile-time error: infinite recursion
  4. D. 120

Correct answer: D. 120

Each F<N> is instantiated at compile time from F<N−1>, and the full specialisation F<0> stops the recursion: 5·4·3·2·1·1 = 120.

More OOP with C++ and Java questions

Exams that ask this topic