CPP

CPP

Function Templates has 9 Questions
What is a function template?

creating a function without having to specify the exact type.
creating a function with having a exact type.
both a & b
none of the mentioned
Which is used to describe the function using placeholder types?

template parameters
template type parameters
template type
none of the mentioned
Pick out the correct statement.

you only need to write one function, and it will work with many different types.
it will take a long time to execute
duplicate code is increased
none of the mentioned
What is the output of this program?
  1.     #include 
  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.     }

100
200
300
100200
What is the output of this program?
  1.     #include 
  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.     }

100
200
50
none of the mentioned
What is the output of this program?
  1.     #include 
  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.     }

2 4 2.2 4.84
2 4
error
runtime error
What is the output of this program?
  1.     #include 
  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.     }

2.1
3.1
3.2
2.1 3.1 4.1
From where does the template class derived?

regular non-templated C++ class
templated class
a or b
none of the mentioned
What can be passed by non-type template parameters during compile time?

int
float
constant expression
none of the mentioned