C++ Programming Questions and Answers – Templates

«
»

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Templates”.

1. Which of the following is used for generic programming?
a) Virtual functions
b) Modules
c) Templates
d) Abstract Classes
View Answer

Answer: c
Explanation: Templates are used for generic programming. They help in making generic functions and classes hence achieving the generic codes.
advertisement

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

Answer: d
Explanation: 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?

#include <iostream>
using 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
advertisement

d)

x = 1.0 count = 0.0
x = 1.0 count = 1.0
x = 1.1 count = 0.0
View Answer
Answer: a
Explanation: 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?

#include <iostream>
using 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
View Answer

Answer: d
Explanation: 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.
advertisement

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

#include <iostream>
using 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
Explanation: 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<iostream>
#include<stdlib.h>
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
advertisement

b)

8
2

c)

1
4

d)

4
1
View Answer
Answer: a
Explanation: 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 <char,char> type then the class have two variable of char hence making the size of class = 2 which in case of <int,int> = 8.
 
 

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

#include<iostream>
#include<stdlib.h>
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
View Answer

Answer: a
Explanation: 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 <int,int> so its size is 4+4+8 = 16 and second object of <double,double> which leads to 8+8+8 = 24.

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

#include <iostream>
using 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
View Answer

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

#include <iostream>
using 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
View Answer

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

#include <iostream>
using 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
View Answer

Answer: b
Explanation: 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.

Sanfoundry Global Education & Learning Series – C++ Programming Language.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn