Any2Quiz
Computer Science / Languages / C++

Templates Quiz - Languages C++

Free practice quiz: Templates (Languages C++). 56 multiple-choice questions with answers and explanations. Play online, no sign-up needed.

Play all 56 questions free

Sample questions

  1. What is the primary purpose of C++ templates?

    1. To implement inheritance between classes.
    2. To achieve runtime polymorphism.
    3. To allow writing generic code that works with different data types.
    4. To manage memory allocation automatically.
    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.

  2. What is a 'primary template' in C++?

    1. The first template definition encountered.
    2. The most specialized version of a template.
    3. The general template definition from which specific instances are generated or specialized versions are derived.
    4. A template that cannot be instantiated directly.
    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.

  3. In a template function like `template <typename T> void process(T& value);`, what is `T&`?

    1. A reference to a generic type.
    2. A pointer to a generic type.
    3. A constant reference to a generic type.
    4. A function taking a generic type by value.
    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.

  4. Consider `template <typename T> struct Foo { typedef T type; };`. What is the purpose of `typedef T type;`?

    1. To create a data member named `type`.
    2. To define a type alias for the template parameter `T` within the `Foo` struct.
    3. To declare a function named `type`.
    4. To indicate that `Foo` is a 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`.

  5. What is the role of `std::make_tuple`?

    1. To define a new tuple type.
    2. To create a tuple object, often with type deduction.
    3. To destroy a tuple object.
    4. To resize a 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`.

Take the full quiz

More quizzes

Browse all C++ quizzes