C++ Templates

Last Updated :
Discuss
Comments

Question 1

Which of the following is true about templates.

  1. Template is a feature of C++ that allows us to write one code for different data types.
  2. We can write one function that can be used for all data types including user defined types. Like sort(), max(), min(), ..etc.
  3. We can write one class or struct that can be used for all data types including user defined types. Like Linked List, Stack, Queue ..etc.
  4. Template is an example of compile time polymorphism.
  • 1 and 2

  • 1, 2 and 3

  • 1, 2 and 4

  • 1, 2, 3 and 4

Question 2

What will be the output?

C
#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add(2, 3);
}
  • 5

  • 23

  • Compiler Error

  • 6

Question 3

C
#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;
}
  • 7
    7.0
    7.0
  • Compiler Error in all cout statements as data type is not specified.
  • Compiler Error in last cout statement as call to max is ambiguous.
  • None of the above

Question 4

What will be the output?

CPP
#include <iostream>
using namespace std;

template <typename T>
T square(T x) {
    return x * x;
}

int main() {
    cout << square(4);
}
  • 4

  • 8

  • 16

  • Compiler Error

Question 5

What is the output of the following code?

CPP
#include <iostream>
using namespace std;

template <typename T>
void fun(const T& x) {
    static int count = 0;
    cout << count << " ";
    count++;
}

int main() {
    fun<int>(1);
    fun<int>(2);
    fun<double>(3.5);
}
  • 0 1 0

  • 0 0 0

  • 0 1 2

  • Compiler Error

Question 6

Output of following program? Assume that the size of int is 4 bytes and size of double is 8 bytes, and there is no alignment done by the compiler. C
#include<iostream>
#include<stdlib.h>
using namespace std;

template<class T, class U, class V=double>
class A  {
    T x;
    U y;
    V z;
    static int count;
};

int main()
{
   A<int, int> a;
   A<double, double> b;
   cout << sizeof(a) << endl;
   cout << sizeof(b) << endl;
   return 0;
}
  • 16
    24
  • 8
    16
  • 20
    28
  • Compiler Error: template parameters cannot have default values.

Question 7

Output of following program.

CPP
#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;
}
  • Compiler error, template argument must be a data type.

  • 10
    1
  • 10000
    256
  • 1
    1

Question 8

Output? CPP
#include <iostream>
using namespace std;

template <int i>
void fun()
{
   i = 20;
   cout << i;
}

int main()
{
   fun<10>();
   return 0;
}
  • 10
  • 20
  • Compiler Error

Question 9

Output? CPP
#include <iostream>
using namespace std;

template <class T>
T max (T &a, T &b)
{
    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);
}
  • 20
  • Called 20
  • Compiler Error

Question 10

Output? CPP
#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;
}
  • Compiler Error
  • 1024
  • 2
  • 1

There are 12 questions to complete.

Take a part in the ongoing discussion