Templates(Engineering > Computer Science And Engineering > C++ Language ) Questions and Answers

Question 1. What is a template?
  1.    A template is a formula for creating a generic class
  2.    A template is used to manipulate the class
  3.    A template is used for creating the attributes
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> A template is a formula for creating a generic class


A template is a formula for creating a generic class



Question 2. Pick out the correct statement about string template?
  1.    It is used to replace a string.
  2.    It is used to replace a string with another string at runtime.
  3.    It is used to delete a string.
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> It is used to replace a string with another string at runtime.


Every string template is used to replace the string with another string at runtime.



Question 3. How many types of templates are there in c++?
  1.    1
  2.    2
  3.    3
  4.    4
Explanation:-
Answer: Option B. -> 2


There are two types of templates. They are function template and class template.



Question 4.

What is the output of this program?

#include < iostream >#include < string >using namespace std;template < typename T >void print_mydata(T output){cout
  1.    5.5 Hello World
  2.    5.5
  3.    Hello World
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> 5.5 Hello World


In this program, We are passing the value to the template and printing it in the template.



Question 5. How to declare a template?
  1.    tem
  2.    temp
  3.    template < >
  4.    none of the mentioned
Explanation:-
Answer: Option C. -> template < >


template < >



Question 6. What may be the name of the parameter that the template should take?
  1.    same as template
  2.    same as class
  3.    same as function
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> same as template


same as template



Question 7. Which are done by compiler for templates?
  1.    type-safe
  2.    portability
  3.    code elimination
  4.    all of the mentioned
Explanation:-
Answer: Option A. -> type-safe


The compiler can determine at compile time whether the type associated with a template definition can perform all of the functions required by that template definition.



Question 8. How many parameters are legal for non-type template?
  1.    1
  2.    2
  3.    3
  4.    4
Explanation:-
Answer: Option D. -> 4


The following are legal for non-type template parameters: integral or enumeration type, Pointer to object or pointer to function, Reference to object or reference to function, Pointer to member.



Question 9. Which is used to describe the function using placeholder types?
  1.    template parameters
  2.    template type parameters
  3.    template type
  4.    none of the mentioned
Explanation:-
Answer: Option B. -> template type parameters


During runtime, We can choose the appropriate type for the function
and it is called as template type parameters.



Question 10. What is a function template?
  1.    creating a function without having to specify the exact type.
  2.    creating a function with having a exact type.
  3.    both a & b
  4.    none of the mentioned
Explanation:-
Answer: Option A. -> creating a function without having to specify the exact type.


creating a function without having to specify the exact type.