Skip to main content

Templates

1).What is the purpose of templates in C++?

1) To allow generic programming
2) To enable function overloading
3) To define custom data types
4) None of the above

2).Which of the following can be templated in C++?

1) Functions
2) Classes
3) Structures
4) All of the above

3).What does the 'typename' keyword in templates do?

1) Defines a type parameter
2) Declares a function
3) Indicates a template argument
4) None of the above

4).What is the syntax for declaring a template for a function?

1) template<typename T> return_type function_name()
2) template<class T> return_type function_name()
3) Both A and B
4) None of the above

5).What is the main advantage of using class templates?

1) Code reuse for different data types
2) Better performance
3) Faster execution
4) None of the above

6).Can templates be used with non-type parameters?

1) Yes
2) No
3) Only for functions
4) Only for classes

7).What happens if no explicit template specialization is provided?

1) The compiler generates code for every type
2) The code will not compile
3) The function is ignored
4) None of the above

8).What is the difference between 'template<class T>' and 'template<typename T>'?

1) There is no difference
2) template<class T> allows more complex types
3) template<typename T> is more efficient
4) None of the above

9).Which of the following is used to specialize a template?

1) template<>
2) template<class T> specialization
3) template<T> specialization
4) None of the above

10).Can template functions be overloaded?

1) Yes
2) No
3) Only for different types
4) Only for different return types