C++ Programming Questions and Answers – Simple String Template

This section on 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.

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

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
View Answer

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()
View Answer

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;
}
advertisement
advertisement

4. What will be the output of the following C++ code?

  1.     #include <iostream>
  2.     using namespace std;
  3.     template <class T>
  4.     inline T square(T x)
  5.     {
  6.         T result;
  7.         result = x * x;
  8.         return result;
  9.     };
  10.     template <>
  11.     string square<string>(string ss)
  12.     {
  13.         return (ss+ss);
  14.     };
  15.     int main()     
  16.     {
  17.         int i = 4, ii;
  18.         string ww("A");
  19.         ii = square<int>(i);
  20.         cout << i << ii;
  21.         cout << square<string>(ww) << endl;
  22.     }

a) 416AA
b) 164AA
c) AA416
d) AA41A
View Answer

Answer: a
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?

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     template <typename T, typename U>
  4.     void squareAndPrint(T x, U y)
  5.     {
  6.         cout << x << x * x << endl;
  7.         cout << y << " " << y * y << endl;
  8.     };
  9.     int main()
  10.     {
  11.         int ii = 2;
  12.         float jj = 2.1;
  13.         squareAndPrint<int, float>(ii, jj);
  14.     }

a)

   23
   2.1 4.41
advertisement

b)

   24
   2.1 4.41

c)

   24
   2.1 3.41

d) 2.1 3.41
View Answer

Answer: b
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?

  1.     #include <iostream>
  2.     #include <string>
  3.     using namespace std;
  4.     template<typename T>
  5.     void print_mydata(T output)
  6.     {
  7.         cout << output << endl;
  8.     }
  9.     int main()
  10.     {
  11.         double d = 5.5;
  12.         string s("Hello World");
  13.         print_mydata( d );
  14.         print_mydata( s );
  15.         return 0;
  16.     }

a)

   5.5
   Hello World

b) 5.5
c) Hello World
d) Hello
View Answer

Answer: a
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

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
View Answer

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
View Answer

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
View Answer

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.

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]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.