C++ Programming Questions and Answers – Function Templates – 1

This section on C++ programming interview questions and answers focuses on “Function Templates”. 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++ programming interview questions come with detailed explanation of the answers which helps in better understanding of C++ concepts.

Here is a listing of C++ programming interview questions on “Function Templates” along with answers, explanations and/or solutions:

1. What is a function template?
a) creating a function without having to specify the exact type
b) creating a function with having an exact type
c) creating a function without having blank spaces
d) creating a function without class
View Answer

Answer: a
Explanation: Function template is used to create a function without having to specify the exact type.

2. Which is used to describe the function using placeholder types?
a) template parameters
b) template type parameters
c) template type
d) type parameters
View Answer

Answer: b
Explanation: During runtime, We can choose the appropriate type for the function and it is called as template type parameters.

3. Pick out the correct statement.
a) you only need to write one function, and it will work with many different types
b) it will take a long time to execute
c) duplicate code is increased
d) it will take a long time to execute & duplicate code is increased
View Answer

Answer: a
Explanation: Because of template type parameters, It will work with many types and saves a lot of time.
advertisement
advertisement

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template<typename type>
  4.     type Max(type Var1, type Var2)
  5.     {
  6.         return Var1 > Var2 ? Var1:Var2;
  7.     }
  8.     int main()
  9.     {
  10.         int p;
  11.         p = Max(100, 200);
  12.         cout << p << endl;
  13.         return 0;
  14.     }

a) 100
b) 200
c) 300
d) 100200
View Answer

Answer: b
Explanation: In this program, We are returning the maximum value by using function template.
Output:

$ g++ ftemp.cpp
$ a.out
200

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

advertisement
  1.     #include <iostream>
  2.     using namespace std;
  3.     template<typename type> 
  4.     class Test
  5.     {
  6.         public:
  7.         Test()
  8.         {
  9.         };
  10.         ~Test()
  11.         {
  12.         };
  13.         type Funct1(type Var1)
  14.         {
  15.             return Var1;
  16.         }
  17.         type Funct2(type Var2)
  18.         {
  19.             return Var2;
  20.         }
  21.     };
  22.     int main()
  23.     {
  24.         Test<int> Var1;
  25.         Test<float> Var2;
  26.         cout << Var1.Funct1(200) << endl;
  27.         cout << Var2.Funct2(3.123) << endl;
  28.         return 0;
  29.     }

a)

   200
   3.123
advertisement

b)

   3.123
   200

c) 200
d) 3.123
View Answer

Answer: a
Explanation: In this program, We are passing the values and getting it back from template. And we are using the constructor and destructor for the function template.
Output:

$ g++ ftemp1.cpp
$ a.out
200
3.123

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template<typename type>
  4.     class TestVirt
  5.     {
  6.         public:
  7.         virtual type TestFunct(type Var1)
  8.         {
  9.             return Var1 * 2;
  10.         }
  11.     };
  12.     int main()
  13.     {
  14.         TestVirt<int> Var1;
  15.         cout << Var1.TestFunct(100) << endl;
  16.         return 0;
  17.     }

a) 100
b) 200
c) 50
d) 150
View Answer

Answer: b
Explanation: In this program, We are using class to pass the value and then we are manipulating it.
Output:

$ g++ ftemp3.cpp
$ a.out
200

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

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

a)

   2 4
   2.2 4.84

b) 2 4
c) error
d) 3 6
View Answer

Answer: a
Explanation: In this program, We are passing the values and calculating the square of the value by using the function template.
Output:

$ g++ ftemp4.cpp
$ a.out
2 4
2.2 4.84

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

  1.     #include <iostream>
  2.     using namespace std;
  3.     template<typename T> 
  4.     void loopIt(T x)
  5.     {
  6.         int count = 3;
  7.         T val[count];
  8.         for (int ii=0; ii < count; ii++)
  9.         {
  10.             val[ii] = x++;
  11.             cout <<  val[ii] << endl;
  12.         }
  13.     };
  14.     int main()
  15.     {
  16.         float xx = 2.1;
  17.         loopIt(xx);
  18.     }

a) 2.1
b) 3.1
c)

   2.1
   3.1
   4.1

d) 3.2
View Answer

Answer: c
Explanation: In this program, We are using the for loop to increment the value by 1 in the function template.
Output:

$ g++ ftemp5.cpp
$ a.out
2.1
3.1
4.1

9. What can be passed by non-type template parameters during compile time?
a) int
b) float
c) constant expression
d) string
View Answer

Answer: c
Explanation: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.

10. From where does the template class derived?
a) regular non-templated C++ class
b) templated class
c) regular non-templated C++ class or templated class
d) main function
View Answer

Answer: c
Explanation: Class derived template is derived from regular non-templated C++ class or templated class.

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.