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; }- A. 24
- B. 0
- C. Compile-time error: infinite recursion
- 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
- What is printed? cpp struct M { M() { throw 1; } }; struct S { M m; S() try : m() { } catch (int) { cout << "S"; } }; int main() { try { S…
- What is printed? cpp void f() noexcept { } void g() { } int main() { cout << noexcept(f()) << noexcept(g()); }
- What happens when this is compiled? cpp int main() { try { throw 1; } catch (...) { cout << "any"; } catch (int) { cout << "int"; } }
- What is printed? cpp int main() { try { throw runtimeerror("disk full"); } catch (const exception& e) { cout << e.what(); } }
- What is printed? cpp struct E { }; int main() { try { throw new E(); } catch (E&) { cout << "ref"; } catch (E) { cout << "ptr"; } }
- What is printed? cpp int main() { try { throw 1; } catch (long) { cout << "L"; } catch (double) { cout << "D"; } catch (...) { cout <<…