C++ interview questions and answers focuses on “Simple String Template”. One shall practice these interview questions to improve their C++ programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C++ programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C++ interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.
C++ interview questions on “Simple String Template” along with answers, explanations and/or solutions:
Q 1. What is a template?
A. A template is used to manipulate the class
B. A template is a formula for creating a generic class
C. A template is used for creating the attributes
D. A template is used to delete the class
Show Answer
Answer:-B. A template is a formula for creating a generic classExplanation
Templates are used for creating generic classes to handle different types in single classes.Q 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
Show Answer
Answer:-B. It is used to replace a string with another string at runtimeExplanation
Every string template is used to replace the string with another string at runtime.
Q 3. How to declare a template?
A. tem
B. temp
C. temp()
D. template<>
Show Answer
Answer:-D. template<>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. templateQ 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
Show Answer
Answer:-A. 416AAExplanation
In this program, We are using two template to calculate the square and to find the addition. Output: $ g++ tem.cpp $ a.out 416AA
Q 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
Show Answer
Answer:-B. 24 2.1 4.41Explanation
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
Q 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
Show Answer
Answer:-A. 5.5 Hello WorldExplanation
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
Q 7. How many types of templates are there in c++?
A. 1
B. 2
C. 3
D. 4
Show Answer
Answer:-B. 2Explanation
There are two types of templates. They are function template and class template.
Q 8. Which are done by compiler for templates?
A. type-safe
B. portability
C. code elimination
D. prototype
Show Answer
Answer:-A. type-safeExplanation
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.
Q 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
Show Answer
Answer:-A. same as templateExplanation
The name of the parameter that the template should take same as the template.
Q 10. How many parameters are legal for non-type template?
A. 1
B. 2
C. 3
D. 4
Leave a Reply