250+ TOP MCQs on Simple String Template and Answers

This section on C++ interview questions and answers 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
Answer: a
Clarification: 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
Clarification: 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
Clarification: 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;
}

  250+ TOP MCQs on References – 2 and Answers

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

  1.     #include 
  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
Answer: a
Clarification: 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?

  1.     #include 
  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

b)

   24
   2.1 4.41

c)

   24
   2.1 3.41

d) 2.1 3.41
Answer: b
Clarification: 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 
  2.     #include 
  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
Answer: a
Clarification: 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
Answer: b
Clarification: 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
Clarification: 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
Clarification: 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
Clarification: 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.

Leave a Comment

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

Scroll to Top