Here is a listing of C++ interview questions on “Simple String Template” along with answers, explanations and/or solutions:
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
View Answer
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
View Answer
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()
View Answer
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 <iostream>
using namespace std;
template <class T>
inline T square(T x)
{
T result;
result = x * x;
return result;
};
template <>
string square<string>(string ss)
{
return (ss+ss);
};
int main()
{
int i = 4, ii;
string ww("A");
ii = square<int>(i);
cout << i << ii;
cout << square<string>(ww) << endl;
}
a) 416AA
b) 164AA
c) AA416
d) AA41A
View Answer
Explanation: In this program, We are using two template to calculate the square and to find the addition.
Output:
$ g++ tem.cpp $ a.out 416AA
5. What will be the output of the following C++ code?
#include <iostream>
using namespace std;
template <typename T, typename U>
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<int, float>(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
View Answer
Explanation: In this multiple templated types, We are passing two values of different types and producing the result.
Output:
$ g++ tem1.cpp $ a.out 24 2.1 4.41
6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
template<typename T>
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
View Answer
Explanation: In this program, We are passing the value to the template and printing it in the template.
Output:
$ g++ tem2.cpp $ a.out 5.5 Hello World
7. How many types of templates are there in c++?
a) 1
b) 2
c) 3
d) 4
View Answer
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
View Answer
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
View Answer
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
View Answer
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.
Sanfoundry Global Education & Learning Series – C++ Programming Language.
To practice all areas of C++ language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check Computer Science Books
- Practice Programming MCQs
- Apply for Computer Science Internship
- Practice Computer Science MCQs
- Check C++ Books