C++ Programming Multiple Choice Questions & Answers (MCQs) on “Templates”.
1. Which of the following is used for generic programming?
a) Virtual functions
b) Modules
c) Templates
d) Abstract Classes
Answer: c
Clarification: Templates are used for generic programming. They help in making generic functions and classes hence achieving the generic codes.
2. Which of the following is correct about templates?
a) It is a type of compile time polymorphism
b) It allows the programmer to write one code for all data types
c) Helps in generic programming
d) All of the mentioned
Answer: d
Clarification: Templates are used for generic programming hence allowing to write a single function for all data types. It is a type of compile time polymorphism.
3. What will be the output of the following C++ code?
#includeusing namespace std; template <typename T> void fun(const T&x) { static int count = 0; cout << "x = " << x << " count = " << count; ++count; return; } int main() { fun<int> (1); cout << endl; fun<int>(1); cout << endl; fun<double>(1.1); cout << endl; return 0; }
a)
x = 1 count = 0 x = 1 count = 1 x = 1.1 count = 0
b)
x = 1 count = 0 x = 1.0 count = 1.0 x = 1.1 count = 0
c)
x = 1.0 count = 0.0 x = 1 count = 1 x = 1.1 count = 0
d)
x = 1.0 count = 0.0 x = 1.0 count = 1.0 x = 1.1 count = 0.0
View Answer
Answer: a
Clarification: As template is a type of polymorphism so count becomes 1 for int type because we have called the function for int twice but as we have called it only once for double therefore value of count is 0 for double i.e. for last call.
4. What will be the output of the following C++ function?
#includeusing namespace std; template <typename T> T max(T x, T y) { return (x > y)? x : y; } int main() { cout << max(3, 7) << std::endl; cout << max(3.0, 7.0) << std::endl; cout << max(3, 7.0) << std::endl; return 0; }
a)
7 7.0 7.0
b)
7 7 7
c)
7.0 7.0 7.0
d) Error
Answer: d
Clarification: Here in the template function as both the argument have same type T but in third called of max function we are using 2 types int and float while calling the function hence the program gives error.
5. What will be the output of the following C++ code?
#includeusing namespace std; template <class T> class Test { private: T val; public: static int count; Test() { count++; } }; template<class T> int Test<T>::count = 0; int main() { Test<int> a; Test<int> b; Test<double> c; cout << Test<int>::count << endl; cout << Test<double>::count << endl; return 0; }
a)
2 1
b)
1 2
c)
1 1
d)
2 2
View Answer
Answer: a
Clarification: In this as we have first used int to define the objects of class Test so count is increased to 2 for int types and declared the double object once therefore the constructor is called only once hence value of count is 1.
6. What will be the output of the following C++ code?
#include#include using namespace std; template<class T, class U> class A { T x; U y; }; int main() { A<char, char> a; A<int, int> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; }
a)
2 8
b)
8 2
c)
1 4
d)
4 1
View Answer
Answer: a
Clarification: In this we have made class with two template arguments which are used in defining one variable of each type inside the class. So the total size of the class will depend on the type of U and T. So when we have defined the object with
7. What will be the output of the following C++ code?
#include#include using namespace std; template<class T, class U, class V=double> class A { T x; U y; V z; }; int main() { A<int, int> a; A<double, double> b; cout << sizeof(a) << endl; cout << sizeof(b) << endl; return 0; }
a)
16 24
b)
24 16
c) Error
d) Segmentation fault
Answer: a
Clarification: In this case the class has three variables of type T, U and V = double by default. Now as we have defined objects with types
8. What will be the output of the following C++ code?
#includeusing namespace std; template <class T, int max> int arrMin(T arr[], int n) { int m = max; for (int i = 0; i < n; i++) if (arr[i] < m) m = arr[i]; return m; } int main() { int arr1[] = {10, 20, 15, 12}; int n1 = sizeof(arr1)/sizeof(arr1[0]); char arr2[] = {1, 2, 3}; int n2 = sizeof(arr2)/sizeof(arr2[0]); cout << arrMin<int, 10000>(arr1, n1) << endl; cout << arrMin<char, 256>(arr2, n2); return 0; }
a)
10 1
b)
12 2
c)
10000 256
d) Error
Answer: a
Clarification: In this, we are passing arr and its size in the template function. Now during first call value of m becomes 10 as it is the minimum in the array. In second call the value of m becomes 1 which is minimum in 2nd array.
9. What will be the output of the following C++ code?
#includeusing namespace std; template <class T> T max (T &a, T &b) { cout << "Template Called "; return (a > b)? a : b; } template <> int max <int> (int &a, int &b) { cout << "Called "; return (a > b)? a : b; } int main () { int a = 10, b = 20; cout << max <int> (a, b); }
a) Template Called 20
b) Called 20
c) Error
d) Segmentation fault
Answer: b
Clarification: For T = int we have created a separate definition for the above template function. Hence the call using int takes the newly defined function.
10. What will be the output of the following C++ code?
#includeusing namespace std; template<int n> struct funStruct { static const int val = 2*funStruct<n-1>::val; }; template<> struct funStruct<0> { static const int val = 1 ; }; int main() { cout << funStruct<10>::val << endl; return 0; }
a) 1
b) 1024
c) Error
d) Segmentation fault
Answer: b
Clarification: The above call for struct will call the first struct for n > 0 and second one when n = 0. Therefore when value of n = 10 the until n becomes 0 first struct is called so we will call 2*2*2…10 times*1 which will give the result 210 = 1024.