Skip to content
QuiznExam

Templates in C++ Quiz

Test your knowledge of templates in C++, including function templates, class templates, template parameters, specialization, multiple template parameters, and the fundamentals of generic programming.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of templates in C++?

    • A. To allocate memory dynamically
    • B. To enable generic programming with different data types
    • C. To prevent function overloading
    • D. To replace all class definitions
    Show answer

    B. To enable generic programming with different data types

    Templates allow functions and classes to work with different types without requiring separate implementations for every type.

  2. 2. Which keyword is commonly used to declare a function template in C++?

    • A. generic
    • B. define
    • C. template
    • D. typenameclass
    Show answer

    C. template

    The template keyword introduces a template declaration, followed by its template parameter list.

  3. 3. Which declaration correctly introduces a function template with one type parameter T?

    • A. template <typename T>
    • B. function <type T>
    • C. template (class T)
    • D. typename <template T>
    Show answer

    A. template <typename T>

    The syntax template <typename T> declares a type template parameter named T.

  4. 4. What is a function template in C++?

    • A. A function that can only accept integer arguments
    • B. A function that executes automatically at program startup
    • C. A function that must return a pointer
    • D. A blueprint for generating functions for specified or deduced types
    Show answer

    D. A blueprint for generating functions for specified or deduced types

    A function template describes a family of functions that can be instantiated for different types.

  5. 5. Which of the following is a valid function template definition?

    • A. template T max(T a, T b) { return a > b ? a : b; }
    • B. template <typename T> T maximum(T a, T b) { return a > b ? a : b; }
    • C. typename T maximum(T a, T b) { return a > b ? a : b; }
    • D. template (T) maximum(T a, T b) { return a > b ? a : b; }
    Show answer

    B. template <typename T> T maximum(T a, T b) { return a > b ? a : b; }

    This definition declares a type parameter T and uses it as the return type and parameter type. The type must support the comparison and return operations used by the function.

  6. 6. What is a class template in C++?

    • A. A class that cannot contain member functions
    • B. A class that can only store strings
    • C. A blueprint for creating classes parameterized by types or other template arguments
    • D. A class that is always abstract
    Show answer

    C. A blueprint for creating classes parameterized by types or other template arguments

    Class templates allow reusable class definitions, such as containers, to work with different types.

  7. 7. Which declaration correctly defines a class template named Box with a type parameter T?

    • A. template <typename T> class Box { T value; };
    • B. class <typename T> Box { T value; };
    • C. template (T) class Box { T value; };
    • D. typename Box<T> { T value; };
    Show answer

    A. template <typename T> class Box { T value; };

    The template declaration introduces T and defines Box as a class template with a data member of type T.

  8. 8. What does the following code demonstrate? template <typename T> T identity(T value) { return value; }

    • A. Multiple inheritance
    • B. Operator overloading
    • C. Dynamic memory allocation
    • D. A function template
    Show answer

    D. A function template

    The identity function is a function template that accepts and returns a value of the deduced or explicitly specified type T.

  9. 9. What is template argument deduction in C++?

    • A. Automatically deleting unused template parameters
    • B. Inferring template arguments from function arguments when possible
    • C. Converting every argument to a string
    • D. Replacing templates with macros
    Show answer

    B. Inferring template arguments from function arguments when possible

    For function templates, the compiler can often deduce template arguments from the types of function arguments supplied at the call site.

  10. 10. Which statement about typename and class in a template type parameter list is correct?

    • A. typename is valid only for integer types
    • B. class must always be used instead of typename
    • C. Both can introduce a type template parameter
    • D. Neither keyword can be used in templates
    Show answer

    C. Both can introduce a type template parameter

    In declarations such as template <typename T> and template <class T>, the two keywords are interchangeable for introducing a type template parameter.

  11. 11. What is template specialization in C++?

    • A. Providing a specific implementation for particular template arguments
    • B. Removing all template parameters from a program
    • C. Making every template parameter private
    • D. Converting a template into a macro
    Show answer

    A. Providing a specific implementation for particular template arguments

    Specialization allows a template to have a tailored implementation for specific template arguments. Class templates support full and partial specialization, while function templates support explicit full specialization but not partial specialization.

  12. 12. Which statement about a class template with two type parameters is correct?

    • A. It must use two separate class definitions
    • B. It cannot contain data members
    • C. It can only be instantiated with integer types
    • D. It can accept two types, such as Key and Value
    Show answer

    D. It can accept two types, such as Key and Value

    A template can declare multiple parameters, for example template <typename Key, typename Value>, allowing each parameter to represent a different type.

  13. 13. Which standard C++ library container is a class template?

    • A. std::cout
    • B. std::vector
    • C. std::endl
    • D. std::cin
    Show answer

    B. std::vector

    std::vector is a class template that provides a dynamic-size sequence container, such as std::vector<int> or std::vector<std::string>.

  14. 14. What is a non-type template parameter?

    • A. A parameter that can only be a floating-point variable
    • B. A parameter that must always be a class name
    • C. A template parameter that represents a permitted value rather than a type
    • D. A function parameter that has no declared type
    Show answer

    C. A template parameter that represents a permitted value rather than a type

    A non-type template parameter represents a compile-time value, such as a size used in a declaration like template <std::size_t N>.

  15. 15. Why are templates useful in C++?

    • A. They improve code reuse while preserving type checking
    • B. They eliminate the need for all data types
    • C. They guarantee that programs never produce runtime errors
    • D. They allow every function to use incompatible types without restrictions
    Show answer

    A. They improve code reuse while preserving type checking

    Templates support reusable generic code while allowing the compiler to check whether the operations required by a template are valid for the types used.