Templates

1.What is a template in C++?
  • A. A class for input/output operations
  • B. A feature that allows defining generic classes or functions
  • C. A tool for memory management
  • D. A form of inheritance

A feature that allows defining generic classes or functions

A feature that allows defining generic classes or functions

2.Which of the following is the correct syntax for defining a function template in C++?
  • A. template function <T>() {}
  • B. template <T> function() {}
  • C. template <T> void function() {}
  • D. template void function<T>() {}

template <T> void function() {}

template <T> void function() {}

3.What does the keyword typename indicate in a template?
  • A. It specifies a type alias
  • B. It declares a type
  • C. It defines a function template
  • D. It is used to define a type inside a template class

It declares a type

It declares a type

4.What is the purpose of the template <typename T> syntax?
  • A. It defines a variable of type T
  • B. It declares a type for a template function or class
  • C. It specifies the inheritance of a class template
  • D. It defines a function that accepts multiple types

It declares a type for a template function or class

It declares a type for a template function or class

5.What type does a template function return when instantiated?
  • A. Template type itself
  • B. Type of the template parameter
  • C. The base type
  • D. The return type specified in the function signature

Type of the template parameter

Type of the template parameter