Templates Quiz - Languages C++
Free practice quiz: Templates (Languages C++). 56 multiple-choice questions with answers and explanations. Play online, no sign-up needed.
This Languages C++ practice quiz has 56 multiple-choice questions on Templates, and 56 of them come with an explanation. Try the 5 sample questions below (tap an option to check yourself), then play the full quiz online. No sign-up needed.
Start quiz: 56 questions, freeSample questions
What is the primary purpose of C++ templates?
Show answer
Answer: C. To allow writing generic code that works with different data types.
C++ templates enable the creation of generic classes and functions, allowing them to operate on various data types without needing to rewrite the code for each type. This promotes code reusability and type safety.
What is a 'primary template' in C++?
Show answer
Answer: C. The general template definition from which specific instances are generated or specialized versions are derived.
The primary template is the main, most general definition of a class or function template. All other template specializations (explicit and partial) are derived from or provide specific implementations for this primary template.
In a template function like `template <typename T> void process(T& value);`, what is `T&`?
Show answer
Answer: A. A reference to a generic type.
`T&` signifies that `T` is a template parameter representing a type, and the function parameter `value` is a reference to an object of that type `T`. This allows the function to modify the original object passed to it.
Consider `template <typename T> struct Foo { typedef T type; };`. What is the purpose of `typedef T type;`?
Show answer
Answer: B. To define a type alias for the template parameter `T` within the `Foo` struct.
The `typedef T type;` statement creates a member type alias named `type` within the `Foo` struct. This alias refers to the type `T` that was used to instantiate `Foo`.
What is the role of `std::make_tuple`?
Show answer
Answer: B. To create a tuple object, often with type deduction.
`std::make_tuple` is a helper function that constructs a `std::tuple` object from its arguments. It's convenient because it deduces the types of the elements, similar to `std::make_pair`.
More questions in this quiz
Answers and explanations are in the full quiz.
- Which of the following best describes a template instantiation?
- Consider the following template function: `template <typename T> void print(T value) { std::cout << value << std::endl; }`. What happens when you call `print(5)`?
- What is a non-type template parameter?
- Which mechanism allows you to provide a specific implementation for a template when certain conditions are met?
- What does `template <typename T, size_t N> class Array { ... };` signify?
- What is the difference between `template <class T>` and `template <typename T>`?
- What is a 'dependent name' in the context of C++ templates?
- When is `typename` keyword strictly required within a template definition?
- Consider the following code: `template <typename T> struct Foo { typedef T value_type; };`. What is `Foo<int>::value_type`?
- What is template argument deduction?
- Which of the following is an example of explicit template specialization?
- What is partial template specialization?
- Consider `template <typename T> class Container { ... };` and `template <typename T> class Container<T*> { ... };`. What is the second declaration?
- What is the purpose of `template <typename T> void foo(T* ptr) {}`?
- What is template metaprogramming?
- Consider `template <int N> struct Factorial { static const int value = N * Factor<N - 1>::value; };`. What is the base case for this template metaprogramming example?
- What does `template <typename T> using VectorAlias = std::vector<T, std::allocator<T>>;` achieve?
- What is a potential drawback of excessive template instantiation?
- Which keyword is used to provide a default template argument?
- What is the primary advantage of using `std::enable_if` in template metaprogramming?
- What does `template <auto V> void printValue() { std::cout << V << std::endl; }` enable?
- What is SFINAE in the context of templates?
- Consider `template <typename T> struct Traits { static constexpr bool is_fundamental = false; };` and `template <typename T> struct Traits<const T> { static constexpr bool is_fundamental = Traits<T>::is_fundamental; };`. What would `Traits<const int>::is_fundamental` evaluate to if `Traits<int>::is_fundamental` were `true`?
- What is the purpose of `template <typename T> class Container { ... T* data; ... };` and `template <typename T> class Container<T*> { ... T** data; ... };`?
- What is a template parameter pack?
- How do you expand a template parameter pack?
- What is a 'variadic template'?
- Consider `template <typename... Args> void printArgs(Args... args) { ... }`. What is `Args... args`?
- What is the purpose of `constexpr` in relation to templates?
- What is the primary mechanism for conditional compilation in C++ templates?
- What is an example of a type trait used with templates?
- What is the purpose of `template <typename T, typename Allocator = std::allocator<T>> class MyContainer { ... };`?
- What does `template <typename T> requires std::is_integral_v<T> void foo(T value);` signify (C++20 Concepts)?
- What is a template alias (using declaration)?
- Consider `template <typename T> struct Foo { static T value; };`. What must be done to use this template?
- What is the benefit of template metaprogramming for compile-time calculations like factorial?
- What is a 'template template parameter'?
- Consider `template <template <typename> class Container> void processContainer(Container<int>& c) { ... }`. What is `template <typename> class Container`?
- What is the primary purpose of `std::tuple`?
- How do you access elements in a `std::tuple`?
- What does `template <typename T, int Size> class FixedArray { T data[Size]; };` represent?
- What is a 'dependent type' in the context of templates?
- What does `template <typename T> void func(T t);` and `template <> void func(int t);` demonstrate?
- What is the primary goal of C++ templates?
- What is the difference between `template <class T> class Container;` and `template <typename T> class Container;`?
- What does `template <typename T> struct Foo { static const T value; };` require for instantiation?
- What is the purpose of `decltype` in conjunction with templates?
- Which C++ standard introduced variadic templates?
- What is the typical use case for `std::array`?
- What is the purpose of `template <auto Value> void func() { ... }` in C++17?
- What is a common pattern for implementing recursive template metaprogramming?