1. What is a template?
A. A template is a formula for creating a generic class
B. A template is used to manipulate the class
C. A template is used for creating the attributes
D. A template is used to delete the class
Answer: A
Explanation:
Templates are used for creating generic classes to handle different types in single classes.
2. Pick out the correct statement about string template.
A. It is used to replace a string
B. It is used to replace a string with another string at runtime
C. It is used to delete a string
D. It is used to create a string
Answer: B
Explanation:
Every string template is used to replace the string with another string at runtime.
3. How to declare a template?
A. tem
B. temp
C. template<>
D. temp()
Answer: C
Explanation:
template<> syntax is used.
An example for calculating max of two ints, floats, doubles, or any other number type where T indicates the type of the parameters passes.
template <typename T>
T max(T a, T B.{
return a > b? a : b;
}
4. What will be the output of the following C++ code?
#include
using namespace std;
template
inline T square(T x)
{
T result;
result = x * x;
return result;
};
template <>
string square(string ss)
{
return (ss+ss);
};
int main()
{
int i = 4, ii;
string ww("A");
ii = square(i);
cout << i << ii;
cout << square(ww) << endl;
}
A. 416AA
B. 164AA
C. AA416
D. AA41A
Answer: A
Explanation:
In this program, We are using two template to calculate the square and to find the addition.
5. What will be the output of the following C++ code?
#include
using namespace std;
template
void squareAndPrint(T x, U y)
{
cout << x << x * x << endl;
cout << y << " " << y * y << endl;
};
int main()
{
int ii = 2;
float jj = 2.1;
squareAndPrint(ii, jj);
}
A.
23
2.1 4.41
B.
24
2.1 4.41
C.
24
2.1 3.41
D. 2.1 3.41
Answer: B
Explanation:
In this multiple templated types, We are passing two values of different types and producing the result.
$ g++ class1.cpp
$ a.out
1210
6. What will be the output of the following C++ code?
#include
#include
using namespace std;
template
void print_mydata(T output)
{
cout << output << endl;
}
int main()
{
double d = 5.5;
string s("Hello World");
print_mydata( d );
print_mydata( s );
return 0;
}
A.
5.5
Hello World
B. 5.5
C. Hello World
D. Hello
Answer: A
Explanation:
In this program, We are passing the value to the template and printing it in the template.
7. How many types of templates are there in c++?
A. 1
B. 2
C. 3
D. 4
Answer: B
Explanation:
There are two types of templates. They are function template and class template.
8. Which are done by compiler for templates?
A. type-safe
B. portability
C. code elimination
D. prototype
Answer: A
Explanation:
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.
9. What may be the name of the parameter that the template should take?
A. same as template
B. same as class
C. same as function
D. same as member
Answer: A
Explanation:
The name of the parameter that the template should take same as the template.
10. How many parameters are legal for non-type template?
A. 1
B. 2
C. 3
D. 4
Answer: D
Explanation:
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.