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 class
Explanation 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 runtime
Explanation 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. template T max(T a, T b){ return a > b? a : b; }

Q 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. 416AA
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


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.41
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


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 World
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


Q 7. How many types of templates are there in c++?
A. 1
B. 2
C. 3
D. 4

Show Answer Answer:-B. 2
Explanation 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-safe
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.


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 template
Explanation 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

Show Answer Answer:-D. 4
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.

1 comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Author

quizsquestion@gmail.com

Related Posts

Top 100 UK GK Questions (Culture, Sport & Icons)

🎭 Culture (1–35) Q 1. What is the capital of the United Kingdom? Show Answer Answer:-London Q 2. What is the official...

Read out all

Current Affairs Quiz – May 2026

Q 1.Which state has been declared the country’s first paperless judiciary state? A. SikkimB. ManipurC. AssamD. Odisha Show Answer Answer:- A. Sikkim...

Read out all

Culture Sport & Icons Australia GK Question Top 100

Australia’s identity is a vibrant blend of the world’s oldest living culture, a deep-seated passion for sport, and a unique collection of...

Read out all

Current Affairs Quiz – March 2026

Q 1.Where was India–UK Conference on Green Hydrogen Standards and Safety Protocols held? A. New DelhiB. ChennaiC. HyderabadD. Bengaluru Show Answer Answer:-A....

Read out all

Python Questions and Answers – Exception Handling – 3

Python Multiple Choice Questions & Answers (MCQs) focuses on “Exception Handling – 3”. Q 1. What happens if the file is not...

Read out all

Important Events & History Canadian Multiple Choice Question

Q 1. In what year did Canadian Confederation take place? A. 1776 B. 1812 C. 1867 D. 1982 Show Answer Answer:-C. 1867...

Read out all

You cannot copy content of this page