Templates


  1. Which is similar to template specialization?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    function template overloading


  1. What is meant by template specialization?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In the template specialization, it will make the template to be specific for some data types.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template<typename T>
    void loopIt(T p)
    {
    int n = 2;
    T value[n];
    for (int k=0; k < n; k++)
    {
    value[k] = p++;
    cout << value[k] << endl;
    }
    };
    int main()
    {
    float q = 4.25;
    loopIt(q);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    In this program, We are using the non-type template parameter to increment the value in the function template.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class type>
    class Sample
    {
    public:
    Sample()
    {
    };
    ~Sample()
    {
    };
    type FunA(type num1)
    {
    return num1;
    }
    type FunB(type num2)
    {
    return num2;
    }
    };
    int main()
    {
    Sample<int> num1;
    Sample<double> num2;
    cout << num1.FunA(250) << " ";
    cout << num2.FunB(5.225);
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In this program, We are passing the value and returning it from template.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    template <class T7gt;
    T Maximum(T& p, T& q)
    {
    return (p > q ? p : q);
    }
    int main ()
    {
    int k = 6, L = 9, S;
    long Lon = 12, a = 13, b;
    S = Maximum(k, L);
    b = Maximum(Lon, a);
    cout << S << endl;
    cout << b << endl;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In this program, We are using the ternary operator on the template function.